5 Python Programming Language Interview Questions

Below are 5 simple Python Programming Language questions which can be asked in a Python Related job interview.

Q. 1 – Write a Python Program to count occurrences of characters in a word.

import collections

counters = collections.Counter("Computer")

print(counters)
# Prints out Counter({'C': 1, 'o': 1, 'm': 1, 'p': 1, 'u': 1, 't': 1, 'e': 1, 'r': 1})

Q. 2 – Write a Simple Lambda Expression?

sumlambda = lambda x, y : x + y
print(sumlambda(4, 5))

Q. 3 – What is filter keyword in Python Programming Language?

The filter function is a utility function which can skip values based on some condition.

a_list = [3, 4, 5, 6, 7, 8, 9]
for i in filter(lambda x: x % 2 == 0, a):
	print(i)

Q. 4 – Explain Reading/Writing Files in Python Programming Language?

Writing a File in Python

f = open('hello.txt','w')                  # opens a file in write mode
f.write('ComputerScienceHub Website')     # writes this line in file
f.close()                                 # closes the file

Reading a File in Python

f =open('hello.txt','r') 		# opens a file in read mode
text = f.read() 				# reads the entire contents of file in memory
print(text) 					# prints the read text
f.close()                       # closes the file

Q. 5 – Best way to read a File in Python?

with open("hello.txt", "r") as f:       
	for line in f:
		print(line)

Above code block after it ends will automatically close file hello.txt without programmer need to explicitly do it using f.close()

Leave a Comment