Python Questions
Warm up for Python interview with most asked questions and answers for junior, mid, and senior roles.
What is Python?
Show AnswerHide Answer
Python is a high-level programming language designed to be easy to read and simple to implement.
It is open source, which means it is free to use, even for commercial applications.
What are some advantages of Python?
Show AnswerHide Answer
Some of the advantages of Python are:
- Easy to Read, Learn and Write
- Improved Productivity
- Interpreted Language
- Dynamically Typed
- Vast Libraries Support
- Portability
What is swapcase() function in the Python?
Show AnswerHide Answer
The swapcase() function in Python is used to swap the case of the characters in a string.
How to remove whitespaces from a string in Python?
Show AnswerHide Answer
The strip() function in Python is used to remove whitespaces from a string.
What are immutable objects in Python?
Show AnswerHide Answer
Immutable objects in Python are objects that cannot be changed after they are created.
The following are examples of immutable objects:
- Numbers (int, float, bool,…)
- Strings.
- Tuples.
- Frozen sets.
What is the function of “self”?
Show AnswerHide Answer
The self parameter is a reference to the current instance of the class.
Is it possible to have static methods in Python?
Show AnswerHide Answer
Yes, it is possible to have static methods in Python. Static methods are methods that are called without instantiating the class.
What is a None value?
Show AnswerHide Answer
The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.
is Python interpreted language?
Show AnswerHide Answer
Yes, Python is an interpreted language.
What are iterators in Python?
Show AnswerHide Answer
Iterators in Python are objects that can be used to iterate over a sequence of items.
What are functions in Python?
Show AnswerHide Answer
Functions are the basic building blocks of Python. They are used to perform a specific task.
What are global, protected, and private attributes in Python?
Show AnswerHide Answer
Variables with the public access modifiers can be accessed anywhere inside or outside the class
The private variables can only be accessed inside the class,
Protected variables can be accessed within the same package.
What is Pandas?
Show AnswerHide Answer
Pandas is a Python library for data manipulation and analysis. It provides high-performance, easy-to-use data structures and data analysis tools.
How to overload constructors or methods in Python?
Show AnswerHide Answer
Overloading constructors or methods in Python is done by using the __init__() method.
What is __init__?
Show AnswerHide Answer
__init__ is a special method that is called when an object is instantiated. It is used to initialize the object.
What is the usage of enumerate () function in Python?
Show AnswerHide Answer
The enumerate function returns an enumerate object. It is a way to iterate over an iterable and return both the index and value of each item in the iterable.
What is a dictionary in Python?
Show AnswerHide Answer
Dictionaries are used to store data in key-value pairs.
What is the difference between deep and shallow copy?
Show AnswerHide Answer
Deep copy is a copy of the object itself while shallow copy is a copy of the object’s reference.
What is the purpose of the single underscore “_” variable in Python?
Show AnswerHide Answer
The single underscore “_” variable is used to indicate that a variable is private and should not be used outside of the class.
Explain docstring in Python?
Show AnswerHide Answer
A docstring is a string literal used to document the purpose of a Python module, function, class, or method.
What is pickling and unpickling in Python?
Show AnswerHide Answer
Python docstrings are the string literals that appear right after the definition of a function, method, class, or module. Let's take an example.
What are python modules?
Show AnswerHide Answer
In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program.
What is Flask?
Show AnswerHide Answer
Flask is a web framework, it’s a Python module that lets you develop web applications easily.
What is namespace in Python?
Show AnswerHide Answer
Namespaces in Python. A namespace is a collection of currently defined symbolic names along with information about the object that each name references.
What's the difference between the list methods append() and extend()?
Show AnswerHide Answer
The append() adds a single element to the end of the list while . extend() can add multiple individual elements to the end of the list.
What is the python “with” statement designed for?
Show AnswerHide Answer
The with statement in Python is used for resource management and exception handling. You'd most likely find it when working with file streams.
What is a "callable"?
Show AnswerHide Answer
A callable is an object that can be called. For example, a function is callable, but classes are not callable.
What is lambda functions in Python?
Show AnswerHide Answer
Lambda functions are anonymous functions in Python. They are used to create small anonymous functions.
What are decorators in Python?
Show AnswerHide Answer
Decorators are functions that are used to modify the behavior of other functions.
What is PEP 8?
Show AnswerHide Answer
PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code.
What is Python literals?
Show AnswerHide Answer
Literals in Python is defined as the raw data assigned to variables or constants while programming.
What is zip() function in Python?
Show AnswerHide Answer
The zip() function in Python is used to create a sequence of tuples, and it takes any number of iterables as arguments.
What are descriptors?
Show AnswerHide Answer
Descriptors are objects that provide access to the properties of an object.
What is pickling and unpickling?
Show AnswerHide Answer
Pickling is the process of converting a Python object into a byte stream, and unpickling is the process of converting a byte stream into a Python object.
What is negative index in Python?
Show AnswerHide Answer
Negative index in Python is used to access the elements of a list or a tuple from the end of the list or tuple.
What is Pass in Python?
Show AnswerHide Answer
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.
What are Keywords in Python?
Show AnswerHide Answer
Python keywords are special reserved words that have specific meanings and purposes and can't be used for anything but those specific purposes.
List the common built-in datatypes in Python?
Show AnswerHide Answer
The following are the common built-in data types in Python:
- Binary Types: memoryview, bytearray, bytes
- Boolean Type: bool
- Set Types: frozenset, set
- Mapping Type: dict
- Sequence Types: range, tuple, list
- Numeric Types: complex, float, int
- Text Type: str
What is a generator in Python?
Show AnswerHide Answer
A generator is a special type of function which does not return a single value, instead, it returns an iterator object with a sequence of values.
What are metaclasses in Python?
Show AnswerHide Answer
A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass.
What is GIL?
Show AnswerHide Answer
The Global Interpreter Lock (GIL) is a synchronization mechanism in Python that prevents multiple threads from running Python code at the same time.
What are the different stages of the Life Cycle of a Thread?
Show AnswerHide Answer
The Life Cycle of a Thread is as follows:
- New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
- Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
- Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. Thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
- Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
- Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.