Python Interview Questions And Answers

python interview questions and answers   Top 33 Python Interview Questions And Answers

1) What is Python? And why we should use Python?

● Python is server side programming language which can be used to create web applications.
● It can easily handle complex arithmetic problems.
● It can be used to handle big data.
● Python is simple and easy to understand.
● The latest version of Python is 3.


2) How to execute simple Python program in windows command prompt?

● Python files have .py extension.
● To execute python program in command prompt we need to write the command in following manner:

C:\>python abc.py


3) In Python, how we can add the comments?

Comments start with hash(#). Python render all the lines expect the lines which have #.


4) What is docstrings in Python?

Just like other programming language Python has multiline comments or documentation capability called as Docstrings.

For this docstrings Python uses triple quotes at the start and end of lines:
Example:
""" This is multi line
docstring """.


5) Can we declare variables in Python?

No

In Python we don't need to explicitly declare the variables. They will take the values which have been assigned to them.
Example:
x = "abc"


6) What will be output of below code snippet?
x = 4
x = "abc"
print(x)

The answer is : abc

In Python variables take the values which have been assigned to them.


7) What will be output of below code snippet?
x = 4
y = "abc"
print(x+y)

This will give an error.

TypeError: unsupported operand type(s) for +: 'int' and 'str'


8) What are the different numeric types in Python?

Different numeric types in Python are:
● Int
● Float
● Complex

Examples:
x = 3 # int
y = 3.4 # float
z = 7f # complex


9) What is type() function in Python?

Type() function is used to detect the type of an object.

Examples:
x = 3 # int
y = 3.4 # float
print(type(x))
print(type(y))

Output is:


10) What is type casting in Python?

Type casting means specify the type of variable.

Casting is done using following constructor functions:
int()
float()
str()


11) What will be the output of this code snippet?

x = int(3.7)
y = int(6)
z = int("4")
print(x)
print(y)
print(z)

Output is :
3
6
4


12) In Python, what is strip() method?

It removes the whitespaces from beginning and end of the string.

Example:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"


13) In Python, which method is used to take the data from the user through command prompt?

Input() method is used to get the data from the user using command prompt.


14) In Python, what is tuple?

Tuple is collection data type in Python which is ordered and unchangeable.


15) What will be output of below code snippet?
b = "world"
print(b[2:5])

Output is :
rld


16) Can we remove items from tuple?

No, we can't remove items from tuple.


17) What is Set in Python?

In Python, set is a collection which is unordered. It is written with curly braces.
Example:
val = { "a","b","c"}


18) What is Dictionary in Python?

These are the collection which are unordered and changeable. They have key, value.
val = {
"a": "1",
"b": "2",
"c": "3"
}


19) What will be the output of this code snippet?

a = 33
b = 200

if b > a:
print("b is greater than a")

This will give an error. IndentationError: expected an indented block


20) What is Elif in Python?

This means else if Python.


21) What is range() function in Python?

It is use to specify the number of values. Example: range(6) will give values from 0 to 5.


22) What will be the output of this code snippet?

for x in range(2, 6):
print(x)

Output is :
2
3
4
5


23) What will be the output of this code snippet?

for x in range(2, 19, 3):
print(x)

Output is :
2
5
8
11
14
17


24) What will be the output of this code snippet?

def my_function(car = "BMW"):
print("I have " + car)

my_function("Audi")
my_function("Merc")
my_function()
my_function("Ford")

Output is :
Audi
Merc
BMW
Ford


25) What is lamda function in Python?

It is used to create anonymous functions. These functions don't have pre defined name.
Example: vd = lambda x,y,z: z*y*2+x


26) What are the different modes of open() function in Python?

Following are the different modes of open() function:
"r" - Read only - this is the default mode
"w" - Write mode
"a" - append mode
"x" - create mode


27) What is the difference between read() and readline() in Python?

read() - this function is used to read the content of the file.
readline() - this function is used to read the single line at a time of the file.


28) In Python, how to create new file if file doesn't exist?

Here is the way to create file if it wont exist.
f = open("text.txt", "w")


29) In Python, is it possible to remove the folder having files?

No, its not possible to remove the folder with files. That is it can only remove empty folders.


30) In Python, what are the built in class attributes?

Every class in Python has built in class attributes which can be accessed by dot operator:
Example:
__dict__ - Dictionary
__name__ - Class name
__module__ - Module in which class is defined


31) In Python, what is special method __del__() used for?

This special method is called as destructor, which invokes when instance is about to destroy. This method can be used to cleanup the resources.


32) In Python, how we can define the constructor of a class?

In Python, __init__() method is the special method which is called as constructor or initialization. Below is the example:
def __init__(self):
print "Calling constructor"


33) What is a difference between match and search in Python regular expression?

Match check only at the starting of the string whereas search checks in complete string.