Blog

Common Interview questions for python in 2022?

Common Interview questions for python
  1. What is the difference between list and tuple in Python?

Ans: In Python lists and tuples are heterogeneous collections of objects. The list has a dynamic nature whereas tuple is having static characteristics. Both are used to store a collection of data and both are indexed and ordered objects, following are the four major differences between list and tuple:

    • List is mutable vs Tuple is immutable
    • List is bit heavier and slower vs Tuple is lightweight and faster
    • The tuples are more memory efficient than the list because tuples have less built-in operations.
    • List is memory consuming whereas tuple is memory efficient.
  1. Why do we need dictionary objects in Python?

Ans : Python dictionary is basically well designed version of data structure that is known as hash map. For example, if list hold many information and on the 57th index salary is stored, it will be difficult to remember the index number. Whereas if we store the same data in a dictionary you can give an identifier to it in the form of a key, which is easy for the user to fetch/manipulate the data from the dictionary. 

  1. What do you know about Negative indexing in Python objects? How can we extract the last 3 elements from a tuple/list?

Ans: Negative indexing in Python is used to eliminate the new-line space from the string and permit the string to except the last character. It is specially used to extract the last part of every group object in a dynamic way, If L is the list ,then L[-3:] gives the last 3 elements. Similar operations can be done in string and tuple.

  1. What is a generator function in Python?

Ans : Generator is the kinda function which generates (yields) values on the fly whenever we need it. Technically speaking, any function which has yield keyword instead of return keyword, is a generator function and it throws a generator object and not the actual data. When we need a stream of data we can generate the values out of this generator object.

  1. What is the difference between sort and sorted function?

Ans: The major difference between sort function and sorted function is that, sort is list function and sorted is built in function, sort will modify the list it is called on whereas sorted function can create any new sequence containing a sorted version of sequence it is given.

  1. What is map, reduce and filter function in Python?

Ans: Map Function-  Map Function is basically a higher-order function which takes mapping function. It can also be implemented with list comprehensions. 

Reduce Function- Reduce Function transform the given list into single value by implementing the given function to all the elements. 

Filter Function- Filter Function works on list and returns a subset of list. 

  1. What is list comprehension ? 

Ans : List comprehension used for creating new list from other iterable like list, arrays, tuples, etc

For Example

[x**2 for x in range(1,25)] –> Works on range iterable of 24 elements and come up with 24 results

  1. What is the Lambda function in Python ? When to use lambda function?

Ans : Lambda functions are generally used when we need results from a function without storing it in memory. So it’s an anonymous function, which takes an input and returns results without being stored in memory. Lambda functions are generally used as an argument in another functions.

  1. What is the significance of finally keyword in exception handling? 

Ans: Finally keyword is the block which we write to execute which must to be executed in any situation .Even if any exception is not handled and there is possibility of program crash, that time also finally gets run.

  1. What is constructor in Python Class ? What is the use of the self keyword ?

Answer : Constructor is the method which constructs the object which is used for instantiating an object from class. In python, _init_ is the constructor which is always called implicitly when we call class as a method

Self is the empty object which has to be passed as an first argument in any method in the class

  1. What is Multiple inheritance ? What is the Diamond inheritance?

Answer : Multiple Inheritance is a class that can be derived from multiple classes in python. It is basically a feature of some object-oriented programming in which an object or class can inherit the characteristics.

Diamond Inheritance is generally used for an ambiguity that arises when two classes B and C inherit A and another D inherits B and C

  1. What are Magic/Dunder methods in Python?  

Answer : Magic Method are special methods that starts and ends with double underscores hence it is named as dunder. It also helps in override functionality for built-in functions.

_init_ is the example of dunder/method which we don’t call directly but being called on creation of an object

  1. What are decorators in python? 

Answer : Decorator is a function which wraps another function and gives an extension to that another function without changing its definition. It is extremely useful as it allows the extension of an existing function, without any modification to original function.

  1. What are steps to follow the CRUD database operation in python?

Answer : Following 6 generic steps

    • Importing DB Library 
    • Creating Connection
    • Creating Cursor to deal with DB Tables with connection object
    • Run queries with the cursor object
    • Commit with the connection object # Absent in case of Select /Dealing with structures
    • Close Connection Object
  1. Regular expressions and its use?  match vs search method?

Answer : When we need to extract insights out of unstructured data(text), Regular expressions are used ideally.

Two things mainly are considered in regular expression

  1. Pattern Creation
  2. Where to find out the pattern

Match matches given pattern at the beginning of the string only , search searches for the occurrence throughout the string, once it finds first occurrence , it stops

Similarity is , both throw match class objects.

Conclusion: 

As we have come into conclusion that Python is the most demanding programming language and many top companies like Facebook, Capgemini, Accenture, Amazon are using python as their basic programming language. It also helps the non-technical and technical background people to take one-step ahead into IT-sector and give a kick start to your career. You can learn and have a complete exposure of Python from fundamental to advanced from Python Training in Pune at Ethan’s Tech who will provide you a complete hands-on and real-time project also give you 100% job assistance to shape your career in a better way.

Inquire Now