Top Python Interview Questions

Python is one of the most popular and widely used programming langauge. There are lot of job requirements for python developers in IT Industry. So our team has come up with Top Python Interview Questions to help you. Dear readers, these Python Programming Interview questions have been specially designed so that you can get acquainted with the nature of the questions you may be ask during your interview.

1. What is Python?

Answer:- Python is a general-purpose programming language and was created by Guido van Rossum in 1991. It is interactive and interpreted language. It supports both structured and object oriented style of programming. Python is an open source language under general public license(GPL).
Advantage of Python is that it has wide range of libraries and in-built functions which aid in rapid development of applications. Python is easy to learn and follows a simple syntax.

2. What are the features of Python?

Answer:- The features of python are as follows :-

1. Python is open source:The Python implementation is under an open source license that makes it freely usable and distributable, even for commercial use.

2. Python is interpreted:Python is a high level language which is interpreted by a python interpreter.

3. Python is cross platform compatible:Python can be executed on all major platforms like Windows, Linux/Unix, OS/2, Mac and others.

4. Python is Object-Oriented: In Python we encapsulate data within the objects as it supports the object oriented style of programming.

5. Python is extensible: Python has a large range of libraries and built in functions which helps in easy and rapid development of applications.

6. Python is Interactive: Python provides user a command prompt where one can interact directly with interpreter to write programs.

3. What are identifiers in Python?

Answer:- In Python any variable, function, class, module and object are identified using a name which is called as identifier. An identifier can start with uppercase or lowercase character or an underscore (_) followed by any number of underscores, letters and digits. All identifiers in python are case sensitive.

Example: height=10 // In the example height is an identifier.

4. What are Keywords in Python?

Answer:- Keywords are the reserved words in python. So keywords cannot be used as an identifier.

Few of the keywords are listed below.
Example: if, else, elif, for, where, break, continue

5. What is type conversion in python?

Answer:- When we perform any operation on variables of different datatypes, the data of one variable will be converted to a higher datatype among the two variables and the operation is completed. When this conversion is done by interpreter automatically then it is known as implicit type conversion while convertions is done by user then it is called explicit type conversion.


6. What are functions in python?

Answer:- Functions are set of instructions to perform a specific task. Below is the syntax of functions in python.

def function_name([arg1,...,argn]): 
[return value] 
variable_name = function_name([val1,...,valn])

7. How many types of aruments are there in python?

Answer:- Programming languages allows controlling the ordering and default values of arguments.
1. Positional : Default way of specifying arguments. In this, the order, count and type of actual argument should exactly match to that of formal argument. Else, it will result in error.

def function_name(arg1, arg2): 
return result
res = function_name(val1, val2)

2. Keyword : Allow flexibility in order of passing actual arguments by mentioning the argument name.

def function_name(arg1, arg2): 
return result
res = function_name(arg2=val2, arg1=val1)

3. Default : Allow to sepecify the default value for an argument in the function signature. It is used only when no value is passed for that argument else it work normally. In python default arguments should be last in order.

def function_name(arg1, arg2=default value): 
return result
res = function_name(val1)

4. Variable argument count : Allow function to have variable number of arguments. In python, any argument name starting with '*' is consider to be vary length argument. It should be last in order. It will copy all values beyond that position into a tuple.

def function_name(arg1, arg2, *arg3): 
return result
res = function_name(val1, val2, val3, val4, val5)

8. What are Collections in Python?

Answer:- When we want to treat some data as a group, it would not be good to create individual variables for each data. We can store them together as a collection.

There are many collection data types which are supported by Python :-

1. List :- List can be used to store a group of elements together in a sequence.
2. Tuple :- A tuple is an immutable sequence of Python objects. Tuples are sequences, just like lists.
3. String :- In a program, not all values will be numerical. We will also have alphabetical or alpha numerical values. Such values are called strings.
4. Set :- A set is an unordered group of values with no duplicate entries. Set can be created by using the keyword set or by using curly braces {}. set function is used to eliminate duplicate values in a list.
5. Dictionary :- A dictionary can be used to store an unordered collection of key-value pairs. The key should be unique and can be of any data type. Like lists, dictionaries are mutable.

9. What is random module?

Answer:-Python has many inbuilt packages and modules. One of the most useful modules is random. This module helps in generating random numbers.

The code given below generates a random number between x and y-1 (both inclusive) using the randrange function of the random module.

import random
output :
Any random number between 20 to 30.

10. What is math module?

Answer:- math is another useful module in Python. Once you have imported the math module, you can use some of the below functions:

1. math.ceil(x) :- Smallest integer greater than or equal to x
2. math.floor(x) :- Largest integer smaller than or equal to x
3. math.factorial(x) :- Factorial of x
4. math.fabs(x) :- Gives absolute value of x

11. What is Regular Expression in Python?

Answer:- Files can be accessed from a python program for reading the data from a file and writing the data into a file.

1. Opening a file in python :- Python provides a function called open() to open a file programmatically. open() function returns a file object using which other file operations like reading, writing and closing of the file are done.
2. Closing a file :- The number of files that can be simultaneously opened by a program is limited. So it is very important to close all the files, once the operations are completed.
3. Reading from file :- Python provides readline() function to read the single line from the file at a time. When the end of the file reached it returns an empty string.
4. Writing from file :- Python provides write(data) function to write the given data which is a string into the file and it returns the number of characters written into file.

12. What is Exception handling in python?

Answer:- Sometimes the programs may misbehave or terminate/crash unexpectedly due to some unexpected events during the execution of a program. These unexpected events are called as exceptions and the process of handling them to avoid misbehavior or crashing the program is called as exception handling.

13. What is seek() function in python?

Answer:- Python provides seek() function to navigate the file object pointer to the required position specified.

Syntax :,[whence])
//file_object indicates the file object pointer to be navigated
//offset indicates which position the file object pointer is to be navigated

14. What is finally in python?

Answer:- A finally block of statement is an optional part of the try-except statements. A code written inside the finally block will ALWAYS be executed. finally block is majorly used to close the database connections in the programs which involves database connectivity.

15. What is PEP 8??

Answer:- PEP 8 is a coding convention, a set of recommendations, about how to write your Python code more readable.

16. What is pickling and unpickling in Python?

Answer:- Pickling is a way to convert a python object (list, dict, etc.) into a character stream. Pickle has two main methods. The first one is dump, which dumps an object to a file object and the second one is load, which loads an object from a file object.

While the process of retrieving original Python objects from the stored string representation is called unpickling.

17. What is namespace in Python?

Answer:- In Python, every name introduced has a place where it lives and can be hooked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get corresponding object.

18. What is lambda in Python?

Answer:- A lambda function is a small anonymous function (single-line function). A lambda function can take any number of arguments, but can only have one expression.

Syntax => lambda arguments : expression

19. Why we use Lambda Functions?

Answer:- Lambda functions are used when you need a function for a short period of time. This is commonly used when you want to pass a function as an argument to higher-order functions, i.e functions that take other functions as their arguments.

20. What is unittest in python?

Answer:- Python unittest module is used to test a unit of source code. Suppose, you need to test your project. You know what kind of data the function will return. After writing huge code, you need to check it whether the output is correct or not.

21. What is PYTHONPATH??

Answer:- PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages.

22. Is python case sensitive?

Answer:- Yes. Python is a case sensitive language.

23. What is self in Python?

Answer:- Python self variable is used to bind the instance of the class to the instance method. It helps to differentiate between the methods and attributes of a class with local variables.

24. What is __init__ in Python?

Answer:- "__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.

25. Does Python have OOps concepts?

Answer:- Python is an object-oriented programming language. This means that any program can be solved in python by creating an object model. However, Python can be treated as procedural as well as structural language.

26. How will you capitalize the first letter of string?

Answer:- In Python, the capitalize() method capitalizes the first letter of a string. If the string already consists of a capital letter at the beginning, then, it returns the original string.

27. What are docstrings in Python?

Answer:- Docstrings are not actually comments, but they are documentation strings. These docstrings are within triple quotes. They are not assigned to any variable and therefore, at times, serve the purpose of comments as well.

Using docstring as a comment.
This code divides 2 numbers
Output :-

28. What is difference between range and xrange?

Answer:- The differences between range and xrange are as follows :-

range xrange
Return list object Return integer object
Access via list method Access via index
Generate all item at once Generate one item at a time
slower for larger range Faster
High memory requirement Low memory requirement
python 2 and python 3 python 2

29. What are the differences between list and tuple?

Answer:- The differences between list and tuple are as follows :-

list tuple
The item surrounded by [] The item surrounded by ()
List are mutable in nature tuple are immutable in nature
There are 46 method available for list There are 33 method available for list
In dictionary, we can not use list as keys In dictionary, we can use tuple as keys

30. What are the differences between python 2 and 3?

Answer:-The main differences between python 2 and python 3 are as follows :-

python 2 python 3
ASCII is used Unicode is used
integer size limited to 32 bits integer size unlimited
xrange range
complex Simplified
leak of variable never change change while using it inside for loop
print statement is treated more as statement print() function is treated as function

Also check :


* You must be logged in to add comment.