Python interview questions and answers 👇
Python Interview Questions
What are Literals in Python?↑
A literal is a succinct and easily visible way to write a value. Literals represent the possible choices in primitive types for that language. Some of the choices of types of literals are often integers, floating point, Booleans and character strings.
What is PYTHONPATH?↑
PYTHONPATH is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
What is a lambda function?↑
In Python, a lambda function is a single-line function declared with no name, which can have any number of arguments, but it can only have one expression. Such a function is capable of behaving similarly to a regular function declared using the Python's def keyword.
What is self in Python?↑
Self is an instance or an object of a class. In Python, this is explicitly included as the first parameter. However, this is not the case in Java where it’s optional. It helps to differentiate between the methods and attributes of a class with local variables.
The self variable in the init method refers to the newly created object while in other methods, it refers to the object whose method was called.
What is the difference between range & xrange.↑
Explain pickling and unpickling in Python.↑
Pickling is the name of the serialization process in Python. Any object in Python can be serialized into a byte stream and dumped as a file in the memory. The process of pickling is compact but pickle objects can be compressed further. Moreover, pickle keeps track of the objects it has serialized and the serialization is portable across versions.
Unpickling is the inverse of pickling. It deserializes the byte stream to recreate the objects stored in the file and loads the object to memory.
What is a dictionary in Python?↑
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.
Explain Multithreading in Python.↑
Multithreading refers to concurrently executing multiple threads by rapidly switching the control of the CPU between threads (called context switching). The Python Global Interpreter Lock limits one thread to run at a time even if the machine contains multiple processors.
What is Inheritance in Python?↑
Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.
What is Scope in Python?↑
The scope defines the accessibility of the python object. To access the particular variable in the code, the scope must be defined as it cannot be accessed from anywhere in the program. The particular coding region where variables are visible is known as scope.
What is pass in Python?↑
The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.
Explain slicing in Python.↑
Slicing is a technique that allows us to retrieve only a part of a list, tuple, or string. For this, we use the slicing operator .
What is Scope Resolution?↑
Scope resolution is required when a variable is used to determine where should its value be come from. Scope resolution in Python follows the LEGB rule. L, Local — Names assigned in any way within a function (or lambda), and not declared global in that function.
A decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
What is the use of help() and dir() functions?↑
Help() and dir(), are the two functions that are reachable from the python interpreter. Both functions are utilized for observing the combine dump of build-in-function. These created functions in python are truly helpful for the efficient observation of the built-in system.
What is Polymorphism?↑
Polymorphism in python defines methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. Also, it is possible to modify a method in a child class that it has inherited from the parent class.
Explain split() and join() in Python.↑
You can use split() function to split a string based on a delimiter to a list of strings. You can use join() function to join a list of strings based on a delimiter to give a single string.
What is encapsulation?↑
Encapsulation is a fundamental concept in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.
What are access modifiers?↑
There are three types of access modifiers in Python: public, private, and protected.
- Variables with the public access modifiers can be accessed anywhere inside or outside the class
- Private variables can only be accessed inside the class
- Protected variables can be accessed within the same package
Why is finalize used?↑
finalize is a direct way to register a cleanup function to be called when an object is garbage collected. This is simpler to use than setting up a callback function on a raw weak reference, since the module automatically ensures that the finalizer remains alive until the object is collected.
What is type conversion?↑
Python defines type conversion functions like int(), float(), str() to directly convert one data type into another. This type of conversion is also called typecasting because the user casts (change) the data type of the objects. This function converts any data type into integer.
What are Python packages?↑
A python package is a collection of modules. Modules that are related to each other are mainly put in the same package. When a module from an external package is required in a program, that package can be imported and its modules can be put to use.
What are python iterators?↑
Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration.
What is swapcase() function?↑
The swapcase() method returns a string where all the upper case letters are lower case and vice versa.
What is docstring?↑
Python documentation string (docstring) is a string literal used in the class, module, function, or method definition. Docstrings are accessible from the doc attribute (doc) for any of the Python objects and also with the built-in help() function. An object's docstring is defined by including a string constant as the first statement in the object's definition.
What is monkey patching?↑
In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, which means monkey patch is a piece of Python code that extends or modifies other code at runtime.
What are lists and tuples?↑
A list is a collection of arbitrary objects, somewhat akin to an array in many other programming languages but more flexible. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets ().
Tuples are identical to lists in all respects, except for the following properties:
- Tuples are defined by enclosing the elements in parentheses (()) instead of square brackets ()
- Tuples are immutable