Python Questions

Warm up for Python interview with most asked questions and answers for junior, mid, and senior roles.

Junior

What is Python?

Show 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.

Junior

What are some advantages of Python?

Show Answer

Some of the advantages of Python are:

  • Easy to Read, Learn and Write
  • Improved Productivity
  • Interpreted Language
  • Dynamically Typed
  • Vast Libraries Support
  • Portability
Junior

What is swapcase() function in the Python?

Show Answer

The swapcase() function in Python is used to swap the case of the characters in a string.

Junior

How to remove whitespaces from a string in Python?

Show Answer

The strip() function in Python is used to remove whitespaces from a string.

Junior

What are immutable objects in Python?

Show 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.
Junior

What is the function of “self”?

Show Answer

The self parameter is a reference to the current instance of the class.

Junior

Is it possible to have static methods in Python?

Show Answer

Yes, it is possible to have static methods in Python. Static methods are methods that are called without instantiating the class. 

Junior

What is a None value?

Show 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.

Junior

is Python interpreted language?

Show Answer

Yes, Python is an interpreted language.

Junior

What are iterators in Python?

Show Answer

Iterators in Python are objects that can be used to iterate over a sequence of items.

Junior

What are functions in Python?

Show Answer

Functions are the basic building blocks of Python. They are used to perform a specific task.

Mid

What are global, protected, and private attributes in Python?

Show 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.

Mid

What is Pandas?

Show Answer

Pandas is a Python library for data manipulation and analysis. It provides high-performance, easy-to-use data structures and data analysis tools.

Mid

How to overload constructors or methods in Python?

Show Answer

Overloading constructors or methods in Python is done by using the __init__() method.

Mid

What is __init__?

Show Answer

__init__ is a special method that is called when an object is instantiated. It is used to initialize the object.

Mid

What is the usage of enumerate () function in Python?

Show 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.

Mid

What is a dictionary in Python?

Show Answer

Dictionaries are used to store data in key-value pairs.

Mid

What is the difference between deep and shallow copy?

Show Answer

Deep copy is a copy of the object itself while shallow copy is a copy of the object’s reference.

Mid

What is the purpose of the single underscore “_” variable in Python?

Show Answer

The single underscore “_” variable is used to indicate that a variable is private and should not be used outside of the class.

Mid

Explain docstring in Python?

Show Answer

A docstring is a string literal used to document the purpose of a Python module, function, class, or method.

Mid

What is pickling and unpickling in Python?

Show 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.

Mid

What are python modules?

Show Answer

In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program.

Mid

What is Flask?

Show Answer

Flask is a web framework, it’s a Python module that lets you develop web applications easily.

Mid

What is namespace in Python?

Show Answer

Namespaces in Python. A namespace is a collection of currently defined symbolic names along with information about the object that each name references.

Mid

What's the difference between the list methods append() and extend()?

Show 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.

Mid

What is the python “with” statement designed for?

Show 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. 

Mid

What is a "callable"?

Show Answer

A callable is an object that can be called. For example, a function is callable, but classes are not callable. 

Mid

What is lambda functions in Python?

Show Answer

Lambda functions are anonymous functions in Python. They are used to create small anonymous functions.

Mid

What are decorators in Python?

Show Answer

Decorators are functions that are used to modify the behavior of other functions.

Mid

What is PEP 8?

Show Answer

PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code.

Mid

What is Python literals?

Show Answer

Literals in Python is defined as the raw data assigned to variables or constants while programming.

Mid

What is zip() function in Python?

Show Answer

The zip() function in Python is used to create a sequence of tuples, and it takes any number of iterables as arguments.

Mid

What are descriptors?

Show Answer

Descriptors are objects that provide access to the properties of an object.

Mid

What is pickling and unpickling?

Show 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.

Mid

What is negative index in Python?

Show 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.

Mid

What is Pass in Python?

Show 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.

Senior

What are Keywords in Python?

Show Answer

Python keywords are special reserved words that have specific meanings and purposes and can't be used for anything but those specific purposes.

Senior

List the common built-in datatypes in Python?

Show 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
Senior

What is a generator in Python?

Show 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.

Senior

What are metaclasses in Python?

Show 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.

Senior

What is GIL?

Show Answer

The Global Interpreter Lock (GIL) is a synchronization mechanism in Python that prevents multiple threads from running Python code at the same time.

Senior

What are the different stages of the Life Cycle of a Thread?

Show 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.