Undisputed one of the most popular programming languages these days, Python is a hot choice for both established and beginner programmers. And, ease of the language helps develop some interesting Python Projects that are applicable in the real world. Its simplicity and ease of use lend to its popularity. Not to mention, it is the language of choice for the data science and data visualization fields, along with R.
That being said, Python is a very important language for anyone’s toolkit. And if you are someone who is demonstrating your fluency in it through an interview, then the below questions will make for good practice and warming-up.
Apart from these questions, you will also be given code snippets where you have to deduce the resulting value or statement (or the lack of it). These cannot be predicted and will be dependent on your programming practice.
Q 1) What is the difference between a module and a package in Python?
A 1) Each Python program file is a module that imports other modules like objects. Thus, a module is a way to structure the program. The folder of a Python program is called a package of modules.
Q 2) What are the built-in types available in Python?
A 2) There are mutable and immutable built-in types.
The mutable ones include:
The immutable types include:
Q 3) What is lambda function in Python?
A 3) It is often used as an inline function and is a single expression anonymous function. It is used to make a new function object and return them at runtime.
Q 4) What is meant by namespace?
A 4) The namespace can be visualized as the plot / land where a particular name lives. It is a box containing the mapping of a variable to an object. Whenever we look for this variable, the corresponding object is also searched.
Q 5 ) Explain the difference between a list and a tuple?
A 5) The list is mutable while the tuple is not. Tuples can be hashed as in the case of making keys for dictionaries.
Q 6) Difference between pickling and unpickling?
A 6) Pickling is the process whereby the pickle module accepts any Python object and gives its strong representation, then dumps it into a file using the dump function.
Reversing this process- retrieving the original object from the string- is known as unpickling.
Q 7) What are decorators in Python?
A 7) A Python decorator is a specific change made in the Python syntax for the easy alteration of functions.
Q 8) Difference between generators and iterators?
A 8) In Python, iterators are used to iterate over a group of elements (in a list, for example). The way of implementing these iterators is known as generators. It yields an expression in the function, but otherwise behaves like a normal function.
Q 9) How to convert a number into a string?
A 9) We can use the inbuilt str() function. For an octal or hexadecimal representation, we can use the other inbuilt functions like oct() or hex().
Q 10) What is the use of the // operator in Python?
A 10) Using the // operator between 2 numbers gives the quotient when the numerator is divided from the denominator. It is called the Floor Division operator.
Q 11) Does Python have a Switch or Case statement like in C?
A 11) No, it does not. However, we can make our own Switch function and use it.
Q 12) What is the range() function and what are its parameters?
A 12) The range() function is used to generate a list of numbers. Only integer numbers are allowed, and hence, parameters can be both negative and positive. The following parameters are acceptable:
Where ‘stop’ is the no. of integers to generate, starting from 0. Example: range(5) == [0,1,2,3,4]
range([start], stop[, step])
Start: gives the starting no. of the sequence
Stop: specifies the upper limit for the sequence
Step: is the incrementing factor in the sequence
Q 13) What is the use of %s?
A 13) %s is a format specifier which transmutes any value into a string.
Q 14) Is it mandatory for a Python function to return a value?
A 14) No
Q 15) Does Python have a main() function?
A 15) Yes, it does. It is executed automatically whenever we run a Python script. To override this natural flow of things, we can also use the if statement.
Q 16) What is GIL?
A 16) GIL or the Global Interpreter Lock is a mutex, used to limit access to Python objects. It synchronizes threads and prevents them from running at the same time.
Q 17) Before the use of the ‘in’ operator, which method was used to check the presence of a key in a dictionary?
A 17) The has_key() method
Q 18) How do you change the data type of a list?
A 18) To change a list into a tuple, we use the tuple() function
To change it into a set, we use the set() function
To change it into a dictionary, we use the dict() function
To change it into a string, we use the .join() method
The above list of questions, paired with your own practice on the PC, will help you to crack any and every Python interview ever. Apart from the basics, the only thing left is to practice so that while the interviewer is asking you questions, your mind is already writing and executing the code with it.
For further reference, you can also download the PDF version of Python Interview Questions and Answers mentioned below.