Vaishnavi

Mar 15, 2021

4 min read

Most Common Python Interview Questions With Answers 2021

Here are the most commonly asked interview questions on Python language in any technical interview round.

Short answers to know the concept and remind it.

Python Interview Questions And Answers 2021

1.What type of language is Python ? Scripting or Programming ?

Python is capable of scripting, but in general sense, it is considered as a general-purpose programming language.

2. What is type conversion in Python ?

int() — converts any data type into integer type

float() — converts any data type into float type

ord() — converts characters into integer

hex() — converts integers to hexadecimal

oct() — converts integer to octal

tuple() — This function is used to convert to a tuple.

set() — This function returns the type after converting to set.

list() — This function is used to convert any data type to a list type.

dict() — This function is used to convert a tuple of order (key,value) into a dictionary.

str() — Used to convert integer into a string.

complex(real,imag) — This function converts real numbers to complex(real,imag) number.

3. What is pickling and unpickling in Python ?

Pickle module accepts any Python object and converts it into a string and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string is called unpickling.

4. How to comment multiple lines in Python ?

All the lines to be commented are to be prefixed by a #.

5. What are docstrings in Python ?

Docstrings are not actually comments, but, they are documentation strings. These docstrings are within triple quotes. They are not assigned to any variable.

6. What is a dictionary in Python ?

It defines one-to-one relationship between keys and values. Dictionaries are used to store data values in key:value pairs.A dictionary is a collection which is ordered, changeable and does not allow duplicates.

Syntax:

dict = {
“name”: “vaishnavi”,
“subject”: “python”,
“marks”: 80
}

7. What is list in Python ?

Lists are used to store multiple items in a single variable.List items are ordered, changeable, and allow duplicate values.And first item has index [0].

Syntax:

list1 = [“maths”, “physics”, “chemistry”]

list2 = [1, 3, 7, 9, 5]
list3 = [True, False, True]

8. What are tuples in Python ?

Tuples are used to store multiple items in a single variable.Tuple items are ordered, unchangeable, and allow duplicate values.And first item has index [0].

Syntax:

mytuple = (“shivani”, “vaishnavi”, “shruti”)

9. Does Python have OOPS Concept ?

Yes, Because Python is an object-oriented programming language.

10. What are decoraters in python ?

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure.it allows programmers to modify the behavior of function or class. Decorators are usually called before the definition of a function you want to decorate.

11. What are Generators in python ?

Python generators are a simple way of creating iterators.Generator is a function that returns an object (iterator) which we can iterate over one value at a time.

Generator function contains one or more yield statements.

12. What is lambda function in Python ?

The normal functions are defined using a keyword “def”, whereas, the anonymous functions are defined using the lambda function.A lambda function can take any number of arguments, but they contain only a single expression.

product = lambda x, y : x * y

13. What are iterators in Python ?

Iterators are the collection of items, and it can be a list, tuple, or a dictionary. Python iterator implements __itr__ and next() method to iterate the stored elements. In Python, we generally use loops to iterate over the collections (list, tuple).

14. What are Python literals ?

Literals can be defined as a data which is given in a variable or constant.

String literals

Numeric literals

Boolean literals

15. What is difference between remove() and del function ?

remove : To delete specific object in a list.

del/pop : To delete an object at a specific location (index) in the list.

16. How to remove whitespaces from string ?

Using strip[str] function

17. What are python parameters ?

Pass by value

Pass by reference

18. What is zip() function in python ?

Python zip() functions return a zip object, which maps a similar index of multiple containers.

Ex: zip(iterator1, itrerator2, iterator3…)

19. What are functions in python ?

Function is a section of program that is written once and can be executed whenever required in the program.

-Built-in functions : copy(), len(), count()

-User defined functions : functions which are defined by users.

20. What are different File Processing modes supported by python ?

Read-only mode

Write-only mode

Read-Write mode

Append mode

21. Is python interpreted language ?

Yes, Python language program runs directly from the source code.

22. What is the main difference between functions and decoraters ?

A function is a block of code that performs a specific task whereas decorater is a function that modifies another functions.

23. What is slicing in python ?

Used to select the range of items from sequence type like list, tuple.

24. What is the use of Help() and dir() functions in python ?

Help() : Use to display the documentation string

dir() : Use to display the defined symbols.

25. What is the difference between Python 2 and Python 3 ?

The difference is in print statemant

Python 2 : print “Hello”

Python 3 : print(“Hello”)

Python 2 contains xrange() method and Python 3. contains range() function.

26. What is xrange() function in python ?

Used to generate a sequnce of number.