Python Interview Questions with with Answers
Certainly! Here are 100
Python interview questions and answers in brief:
Basics
- What is Python?
- Python is an interpreted high-level programming language known for its readability and versatility.
- How is Python interpreted?
- Line by line, Python code is executed, and the interpreter converts it into intermediate code, which is then executed by the Python Virtual Machine (PVM).
- Explain the difference between Python 2 and Python 3.
- Python 3 is the most recent version, with improvements and changes that are incompatible with Python 2.
- What are the key features of Python?
- The key characteristics are readability, simplicity, and versatility. It supports the paradigms of object-oriented, imperative, and functional programming.
- What is PEP 8?
- PEP 8 is the Python Enhancement Proposal that provides style guide recommendations for writing code in Python.
- How do you comment in Python?
- Single-line comments start with
#
, and multi-line comments are enclosed in triple quotes ('''
or"""
).
- Explain Python’s GIL (Global Interpreter Lock).
- GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecode simultaneously.
- What is a docstring?
- A docstring is a string literal used for documenting Python modules, classes, functions, or methods.
- What is the purpose of the
pass
statement in Python?
pass
is a no-operation statement used as a placeholder where syntactically some code is required but no action is desired.
- What is the purpose of the
__init__
method in Python?__init__
is a special method used for initializing object attributes when an instance of the class is created.
Data Types
- Explain mutable and immutable types in Python.
- Mutable types can be modified after creation (e.g., lists), while immutable types cannot (e.g., tuples).
- What is the difference between
list
andtuple
in Python?- Lists are mutable, and tuples are immutable. Lists use square brackets (
[]
), while tuples use parentheses (()
).
- Lists are mutable, and tuples are immutable. Lists use square brackets (
- Explain the difference between
==
andis
in Python.==
compares values, andis
compares object identity (whether they reference the same object in memory).
- How do you convert a string to an integer in Python?
- Use the
int()
function, e.g.,int("42")
.
- Use the
- What is the purpose of the
ord()
andchr()
functions in Python?ord()
returns the Unicode code point of a character, andchr()
returns a character from a Unicode code point.
- Explain the concept of truthy and falsy values in Python.
- Values that evaluate to
True
in a Boolean context are truthy, and those evaluating toFalse
are falsy.
- Values that evaluate to
- How do you swap the values of two variables in Python without using a temporary variable?
- Use tuple unpacking, e.g.,
a, b = b, a
.
- Use tuple unpacking, e.g.,
- What is the purpose of the
None
keyword in Python?None
is a special constant representing the absence of a value or a null value.
- How do you concatenate two lists in Python?
- Use the
+
operator or theextend()
method.
- Use the
- Explain the purpose of the
*args
and**kwargs
in function definitions.*args
allows a function to accept any number of positional arguments, and**kwargs
allows any number of keyword arguments.
Control Flow
- Explain the purpose of
if __name__ == "__main__":
in Python scripts.- It allows a script to be both importable as a module and executable as a standalone program.
- How do you iterate over a dictionary in Python?
- Use
for key, value in my_dict.items()
.
- Use
- What is the purpose of the
break
andcontinue
statements in Python?break
terminates the loop, andcontinue
skips the rest of the code inside the loop for the current iteration.
- Explain the use of
try
,except
,else
, andfinally
in Python exception handling.try
contains the code that might raise an exception,except
handles the exception,else
executes if no exception occurs, andfinally
executes regardless of an exception.
- How do you use a list comprehension in Python?
[expression for item in iterable if condition]
.
- What is the purpose of the
pass
statement in a loop?- It is a no-operation statement, often used as a placeholder where syntactically some code is required.
- Explain the concept of a generator in Python.
- A generator is a special type of iterator that yields values lazily using the
yield
keyword.
- A generator is a special type of iterator that yields values lazily using the
- How can you create an infinite loop in Python?
- Use
while True:
.
- Use
- What is the purpose of the
range()
function in Python?- It generates a sequence of numbers and is often used for iterating over a sequence.
- Explain the use of the
enumerate()
function in Python.- It adds a counter to an iterable, returning tuples with the counter and the iterable’s elements.
Functions
- What is a lambda function in Python?
- A lambda function is an anonymous function defined using the
lambda
keyword.
- A lambda function is an anonymous function defined using the
- What is the difference between
return
andyield
in a function?return
ends the function and returns a value, whileyield
pauses the function, allowing it to be resumed later.
- Explain the concept of closures in Python.
- Closures allow a nested function to capture and remember the values of the variables in the outer (enclosing) function’s scope.
- How do you define a default value for a function parameter in Python?
- Define the parameter with a default value, e.g.,
def my_function(param=value):
.
- Define the parameter with a default value, e.g.,
- What is the purpose of the
**kwargs
parameter in a function?- It allows a function to accept any number of keyword arguments.
- How can you call a function with a variable number of arguments?
- Use
*args
to pass a variable number of positional arguments.
- Use
- Explain the concept of recursion in Python.
- Recursion is when a function calls itself. It is essential to have a base case to prevent infinite recursion.
- What is the purpose of the
__init__
method in a class?- It initializes the attributes of an object when an instance is created.
- What is the purpose of the
staticmethod
andclassmethod
decorators in Python?staticmethod
defines a static method, andclassmethod
defines a method that belongs