Interview questions Python with sample answer

Md Attaullah
4 min readDec 30, 2020

Is python interpreted or compiled?

Python is an “interpreted” language.

This means it uses an interpreter. An interpreter is very different from the compiler.

An interpreter executes the statements of code “one-by-one” whereas the compiler executes the code entirely and lists all possible errors at a time.

That’s why python shows only one error message even though your code has multiple errors. This will help you to clear errors easily and it definitely will increase the execution speed

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.

Example: d = dict([(‘jan’, 1), (‘feb’, 2), (‘march’, 3)])

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.

We can use the getsource() method of inspect module to get the source code of the function.It returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string. An IOError is raised if the source code cannot be retrieved.

If the function is compiled from a string, stream or imported from a compiled file, then you cannot retrieve its source code.

import inspect
class foo:
def bar():
print 'Hello'
print(inspect.getsource(foo))#OUTPUT
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)

Where

  • Object − A string or a code object passed onto the method.
  • 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?

Metaclasses

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.

Metaclasses aren’t supported by every object oriented programming language. Those programming language, which support metaclasses, considerably vary in way they implement them. Python provides you a way to get under the hood and define custom metaclasses.

Dataclasses

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.

class Example:

def __init__(self):

print("Instance Created")

# Defining __call__ method

def __call__(self):

print("Instance is called via special method")

# Instance created

e = Example()

# __call__ method will be called

e()

Output :

Instance Created
Instance is called via special method

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

--

--