Python interview questions

KnowledgeMandir
Reading Time: 9 minutes

Total views 981 , and 1 views today

As a lifelong student we must pass all the exams of life, and these exams makes us strong. Strong enough to survive in this competition.

One of them is to crack the job interview and to get a better job. Most of the python professionals get confused during the interview even if they know all the things correctly.

So, I have collected all common python interview questions in this article for you. Which will definitely help you to crack the interview.

1. What is python ?
  • Python was created by Guido van Rossum, and released in 1991
  • Python is an interpreted, object-oriented, high-level, free, and open-source computer programming language. It can run equally on different platforms such as Windows, Linux, UNIX, Mac, Raspberry Pi, etc. 
  • It can be used to build any type of application with the right tools and libraries. 
  • It is widely used in data science, machine learning, and artificial intelligence.
  • Python has a simple syntax. It is very easy to learn and it requires less code to develop applications. 
  • Python supports Automatic Garbage Collection. This means when the objects are no longer needed, python deletes those objects automatically to free the memory space.
2. What is the difference between a Shallow Copy and Deep Copy?

Shallow Copy

Deep Copy

copy.copy(x) : It performs the shallow copy.  copy.deepcopy(x) : It performs the deep copy. 
A shallow copy is a copy of an object that stores the reference of the original elements.
A Deep copy is a copy of the original object that stores both original and repetitive copies.
In shallow copy, any changes made to a copy of the object will reflect in the original object.
In a deep copy, any changes made to a copy of an object do not reflect in the original object.
It stores references of the object in the main memory.
It stores copies of the object values.
Shallow copy is faster than Deep copy. Deep Copy is slower than Shallow copy.
3. Explain the difference between the list and the tuple.

List

Tuple

A list is Mutable (Objects which can be changed after creation). A tuple is immutable (An object which can’t change after creation).
We can add, modify or remove the items after the list is created. We can not add, modify or remove the items after the tuple is created.
List iteration is slower and time-consuming. Tuple iteration is faster.
The list takes more memory. A list is stored in two blocks of memory (One is fixed-sized and the other is variable-sized for storing data.) A tuple takes less memory. a tuple uses only required memory. It is not over-allocated as it is not resizable. A tuple is stored in a single block of memory.
The list data is stored in square brackets []. For example my_list = [1,2,3] The tuple data is stored in () brackets. For example my_tuple = (1,2,3)
4. Explain the difference between the del, remove(), and pop().

del

remove()

pop()

del is a keyword.
remove() is a method.
pop() is a method.
del removes the item at a specific index. (Basically works on the index)
remove() removes the first matching value/object. It does not do anything with the indexing (Basically works on the value) pop() removes the item at a specific index. (Basically works on the index)
The del keyword doesn’t return any value.
The remove() method doesn’t return any value. The pop() returns the deleted values.
The del keyword can delete the single value from the list or delete the whole list at a time.
The remove() method deletes only one value at a time from the list. The pop() method deletes only one value at a time from the list.
It raises an index error when the index doesn’t exist in the list.
It raises the value error when the value doesn’t exist in the list. It raises an index error when the index doesn’t exist in the list.
Syntax : del list[index] or del list Syntax : list.remove(value) Syntax : list.pop(index) or list.pop()
5. Explain difference between the append(), extend() and insert() method. 

append()

insert()

extend()

append() method is used to add a single item to the end of a list insert() method is used to add a single item at the desired position in the list extend() method is used to append several items to a list as an individual item
append() takes a single element as a parameter/argument. insert() takes two arguments. The first is for the index where you want to insert the element, and the second is for the element to be inserted. extend() takes an iterable as a parameter/argument. (list, tuple, dictionaries, sets, strings).
By using the append() method, the length of the list will increase by 1. By using the insert() method, the length of the list will increase by 1. By using the extend() method, the length of the list will increase by the number of elements in the iterable.
Syntax : my_list.append(object) Syntax : my_list.insert(index, value)  Syntax : my_list.extend(iterable)
6. What is the break, continue and pass statement in python.

1. Break statement:

The Break statement is used to stop the execution of the program. It will completely break out the current loop. Which means it will not run any more statements of the block. The break statement can be used in both for and while loops.

2. Continue statement:

The continue statement is used to skip all the remaining statements inside a loop for the current iteration only and, it goes back to the beginning of the loop. The continue statement can be used in both for and while loops.

3. Pass statement:

In python, a pass statement means nothing. When the pass statement is executed, nothing happens. It creates a placeholder for the code that we want to implement in the future.

For example, suppose we want a function or a loop that is not needed right now. But we want to implement it in the future. And we all know that we can’t leave a function or a loop empty. And if we do so, then the interpreter will give an error. So, in such a scenario, we can use a pass statement to create a body that does nothing.  

7. What is the lambda function in python? Explain with an example. 
  • In Python, lambda functions are anonymous functions and an anonymous function is a function that is defined without a name. 
  • As we know that the normal functions are defined using the def keyword whereas anonymous functions are declared using the lambda keyword
  • lambda function can have any number of arguments, but only one expression. 
  • The lambda function is a single line function. 

Syntax: lambda arguments: expression     

KnowledgeMandir

8. Explain map(), filter() and reduce() function in python.

1. The map() function:

This function takes another function as a parameter along with a sequence of iterables (list of elements) and returns an output after applying the function to each iterable present in the sequence. Its syntax is as follows:

SYNTAX: map(func, *iterables)  

  • The func is the function used to define the condition or the expression that is then applied to the iterable. Here, the asterisk(*) means there can be as many iterables as possible. 
  • The map function returns the map object. To get the result as a list, the built-in list() function can be applied on the map function i.e. list(map(func, *iterables)).
  • The map function can take user-defined functions as well as lambda functions as a parameter. 

KnowledgeMandir

2. The filter() function:

  • The filter() takes a function object and an iterable and creates a new list that contains only elements that satisfy a certain condition. i.e. the function we passed returns True
  • This function also can take user-defined functions and lambda functions as a parameter. 

SYNTAX: filter(func, *iterables)  

KnowledgeMandir

3. The reduce() function:

  • The reduce() function works differently. The reduce() function applies a function to iterables and returns a single value. 
  • The map() and filter() are in-built in python, but to use the reduce() function we need to import functools

SYNTAX: reduce(func, *iterables)  

KnowledgeMandir

9. What is the difference between a class method and a static method?

Class method

Static method

The @classmethod decorator is used to define the class method.  The @staticmethod decorator is used to define the static method. 
It pass the “cls” as the first parameter. It doesn’t pass any parameter to the method. 
Class methods can access and modify the class state.  Static methods can’t access or modify the class state. 
10. What is a decorator in Python? 
  • Decorators in Python are essentially functions that add functionality to an existing function in Python without modifying the function itself.
  • A decorator in python is a function that receives another function as an argument. The behavior of the argument function is extended by the decorator without actually modifying it.
  • The decorators can be applied to a function using the @decorator_name syntax in Python. The decorator uses the bottom-up approach to execute. 
11. Explain the self in python.
  • The self is used to represent an instance of the class. The self is used to access the attributes and methods of the class in Python.
  • The use of a self variable in Python helps to differentiate between the instance attributes (and methods) and local variables.  

KnowledgeMandir

12. How to declare a protected variable in Python? 
  • Protected members of a class are accessible from within the class and are also available to its sub-classes. 
  • To make an instance variable protected is to add a prefix “ _ (single underscore) ” to it. This prevents it from being accessed unless it is from within a sub-class.

KnowledgeMandir

13. What is the zip() method in Python?
  • The zip() method takes one or more iterables (such as list, tuple, string, etc.) and constructs the iterator of tuples where each tuple contains the elements from each iterable. 
  • zip() functions return the zip object that works as an iterator. To get the result as a list, the built-in list() function can be applied to the zip function.
  • If the passed iterators have different lengths, the iterator stops when the shortest input iterable is exhausted.
  • Syntax: zip(*iterators) 

KnowledgeMandir

14. What is __init__() method in Python?

The __init__ method is a constructor for any Python class. Constructors are used to initializing the object’s state. The __init__ method is used to assign values to the data members of the class when an object of the class is created. It is called whenever an object is created from a class. It is only used within classes.  

KnowledgeMandir

15. What are the Iterables in Python? 

An Iterables are the objects which are capable of being iterated over with the help of for loop. Lists, tuples, dictionaries, sets, strings – are the iterable objects that can be iterated over in a for a loop. It can return a value one by one using a for a loop. 

16. ([1,2],[3,4]) Is it changeable ?

KnowledgeMandir

17. WAP to get a list of even numbers from the range of 1 to 10 using list comprehension. 

KnowledgeMandir

18. WAP to find the square root of list elements using list comprehension. List = [2, 4, 9, 12, 15, 20]

KnowledgeMandir

19. WAP to get sorted list of elements without using any built-in functions. List = [56, 45, 2, 27, 35, 15, 89, 10]

20. How to merge 2 lists. L1 = [2, 5, 9, 12, 15] , L2 = [1, 4, 8, 11, 14]

KnowledgeMandir

21. How to find the missing elements from the list of 1 to 10 elements.

KnowledgeMandir

22. How to iterate through two lists in parallel?

KnowledgeMandir

23. WAP to count occurrences of list elements.

KnowledgeMandir

24. Print all the elements from the list which are located at multiple of 3 index.

KnowledgeMandir

25. How to replace each element of the list with the remaining element of multiplication number. 

KnowledgeMandir

I hope you’ve got all the common question for python interview and their answers for now, let me know in the comment section if this article have helped you to crack the interview.

ALL THE BEST! 👍🏻

For Reading more such interesting blogs on coding please click here

1 thought on “Python interview questions”

Leave a Comment

Your email address will not be published. Required fields are marked *