Interview questions Python with sample answer

Is python interpreted or compiled?

Python is an “interpreted” language.

Are there ways to compile the code?

Python no need any compile because it source code is automatically compiled into Python byte code. All the python file to be saved in .py exe file.

Which is faster in python — Searching in a list or a dictionary. And why?

Dictionary is faster than list.

In Python, a dictionary is an unordered and mutable Python container that stores mappings of unique keys to values. Dictionaries are written with curly brackets ({}), including key-value pairs separated by commas (,). A colon (:) separates each key from its value.

How can we get the code of a python object?

In python, lists come under mutable objects, and tuples come under immutable objects. It is the reason creating a tuple is faster than List. It also explains the slight difference in indexing speed is faster than lists, because in tuples for indexing it follows fewer pointers. The reason behind the same is that Python implements dictionaries using hash tables. Dictionaries are Python’s built-in mapping type and so have also been highly optimized. Sets are implemented in a similar way.

import inspect
class foo:
def bar():
print 'Hello'
class foo:
def bar():
print 'Hello'

What are the ways to make python code work faster?

Here are some of the ways to make python code run faster.

  1. Avoid unwanted loops
  2. use some of Python’s “speedup” applications
  3. Keep Python code small and light
  4. Use some of Python’s “speedup” applications
  5. . Using generators & sorting with keys

What is exec function and how we can use it?

Exec function can dynamically execute code of python programs. The code can be passed in as string or object code to this function. The object code is executed as is while the string is first parsed and checked for any syntax error. If no syntax error, then the parsed string is executed as a python statement.

Syntax for exec() Function

exec(object, globals, locals)
  • globals − A dictionary of available global methods and variables.
  • locals − A dictionary of available local methods and variables.

What are metaclasses and dataclasses in python?


A metaclass is a class whose instances are classes. Like an “ordinary” class defines the behavior of the instances of the class, a metaclass defines the behavior of classes and their instances.


Dataclasses are python classes but are suited for storing data objects.

  1. They store data and represent a certain data type. Ex: A number.
  2. They can be compared to other objects of the same type. Ex: A number can be greater than, less than, or equal to another number

To understand Dataclasses, we shall be implementing a simple class that holds a number, and allows us to perform the above mentioned operations.

what is __call__ method in python?

The __call__ method is used to specify in a class what will be done if some instance of the class is called as a function.

Instance Created
Instance is called via special method

Taking the time to read not only help you further your career prospects,

Explore me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store