Python Interview Questions
1. Python Basics (150 Questions)
- What is Python, and why is it popular?
- Explain the difference between Python 2 and Python 3.
- What are the key features of Python?
- What is PEP 8, and why is it important?
- How is Python an interpreted language?
- What is the difference between a compiled and an interpreted language?
- What are Python’s built-in data types?
- Explain the difference between mutable and immutable objects in Python.
- What is the difference between a list and a tuple?
- How do you define a variable in Python?
- What is the purpose of the
pass
statement? - Explain the use of
None
in Python. - What is the difference between
=
and==
operators? - How does Python handle memory management?
- What is garbage collection in Python?
- Explain the concept of dynamic typing in Python.
- What are Python’s naming conventions for variables?
- How do you check the type of a variable in Python?
- What is the
id()
function in Python? - What is the difference between
is
and==
? - How do you convert a string to an integer in Python?
- What is the purpose of the
print()
function? - How do you use comments in Python?
- What are docstrings, and how are they used?
- What is the difference between single, double, and triple quotes in Python?
- Explain the
input()
function in Python. - What is the difference between
int
andfloat
? - How do you handle exceptions in Python?
- What is the purpose of the
try
andexcept
blocks? - What is the
finally
block used for? - What is the
else
clause in exception handling? - How do you raise an exception in Python?
- What is a custom exception, and how do you create one?
- What is the
assert
statement, and when is it used? - Explain the difference between
break
andcontinue
statements. - What is the
return
statement, and how is it used? - How do you use the
global
keyword in Python? - What is the
nonlocal
keyword, and when is it used? - Explain the difference between local and global variables.
- What are Python’s built-in functions?
- How do you use the
len()
function? - What is the purpose of the
type()
function? - Explain the
range()
function and its parameters. - How do you use the
zip()
function? - What is the
enumerate()
function, and how is it used? - How do you use the
slice()
function in Python? - What is the difference between shallow copy and deep copy?
- How do you perform a shallow copy in Python?
- How do you perform a deep copy in Python?
- What is the
copy
module in Python? - Explain the
import
statement in Python. - What is the difference between
import module
andfrom module import *
? - How do you import a specific function from a module?
- What is the
as
keyword used for in imports? - What is the
sys
module in Python? - How do you access command-line arguments in Python?
- What is the
os
module, and what are its common functions? - Explain the
dir()
function in Python. - What is the
help()
function, and how is it used? - How do you check the Python version in a program?
- What is the difference between
__name__
and__main__
? - Explain the concept of Python’s GIL (Global Interpreter Lock).
- What are Python’s reserved keywords?
- How do you check if a keyword is reserved in Python?
- What is the purpose of the
del
statement? - How do you swap two variables in Python?
- What is the difference between
and
and&
operators? - What is the difference between
or
and|
operators? - Explain the
not
operator in Python. - What are bitwise operators in Python?
- How do you use the
in
operator in Python? - What is the
isinstance()
function, and how is it used? - How do you check if an object is iterable?
- What is the difference between
list()
and[]
? - What is the
bool
data type in Python? - How do you convert a number to a string in Python?
- What is string interpolation in Python?
- Explain the
f-string
syntax in Python. - What is the difference between
%
formatting andstr.format()
? - How do you use the
join()
method for strings? - What is the
split()
method, and how is it used? - How do you reverse a string in Python?
- What is the
strip()
method, and what does it do? - How do you check if a string is alphanumeric?
- What is the difference between
upper()
andlower()
methods? - How do you capitalize a string in Python?
- What is the
replace()
method for strings? - How do you check if a string starts with a specific substring?
- How do you check if a string ends with a specific substring?
- What is the
find()
method, and how does it differ fromindex()
? - How do you count occurrences of a substring in a string?
- What is the
startswith()
method in Python? - What is the
endswith()
method in Python? - How do you check if a string is empty?
- What is the purpose of the
chr()
function? - What is the purpose of the
ord()
function? - How do you use escape characters in Python?
- What is the
r
prefix in Python strings? - What is the
u
prefix in Python strings? - What is the
b
prefix in Python strings? - How do you concatenate strings in Python?
- What is the difference between string concatenation and string formatting?
- How do you handle multiline strings in Python?
- What is the
repr()
function, and how does it differ fromstr()
? - How do you use the
format()
method for string formatting? - What is the purpose of the
encode()
anddecode()
methods? - How do you handle Unicode strings in Python?
- What is the difference between ASCII and Unicode?
- How do you check the memory usage of an object in Python?
- What is the
sys.getsizeof()
function? - How do you use the
round()
function in Python? - What is the
abs()
function, and how is it used? - How do you use the
max()
andmin()
functions? - What is the
sum()
function, and how is it used? - How do you generate random numbers in Python?
- What is the
random
module, and what are its key functions? - How do you use the
randint()
function in therandom
module? - What is the
choice()
function in therandom
module? - How do you shuffle a list using the
random
module? - What is the
seed()
function in therandom
module? - How do you use the
math
module in Python? - What is the
math.pi
constant? - How do you calculate the square root using the
math
module? - What is the
math.ceil()
function? - What is the
math.floor()
function? - How do you use the
pow()
function in Python? - What is the
divmod()
function, and how is it used? - How do you handle floating-point precision issues in Python?
- What is the
decimal
module, and when is it used? - How do you use the
fractions
module in Python? - What is the difference between
int
andlong
in Python 3? - How do you convert a string to a float in Python?
- What is the
complex
data type in Python? - How do you perform complex number arithmetic in Python?
- What is the
bin()
function, and how is it used? - What is the
hex()
function, and how is it used? - What is the
oct()
function, and how is it used? - How do you convert a number to binary in Python?
- What is the
format()
function for number formatting? - How do you use the
zfill()
method for strings? - What is the
isnumeric()
method for strings? - What is the
isdigit()
method, and how does it differ fromisnumeric()
? - How do you check if a string contains only whitespace?
- What is the
title()
method for strings? - How do you align text using string methods?
- What is the
ljust()
method for strings? - What is the
rjust()
method for strings? - What is the
center()
method for strings? - How do you use the
partition()
method for strings? - What is the
rpartition()
method for strings?
2. Control Flow (100 Questions)
- What is an
if
statement, and how is it used? - How do you write a nested
if
statement? - What is the
elif
clause, and when is it used? - How do you use the
else
clause in Python? - What is a ternary operator in Python?
- How do you write a one-line
if
statement? - What is the difference between
if
andelif
? - How do you handle multiple conditions in an
if
statement? - What is the
while
loop, and how is it used? - How do you break out of a
while
loop? - What is the
for
loop, and how is it used? - How do you iterate over a list using a
for
loop? - How do you iterate over a string using a
for
loop? - How do you iterate over a dictionary using a
for
loop? - What is the
range()
function in afor
loop? - How do you skip iterations in a
for
loop? - What is the difference between
break
andcontinue
in loops? - How do you use nested loops in Python?
- What is a loop variable in Python?
- How do you use the
else
clause with loops? - What happens when a loop’s
else
clause is executed? - How do you create an infinite loop in Python?
- How do you avoid infinite loops in Python?
- What is the
pass
statement in loops? - How do you iterate over multiple sequences simultaneously?
- What is the
zip()
function in loops? - How do you use the
enumerate()
function in loops? - How do you iterate over a list in reverse order?
- What is the
reversed()
function in Python? - How do you iterate over a range with a step value?
- What is short-circuit evaluation in Python?
- How does the
and
operator work in control flow? - How does the
or
operator work in control flow? - What is the precedence of logical operators in Python?
- How do you use the
not
operator in control flow? - What is a switch-case equivalent in Python?
- How do you implement a match-case statement in Python 3.10+?
- What are the benefits of the
match
statement overif-elif
? - How do you handle multiple conditions in a
match
statement? - What is the
_
wildcard in amatch
statement? - How do you use pattern matching with lists in a
match
statement? - What is the difference between a
for
loop and awhile
loop? - How do you optimize loops in Python?
- What is loop unrolling in Python?
- How do you use list comprehensions in place of loops?
- What is the difference between a list comprehension and a
for
loop? - How do you handle nested list comprehensions?
- What is a generator expression, and how is it used in loops?
- How do you iterate over a file line by line?
- What is the
next()
function in Python? - How do you use the
iter()
function in Python? - What is an iterator in Python?
- What is the difference between an iterator and an iterable?
- How do you create a custom iterator in Python?
- What is the
__iter__()
method in Python? - What is the
__next__()
method in Python? - How do you handle StopIteration in Python?
- What is a sentinel value in loops?
- How do you use the
while True
construct effectively? - What is the
break
statement’s effect in nested loops? - How do you exit multiple nested loops at once?
- What is the
continue
statement’s effect in nested loops? - How do you use
else
with awhile
loop? - How do you iterate over a dictionary’s keys and values?
- What is the
items()
method in dictionaries? - How do you use
break
in afor
loop withelse
? - What is the difference between
for
andwhile
in terms of performance? - How do you handle empty iterables in loops?
- What is the
map()
function, and how is it used in loops? - What is the
filter()
function, and how is it used in loops? - How do you combine
map()
andfilter()
in Python? - What is the
reduce()
function in Python? - How do you use the
functools.reduce()
function? - What is the
any()
function in Python? - What is the
all()
function in Python? - How do you use
any()
andall()
in control flow? - What is a guard clause in Python?
- How do you implement a guard clause in Python?
- What is the difference between
while
anddo-while
loops? - How do you simulate a
do-while
loop in Python? - What is the
itertools
module, and how is it used in loops? - How do you use
itertools.cycle()
in Python? - What is
itertools.chain()
and how is it used? - How do you use
itertools.islice()
in Python? - What is
itertools.count()
, and how is it used? - How do you handle large datasets in loops efficiently?
- What is lazy evaluation in Python loops?
- How do you use generators to optimize loops?
- What is the
yield
keyword in Python? - How do you create a generator function in Python?
- What is the difference between a generator and a list comprehension?
- How do you use
yield from
in Python? - What is a coroutine in Python?
- How do you use
async
andawait
in loops? - What is the
asyncio
module in Python? - How do you handle asynchronous iteration in Python?
- What is the
async for
loop in Python? - How do you use
asyncio.gather()
in loops? - What is the difference between synchronous and asynchronous loops?
- How do you optimize control flow for performance in Python?
3. Functions (150 Questions)
- What is a function in Python?
- How do you define a function in Python?
- What is the difference between a function and a method?
- How do you call a function in Python?
- What are function arguments in Python?
- What is the difference between positional and keyword arguments?
- How do you use default arguments in a function?
- What are variable-length arguments in Python?
- How do you use
*args
in a function? - How do you use
**kwargs
in a function? - What is the difference between
*args
and**kwargs
? - How do you pass a dictionary as keyword arguments to a function?
- What is a return value in Python functions?
- How do you return multiple values from a function?
- What is the
return
statement in Python? - How do you handle functions with no return value?
- What is a void function in Python?
- How do you define a function with optional arguments?
- What is a lambda function in Python?
- How do you use a lambda function in Python?
- What is the difference between a lambda function and a regular function?
- How do you use lambda functions with
map()
? - How do you use lambda functions with
filter()
? - What is a nested function in Python?
- How do you define a function inside another function?
- What is a closure in Python?
- How do you create a closure in Python?
- What is the
nonlocal
keyword in nested functions? - How do you access outer function variables in a nested function?
- What is function scope in Python?
- What is the LEGB rule in Python?
- How do you use the
global
keyword in a function? - What is function overloading, and does Python support it?
- How do you simulate function overloading in Python?
- What is function recursion in Python?
- How do you write a recursive function in Python?
- What is the base case in a recursive function?
- What are the advantages of recursion in Python?
- What are the disadvantages of recursion in Python?
- How do you optimize recursive functions in Python?
- What is tail recursion, and does Python support it?
- How do you handle stack overflow in recursive functions?
- What is memoization in Python?
- How do you implement memoization in Python?
- What is the
functools.lru_cache
decorator? - How do you use the
lru_cache
decorator for memoization? - What is a decorator in Python?
- How do you create a custom decorator in Python?
- What is the
@
symbol used for in Python decorators? - How do you chain multiple decorators in Python?
- What is a higher-order function in Python?
- How do you pass a function as an argument to another function?
- What is the
callable()
function in Python? - How do you check if an object is callable?
- What is function composition in Python?
- How do you implement function composition in Python?
- What is the
functools
module in Python? - How do you use
functools.partial()
in Python? - What is the
operator
module, and how is it used with functions? - How do you use the
itemgetter()
function from theoperator
module? - What is the
attrgetter()
function in theoperator
module? - How do you handle default mutable arguments in functions?
- What is the issue with mutable default arguments in Python?
- How do you avoid issues with mutable default arguments?
- What is a pure function in Python?
- What are the benefits of pure functions?
- How do you write a pure function in Python?
- What is a side effect in a function?
- How do you avoid side effects in functions?
- What is function annotation in Python?
- How do you add type hints to function parameters?
- What is the
typing
module in Python? - How do you use
Union
from thetyping
module? - What is the
Any
type in thetyping
module? - How do you use
Optional
in type hints? - What is the
Callable
type in thetyping
module? - How do you use type hints with return values?
- What is the
mypy
tool, and how is it used with Python? - How do you document a function using docstrings?
- What is the difference between a docstring and a comment?
- How do you access a function’s docstring?
- What is the
__doc__
attribute in Python? - How do you use the
help()
function with functions? - What is function introspection in Python?
- How do you use the
inspect
module in Python? - What is the
signature()
function in theinspect
module? - How do you get the parameters of a function using
inspect
? - What is a first-class function in Python?
- How does Python support first-class functions?
- What is a function factory in Python?
- How do you create a function factory in Python?
- What is a callback function in Python?
- How do you implement a callback function in Python?
- What is the
apply()
function equivalent in Python? - How do you handle variable scope in recursive functions?
- What is the
staticmethod
decorator in Python? - What is the
classmethod
decorator in Python? - What is the difference between
staticmethod
andclassmethod
? - How do you use the
@property
decorator in Python? - What is a getter and setter in Python?
- How do you implement a getter using
@property
? - How do you implement a setter using
@property
? - What is the
@property.setter
decorator? - What is the
@property.deleter
decorator? - How do you create a read-only property in Python?
- What is the difference between a method and a function?
- How do you use default arguments with
**kwargs
? - What is the
functools.wraps
decorator? - How do you preserve metadata in a decorator using
functools.wraps
? - What is the
__call__()
method in Python? - How do you make a class callable in Python?
- What is a method descriptor in Python?
- How do you use descriptors in Python functions?
- What is a bound method in Python?
- What is an unbound method in Python?
- How do you convert a function to a method in Python?
- What is the difference between a function and a lambda expression?
- How do you use lambda functions in sorting?
- What is the
key
parameter in sorting functions? - How do you sort a list using a lambda function?
- What is the
sorted()
function in Python? - How does
sorted()
differ fromlist.sort()
? - What is the
reverse
parameter in sorting functions? - How do you sort a dictionary by values in Python?
- How do you sort a dictionary by keys in Python?
- What is the
operator.itemgetter()
in sorting? - How do you use
operator.itemgetter()
for sorting? - What is the
operator.attrgetter()
in sorting? - How do you use
operator.attrgetter()
for sorting? - How do you handle exceptions in a function?
- What is the
try-except
block inside a function? - How do you raise an exception in a function?
- What is a generator function in Python?
- How do you create a generator function in Python?
- What is the difference between a generator and a regular function?
- How do you use
yield
in a generator function? - What is the
send()
method in generators? - How do you use the
throw()
method in generators? - What is the
close()
method in generators? - How do you handle generator termination in Python?
- What is a coroutine, and how is it different from a generator?
- How do you use
async def
in Python functions? - What is the
await
keyword in Python? - How do you handle asynchronous functions in Python?
- What is the
asyncio.run()
function? - How do you use
asyncio.create_task()
in Python? - What is the
asyncio.sleep()
function? - How do you handle multiple coroutines in Python?
- What is the
asyncio.gather()
function? - How do you cancel an asynchronous task in Python?
4. Data Structures (150 Questions)
- What are the built-in data structures in Python?
- What is a list in Python?
- How do you create a list in Python?
- What is the difference between a list and an array?
- How do you append an element to a list?
- What is the
append()
method for lists? - How do you insert an element into a list at a specific index?
- What is the
insert()
method for lists? - How do you remove an element from a list?
- What is the
remove()
method for lists? - How do you use the
pop()
method for lists? - What is the difference between
pop()
andremove()
? - How do you clear a list in Python?
- What is the
clear()
method for lists? - How do you extend a list in Python?
- What is the
extend()
method for lists? - How do you reverse a list in Python?
- What is the
reverse()
method for lists? - How do you sort a list in Python?
- What is the
sort()
method for lists? - How do you copy a list in Python?
- What is the
copy()
method for lists? - How do you check if an element exists in a list?
- What is the
in
operator for lists? - How do you find the index of an element in a list?
- What is the
index()
method for lists? - How do you count occurrences of an element in a list?
- What is the
count()
method for lists? - How do you slice a list in Python?
- What is list indexing in Python?
- How do you access elements in a nested list?
- What is a list comprehension in Python?
- How do you create a list comprehension?
- What is the difference between a list comprehension and a loop?
- How do you handle nested list comprehensions?
- What is a tuple in Python?
- How do you create a tuple in Python?
- What is the difference between a tuple and a list?
- How do you access elements in a tuple?
- What is tuple unpacking in Python?
- How do you perform tuple unpacking?
- What is the immutability of tuples in Python?
- How do you concatenate tuples in Python?
- What is tuple slicing in Python?
- How do you check if an element exists in a tuple?
- What is the
index()
method for tuples? - What is the
count()
method for tuples? - How do you convert a list to a tuple?
- How do you convert a tuple to a list?
- What is a dictionary in Python?
- How do you create a dictionary in Python?
- What is the difference between a dictionary and a list?
- How do you access values in a dictionary?
- What is the
get()
method for dictionaries? - How do you add a key-value pair to a dictionary?
- What is the
update()
method for dictionaries? - How do you remove a key-value pair from a dictionary?
- What is the
pop()
method for dictionaries? - How do you use the
popitem()
method for dictionaries? - What is the
clear()
method for dictionaries? - How do you check if a key exists in a dictionary?
- What is the
in
operator for dictionaries? - How do you iterate over a dictionary’s keys?
- How do you iterate over a dictionary’s values?
- What is the
items()
method for dictionaries? - How do you iterate over a dictionary’s key-value pairs?
- What is the
keys()
method for dictionaries? - What is the
values()
method for dictionaries? - How do you copy a dictionary in Python?
- What is the
copy()
method for dictionaries? - How do you merge two dictionaries in Python?
- What is a dictionary comprehension in Python?
- How do you create a dictionary comprehension?
- What is a set in Python?
- How do you create a set in Python?
- What is the difference between a set and a list?
- How do you add an element to a set?
- What is the
add()
method for sets? - How do you remove an element from a set?
- What is the
remove()
method for sets? - What is the
discard()
method for sets? - What is the difference between
remove()
anddiscard()
in sets? - How do you clear a set in Python?
- What is the
clear()
method for sets? - How do you perform a union of two sets?
- What is the
union()
method for sets? - How do you perform an intersection of two sets?
- What is the
intersection()
method for sets? - How do you perform a difference of two sets?
- What is the
difference()
method for sets? - How do you perform a symmetric difference of two sets?
- What is the
symmetric_difference()
method for sets? - How do you check if a set is a subset of another set?
- What is the
issubset()
method for sets? - How do you check if a set is a superset of another set?
- What is the
issuperset()
method for sets? - How do you check if two sets are disjoint?
- What is the
isdisjoint()
method for sets? - How do you copy a set in Python?
- What is the
copy()
method for sets? - What is a frozenset in Python?
- How do you create a frozenset in Python?
- What is the difference between a set and a frozenset?
- How do you convert a list to a set?
- How do you convert a set to a list?
- What is the
collections
module in Python? - What is a
namedtuple
in thecollections
module? - How do you create a
namedtuple
in Python? - What is a
deque
in thecollections
module? - How do you use a
deque
in Python? - What is the difference between a list and a
deque
? - What is a
Counter
in thecollections
module? - How do you use the
Counter
class in Python? - What is an
OrderedDict
in thecollections
module? - How do you create an
OrderedDict
in Python? - What is the difference between
OrderedDict
and a regular dictionary? - What is a
defaultdict
in thecollections
module? - How do you create a
defaultdict
in Python? - What is the difference between
defaultdict
and a regular dictionary? - What is a
ChainMap
in thecollections
module? - How do you use a
ChainMap
in Python? - What is the
heapq
module in Python? - How do you create a heap using the
heapq
module? - What is the
heappush()
function in theheapq
module? - What is the
heappop()
function in theheapq
module? - How do you find the smallest elements in a heap?
- What is the
heapify()
function in theheapq
module? - What is a priority queue, and how is it implemented in Python?
- How do you implement a priority queue using
heapq
? - What is the
array
module in Python? - How do you create an array in Python?
- What is the difference between an array and a list?
- How do you access elements in an array?
- What is the
bytearray
data type in Python? - How do you create a
bytearray
in Python? - What is the difference between
bytes
andbytearray
? - How do you convert a string to a
bytearray
? - What is a linked list, and how do you implement it in Python?
- What is a stack, and how do you implement it in Python?
- What is a queue, and how do you implement it in Python?
- What is a deque, and how does it differ from a queue?
- How do you implement a circular queue in Python?
- What is a binary tree, and how do you implement it in Python?
- What is a binary search tree (BST) in Python?
- How do you implement a BST in Python?
- What is a trie, and how do you implement it in Python?
- What is a hash table, and how is it implemented in Python?
- How do dictionaries use hash tables internally?
- What is a collision in a hash table, and how is it resolved?
- How do you handle hash collisions in Python dictionaries?
5. Object-Oriented Programming (OOP) (150 Questions)
- What is object-oriented programming in Python?
- What is a class in Python?
- How do you define a class in Python?
- What is an object in Python?
- How do you create an object from a class?
- What is the
__init__()
method in Python? - How do you use the
self
parameter in Python? - What is the difference between a class and an object?
- What is inheritance in Python?
- How do you implement inheritance in Python?
- What is single inheritance in Python?
- What is multiple inheritance in Python?
- What is multilevel inheritance in Python?
- What is hierarchical inheritance in Python?
- What is hybrid inheritance in Python?
- How do you use the
super()
function in Python? - What is method overriding in Python?
- How do you override a method in a subclass?
- What is method overloading, and does Python support it?
- How do you simulate method overloading in Python?
- What is polymorphism in Python?
- How do you implement polymorphism in Python?
- What is encapsulation in Python?
- How do you achieve encapsulation in Python?
- What is the difference between public, private, and protected members?
- How do you define private attributes in Python?
- What is name mangling in Python?
- How do you use double underscores for private attributes?
- What is a property in Python?
- How do you create a property using the
@property
decorator? - What is a getter method in Python?
- What is a setter method in Python?
- How do you create a read-only property in Python?
- What is a class method in Python?
- How do you define a class method using
@classmethod
? - What is a static method in Python?
- How do you define a static method using
@staticmethod
? - What is the difference between a class method and a static method?
- What is an abstract class in Python?
- How do you create an abstract class in Python?
- What is the
abc
module in Python? - How do you use the
ABC
class from theabc
module? - What is an abstract method in Python?
- How do you define an abstract method using
@abstractmethod
? - What is method resolution order (MRO) in Python?
- How do you check the MRO of a class?
- What is the
__mro__
attribute in Python? - How do you handle diamond problem in multiple inheritance?
- What is a mixin in Python?
- How do you create a mixin in Python?
- What is the difference between a mixin and a regular class?
- How do you use composition in Python?
- What is the difference between inheritance and composition?
- What is a class variable in Python?
- What is an instance variable in Python?
- How do you access class variables in Python?
- How do you access instance variables in Python?
- What is the difference between class and instance variables?
- How do you define a class attribute in Python?
- What is the
__dict__
attribute in Python? - How do you use
__dict__
to inspect object attributes? - What is the
__slots__
attribute in Python? - How do you use
__slots__
to optimize memory? - What are the advantages of using
__slots__
? - What are the limitations of using
__slots__
? - What is a metaclass in Python?
- How do you create a metaclass in Python?
- What is the
type
class in Python? - How do you use
type
to create a class dynamically? - What is the
__new__()
method in Python? - How does
__new__()
differ from__init__()
? - What is the
__call__()
method in Python? - How do you make a class callable in Python?
- What is operator overloading in Python?
- How do you overload operators in Python?
- What is the
__add__()
method in Python? - How do you overload the
+
operator in Python? - What is the
__str__()
method in Python? - How does
__str__()
differ from__repr__()
? - What is the
__len__()
method in Python? - How do you overload the
len()
function for a class? - What is the
__getitem__()
method in Python? - How do you make a class subscriptable in Python?
- What is the
__setitem__()
method in Python? - How do you make a class support item assignment?
- What is the
__delitem__()
method in Python? - How do you implement
__delitem__()
in a class? - What is the
__iter__()
method in Python? - How do you make a class iterable in Python?
- What is the
__next__()
method in Python? - How do you create a custom iterator class in Python?
- What is the
__eq__()
method in Python? - How do you overload the
==
operator in Python? - What is the
__lt__()
method in Python? - How do you overload comparison operators in Python?
- What is the
__hash__()
method in Python? - How do you make a class hashable in Python?
- What is the
__bool__()
method in Python? - How do you customize truth value testing in Python?
- What is the
__getattr__()
method in Python? - How do you handle attribute access dynamically in Python?
- What is the
__setattr__()
method in Python? - How do you handle attribute assignment dynamically?
- What is the
__delattr__()
method in Python? - How do you handle attribute deletion dynamically?
- What is a descriptor in Python?
- How do you create a custom descriptor in Python?
- What is the
__get__()
method in descriptors? - What is the
__set__()
method in descriptors? - What is the
__delete__()
method in descriptors? - How do you use descriptors to implement properties?
- What is a weak reference in Python?
- How do you use the
weakref
module in Python? - What is a weak reference to an object?
- How do you create a weak reference to a class instance?
- What is the
weakref.proxy()
function in Python? - How do you handle cyclic references in Python?
- What is garbage collection in the context of OOP?
- How do you use the
gc
module in Python? - What is the
__del__()
method in Python? - How do you implement a destructor in Python?
- What are the limitations of the
__del__()
method? - How do you handle memory leaks in Python classes?
- What is a class decorator in Python?
- How do you create a class decorator in Python?
- What is the difference between a class decorator and a function decorator?
- How do you use
dataclasses
in Python? - What is the
@dataclass
decorator in Python? - How do you define default values in a dataclass?
- What is the
field()
function in a dataclass? - How do you make a dataclass immutable?
- What is the
__post_init__()
method in a dataclass? - How do you use type hints in a dataclass?
- What is the difference between a dataclass and a regular class?
- How do you implement a singleton pattern in Python?
- What is the factory pattern in Python?
- How do you implement a factory pattern in Python?
- What is the abstract factory pattern in Python?
- How do you implement an abstract factory pattern in Python?
- What is the builder pattern in Python?
- How do you implement a builder pattern in Python?
- What is the observer pattern in Python?
- How do you implement an observer pattern in Python?
- What is the strategy pattern in Python?
- How do you implement a strategy pattern in Python?
- What is the decorator pattern in Python?
- How do you implement a decorator pattern in Python?
- What is the adapter pattern in Python?
- How do you implement an adapter pattern in Python?
- What is the facade pattern in Python?
6. File Handling (100 Questions)
- How do you open a file in Python?
- What is the
open()
function in Python? - What are the different modes for opening a file in Python?
- What is the difference between
r
andr+
modes in file handling? - What is the difference between
w
andw+
modes in file handling? - What is the
a
mode in file handling? - What is the
b
mode in file handling? - How do you read a file in Python?
- What is the
read()
method for files? - How do you read a file line by line in Python?
- What is the
readline()
method for files? - What is the
readlines()
method for files? - How do you write to a file in Python?
- What is the
write()
method for files? - What is the
writelines()
method for files? - How do you close a file in Python?
- What is the
close()
method for files? - Why is it important to close a file in Python?
- How do you use the
with
statement for file handling? - What are the benefits of using the
with
statement for files? - How do you handle file not found errors in Python?
- What is the
FileNotFoundError
in Python? - How do you check if a file exists in Python?
- What is the
os.path.exists()
function? - How do you check if a path is a file or directory?
- What is the
os.path.isfile()
function? - What is the
os.path.isdir()
function? - How do you get the current working directory in Python?
- What is the
os.getcwd()
function? - How do you change the current working directory in Python?
- What is the
os.chdir()
function? - How do you create a directory in Python?
- What is the
os.mkdir()
function? - How do you create nested directories in Python?
- What is the
os.makedirs()
function? - How do you remove a file in Python?
- What is the
os.remove()
function? - How do you remove a directory in Python?
- What is the
os.rmdir()
function? - How do you remove a directory with all its contents?
- What is the
shutil.rmtree()
function? - How do you rename a file in Python?
- What is the
os.rename()
function? - How do you copy a file in Python?
- What is the
shutil.copy()
function? - How do you move a file in Python?
- What is the
shutil.move()
function? - How do you get the size of a file in Python?
- What is the
os.path.getsize()
function? - How do you get the last modified time of a file?
- What is the
os.path.getmtime()
function? - How do you read a binary file in Python?
- How do you write to a binary file in Python?
- What is the
pickle
module in Python? - How do you serialize an object using
pickle
? - What is the
pickle.dump()
function? - How do you deserialize an object using
pickle
? - What is the
pickle.load()
function? - What are the limitations of the
pickle
module? - How do you handle large files in Python?
- What is the
io
module in Python? - How do you use the
io.StringIO
class? - What is the
io.BytesIO
class? - How do you read a CSV file in Python?
- What is the
csv
module in Python? - How do you use the
csv.reader()
function? - How do you write to a CSV file in Python?
- What is the
csv.writer()
function? - How do you read a JSON file in Python?
- What is the
json
module in Python? - How do you use the
json.load()
function? - How do you write to a JSON file in Python?
- What is the
json.dump()
function? - How do you parse a JSON string in Python?
- What is the
json.loads()
function? - How do you convert a Python object to a JSON string?
- What is the
json.dumps()
function? - How do you handle malformed JSON data?
- What is the
yaml
module in Python? - How do you read a YAML file in Python?
- How do you write to a YAML file in Python?
- What is the difference between JSON and YAML?
- How do you read an Excel file in Python?
- What is the
pandas
library for file handling? - How do you use
pandas.read_excel()
? - How do you write to an Excel file using
pandas
? - What is the
openpyxl
library in Python? - How do you read an Excel file using
openpyxl
? - How do you write to an Excel file using
openpyxl
? - What is the
xlrd
library in Python? - How do you handle file permissions in Python?
- What is the
os.access()
function? - How do you change file permissions in Python?
- What is the
os.chmod()
function? - How do you handle file encoding in Python?
- What is the
encoding
parameter in theopen()
function? - How do you read a file with a specific encoding?
- What is the
utf-8
encoding in Python? - How do you handle Unicode errors in file handling?
- What is the
io
module’s role in file handling?
7. Modules and Packages (100 Questions)
- What is a module in Python?
- How do you create a module in Python?
- How do you import a module in Python?
- What is the difference between
import
andfrom ... import
? - How do you import a specific function from a module?
- What is the
as
keyword in imports? - How do you import all functions from a module?
- What are the dangers of using
from module import *
? - What is a package in Python?
- How do you create a package in Python?
- What is the
__init__.py
file in a package? - How do you import a module from a package?
- What is the difference between a module and a package?
- How do you use relative imports in Python?
- What is the
.
notation in relative imports? - What is the
..
notation in relative imports? - How do you handle import errors in Python?
- What is the
ImportError
exception in Python? - How do you resolve module not found errors?
- What is the
sys.path
variable in Python? - How do you add a directory to
sys.path
? - What is the
PYTHONPATH
environment variable? - How do you set the
PYTHONPATH
environment variable? - What is the
site
module in Python? - How do you install a third-party module in Python?
- What is
pip
in Python? - How do you use
pip install
to install a module? - What is the
requirements.txt
file in Python? - How do you create a
requirements.txt
file? - How do you install dependencies from
requirements.txt
? - What is
venv
in Python? - How do you create a virtual environment in Python?
- How do you activate a virtual environment in Python?
- What is the difference between
venv
andvirtualenv
? - How do you deactivate a virtual environment?
- What is the
site-packages
directory in a virtual environment? - How do you upgrade a package using
pip
? - What is the
pip freeze
command? - How do you uninstall a package using
pip
? - What is the
pip list
command? - How do you check the version of an installed package?
- What is the
importlib
module in Python? - How do you dynamically import a module in Python?
- What is the
importlib.import_module()
function? - How do you reload a module in Python?
- What is the
importlib.reload()
function? - How do you handle cyclic imports in Python?
- What is a namespace package in Python?
- How do you create a namespace package?
- What is the difference between a regular package and a namespace package?
- How do you distribute a Python package?
- What is
setup.py
in a Python package? - How do you create a
setup.py
file? - What is the
setuptools
module in Python? - How do you use
setuptools
to package a module? - What is the
wheel
format in Python? - How do you create a wheel file for a package?
- What is the
twine
tool in Python? - How do you upload a package to PyPI using
twine
? - What is the
PyPI
repository? - How do you install a package from a local wheel file?
- What is the
distutils
module in Python? - How does
distutils
differ fromsetuptools
? - What is a Python egg, and how is it used?
- How do you create a Python egg?
- What is the
pipenv
tool in Python? - How do you use
Pipfile
inpipenv
? - What is the
Pipfile.lock
file? - How do you manage dependencies with
pipenv
? - What is the
poetry
tool in Python? - How do you use
poetry
to manage dependencies? - What is the
pyproject.toml
file? - How do you create a
pyproject.toml
file? - What is the
flit
tool in Python? - How do you use
flit
to package a module? - What is the difference between
pip
,pipenv
, andpoetry
? - How do you handle package version conflicts?
- What is dependency resolution in Python?
- How do you check for outdated packages using
pip
? - What is the
pipdeptree
tool in Python? - How do you visualize package dependencies?
- What is the
pkg_resources
module in Python? - How do you access package metadata using
pkg_resources
? - What is the
__version__
attribute in a module? - How do you specify a package version in
requirements.txt
? - What is the
extras_require
field insetup.py
? - How do you install optional dependencies for a package?
- What is the
entry_points
field insetup.py
? - How do you create a console script using
entry_points
? - What is the
console_scripts
entry point in Python? - How do you test a Python package?
- What is the
pytest
framework for testing modules? - How do you write tests for a Python module?
- What is the
unittest
module in Python? - How do you use
unittest
to test a module? - What is the
doctest
module in Python? - How do you write doctests for a module?
- What is the difference between
unittest
andpytest
? - How do you mock dependencies in a Python module?
- What is the
unittest.mock
module in Python?
8. Exception Handling (100 Questions)
- What is an exception in Python?
- How do you handle exceptions in Python?
- What is the
try-except
block in Python? - How do you catch a specific exception in Python?
- What is the
except
clause in Python? - How do you catch multiple exceptions in a single
except
block? - What is the
Exception
class in Python? - How do you catch all exceptions in Python?
- What is the danger of catching all exceptions?
- How do you use the
else
clause in exception handling? - What is the
finally
clause in exception handling? - How do you raise an exception in Python?
- What is the
raise
statement in Python? - How do you re-raise an exception in Python?
- What is the
assert
statement in Python? - How do you use assertions for debugging?
- What is the difference between
assert
andraise
? - How do you create a custom exception in Python?
- What is the
BaseException
class in Python? - How do you inherit from the
Exception
class? - What is the
ValueError
exception in Python? - What is the
TypeError
exception in Python? - What is the
IndexError
exception in Python? - What is the
KeyError
exception in Python? - What is the
FileNotFoundError
exception in Python? - What is the
ZeroDivisionError
exception in Python? - What is the
AttributeError
exception in Python? - What is the
ImportError
exception in Python? - What is the
ModuleNotFoundError
exception in Python? - What is the
NameError
exception in Python? - What is the
SyntaxError
exception in Python? - What is the
IndentationError
exception in Python? - What is the
TabError
exception in Python? - What is the
OverflowError
exception in Python? - What is the
MemoryError
exception in Python? - What is the
RuntimeError
exception in Python? - What is the
NotImplementedError
exception in Python? - How do you handle
NotImplementedError
in Python? - What is the
StopIteration
exception in Python? - How do you handle
StopIteration
in iterators? - What is the
KeyboardInterrupt
exception in Python? - How do you handle
KeyboardInterrupt
in Python? - What is the
SystemExit
exception in Python? - How do you handle
SystemExit
in Python? - What is the
GeneratorExit
exception in Python? - How do you handle
GeneratorExit
in Python? - What is the
OSError
exception in Python? - How do you handle
OSError
in file operations? - What is the
IOError
exception in Python? - How do you handle
IOError
in Python? - What is the
PermissionError
exception in Python? - How do you handle
PermissionError
in Python? - What is the
FileExistsError
exception in Python? - How do you handle
FileExistsError
in Python? - What is the
IsADirectoryError
exception in Python? - How do you handle
IsADirectoryError
in Python? - What is the
TimeoutError
exception in Python? - How do you handle
TimeoutError
in Python? - What is the
ConnectionError
exception in Python? - How do you handle
ConnectionError
in Python? - How do you log exceptions in Python?
- What is the
logging
module in Python? - How do you use
logging.exception()
to log exceptions? - How do you create a custom exception hierarchy?
- What is the
try-except-else-finally
structure in Python? - How do you handle nested
try-except
blocks? - What is the
traceback
module in Python? - How do you use
traceback.print_exc()
in Python? - How do you get the full traceback of an exception?
- What is the
sys.exc_info()
function in Python? - How do you use
sys.exc_info()
to handle exceptions? - What is the
contextlib
module in Python? - How do you use
contextlib.suppress()
to handle exceptions? - What is the
with
statement’s role in exception handling? - How do you create a custom context manager for exception handling?
- What is the
__enter__()
method in a context manager? - What is the
__exit__()
method in a context manager? - How do you handle exceptions in a context manager?
- What is the
contextlib.contextmanager
decorator? - How do you create a context manager using
@contextmanager
? - How do you handle exceptions in asynchronous code?
- What is the
try-except
block in asynchronous functions? - How do you handle
asyncio.TimeoutError
in Python? - What is the
asyncio.CancelledError
exception? - How do you handle
asyncio.CancelledError
in Python? - How do you debug exceptions in Python?
- What is the
pdb
module in Python? - How do you use
pdb.set_trace()
to debug exceptions? - What is the
ipdb
module in Python? - How do you use
ipdb
for debugging exceptions? - What is the difference between
pdb
andipdb
? - How do you use
try-except
with loops? - How do you handle exceptions in recursive functions?
- What is exception chaining in Python?
- How do you use the
from
keyword in exception raising? - What is the
__cause__
attribute in exceptions? - What is the
__context__
attribute in exceptions? - How do you suppress exception context in Python?
- What is the
raise ... from None
syntax in Python? - How do you test exception handling in Python?
9. Functional Programming (100 Questions)
- What is functional programming in Python?
- How does Python support functional programming?
- What is a pure function in functional programming?
- How do you write a pure function in Python?
- What is immutability in functional programming?
- How do you achieve immutability in Python?
- What is a first-class function in Python?
- How does Python support first-class functions?
- What is a higher-order function in Python?
- How do you create a higher-order function in Python?
- What is the
map()
function in Python? - How do you use
map()
in functional programming? - What is the
filter()
function in Python? - How do you use
filter()
in functional programming? - What is the
reduce()
function in Python? - How do you use
functools.reduce()
in Python? - What is a lambda function in functional programming?
- How do you use lambda functions in functional programming?
- What is function composition in functional programming?
- How do you implement function composition in Python?
- What is the
operator
module in functional programming? - How do you use
operator.add()
in Python? - What is the
itemgetter()
function in functional programming? - How do you use
itemgetter()
in sorting? - What is the
attrgetter()
function in functional programming? - How do you use
attrgetter()
in sorting? - What is currying in functional programming?
- How do you implement currying in Python?
- What is partial application in Python?
- How do you use
functools.partial()
for partial application? - What is a closure in functional programming?
- How do you create a closure in Python?
- What is memoization in functional programming?
- How do you implement memoization in Python?
- What is the
functools.lru_cache
decorator? - How do you use
lru_cache
for memoization? - What is lazy evaluation in Python?
- How do you achieve lazy evaluation in Python?
- What is a generator in functional programming?
- How do you create a generator function in Python?
- What is the
yield
keyword in functional programming? - How do you use
yield from
in generators? - What is a generator expression in Python?
- How do you create a generator expression?
- What is the difference between a generator and a list comprehension?
- How do you use generators for lazy evaluation?
- What is the
itertools
module in functional programming? - How do you use
itertools.chain()
in Python? - What is the
itertools.cycle()
function? - How do you use
itertools.cycle()
in Python? - What is the
itertools.islice()
function? - How do you use
itertools.islice()
in Python? - What is the
itertools.count()
function? - How do you use
itertools.count()
in Python? - What is the
itertools.combinations()
function? - How do you use
itertools.combinations()
in Python? - What is the
itertools.permutations()
function? - How do you use
itertools.permutations()
in Python? - What is the
itertools.product()
function? - How do you use
itertools.product()
in Python? - What is a monad in functional programming?
- How do you implement a monad in Python?
- What is the
Maybe
monad in Python? - How do you implement the
Maybe
monad in Python? - What is the
List
monad in Python? - How do you implement the
List
monad in Python? - What is a functor in functional programming?
- How do you implement a functor in Python?
- What is the
map()
function’s role in functors? - What is immutability’s role in functional programming?
- How do you handle side effects in functional programming?
- What is referential transparency in Python?
- How do you achieve referential transparency in Python?
- What is a pipeline in functional programming?
- How do you create a pipeline in Python?
- What is the
toolz
library in Python? - How do you use
toolz
for functional programming? - What is the
fn
library in Python? - How do you use
fn
for functional programming? - What is the
pyfunctional
library in Python? - How do you use
pyfunctional
for functional programming? - What is the difference between imperative and functional programming?
- How do you combine functional and object-oriented programming in Python?
- What is the
operator
module’s role in functional programming? - How do you use
operator.mul()
in Python? - What is the
compose()
function in functional programming? - How do you implement
compose()
in Python? - What is the
pipe()
function in functional programming? - How do you implement
pipe()
in Python? - How do you handle recursion in functional programming?
- What is tail recursion in functional programming?
- How do you simulate tail recursion in Python?
- What is the
functools
module’s role in functional programming? - How do you use
functools.reduce()
in functional programming? - What is the
itertools.accumulate()
function? - How do you use
itertools.accumulate()
in Python? - How do you handle errors in functional programming?
- What is the
Result
type in functional programming? - How do you implement the
Result
type in Python? - How do you test functional code in Python?
10. Algorithms and Data Structures (150 Questions)
- What is an algorithm in Python?
- How do you implement a binary search algorithm in Python?
- What is the time complexity of binary search?
- How do you implement a linear search algorithm in Python?
- What is the time complexity of linear search?
- How do you implement a bubble sort algorithm in Python?
- What is the time complexity of bubble sort?
- How do you implement a selection sort algorithm in Python?
- What is the time complexity of selection sort?
- How do you implement an insertion sort algorithm in Python?
- What is the time complexity of insertion sort?
- How do you implement a merge sort algorithm in Python?
- What is the time complexity of merge sort?
- How do you implement a quicksort algorithm in Python?
- What is the time complexity of quicksort?
- How do you implement a heap sort algorithm in Python?
- What is the time complexity of heap sort?
- How do you implement a counting sort algorithm in Python?
- What is the time complexity of counting sort?
- How do you implement a radix sort algorithm in Python?
- What is the time complexity of radix sort?
- How do you implement a bucket sort algorithm in Python?
- What is the time complexity of bucket sort?
- What is a sorting algorithm’s stability?
- Which sorting algorithms are stable in Python?
- How do you implement a depth-first search (DFS) in Python?
- What is the time complexity of DFS?
- How do you implement a breadth-first search (BFS) in Python?
- What is the time complexity of BFS?
- How do you implement Dijkstra’s algorithm in Python?
- What is the time complexity of Dijkstra’s algorithm?
- How do you implement a Bellman-Ford algorithm in Python?
- What is the time complexity of Bellman-Ford?
- How do you implement a Floyd-Warshall algorithm in Python?
- What is the time complexity of Floyd-Warshall?
- How do you implement a Kruskal’s algorithm in Python?
- What is the time complexity of Kruskal’s algorithm?
- How do you implement a Prim’s algorithm in Python?
- What is the time complexity of Prim’s algorithm?
- How do you implement a topological sort in Python?
- What is the time complexity of topological sort?
- How do you implement a binary search tree (BST) in Python?
- What is the time complexity of BST operations?
- How do you implement an AVL tree in Python?
- What is the time complexity of AVL tree operations?
- How do you implement a red-black tree in Python?
- What is the time complexity of red-black tree operations?
- How do you implement a trie in Python?
- What is the time complexity of trie operations?
- How do you implement a hash table in Python?
- What is the time complexity of hash table operations?
- How do you handle collisions in a hash table?
- What is open addressing in hash tables?
- What is separate chaining in hash tables?
- How do you implement a stack in Python?
- What is the time complexity of stack operations?
- How do you implement a queue in Python?
- What is the time complexity of queue operations?
- How do you implement a deque in Python?
- What is the time complexity of deque operations?
- How do you implement a priority queue in Python?
- What is the time complexity of priority queue operations?
- How do you implement a linked list in Python?
- What is the time complexity of linked list operations?
- How do you implement a doubly linked list in Python?
- What is the time complexity of doubly linked list operations?
- How do you implement a circular linked list in Python?
- What is the time complexity of circular linked list operations?
- How do you reverse a linked list in Python?
- What is the time complexity of reversing a linked list?
- How do you detect a cycle in a linked list?
- What is Floyd’s cycle detection algorithm?
- How do you implement Floyd’s cycle detection in Python?
- How do you find the middle of a linked list?
- How do you merge two sorted linked lists?
- What is the time complexity of merging two linked lists?
- How do you implement a binary heap in Python?
- What is the time complexity of binary heap operations?
- How do you implement a min-heap in Python?
- How do you implement a max-heap in Python?
- How do you implement a Fibonacci heap in Python?
- What is the time complexity of Fibonacci heap operations?
- How do you implement a disjoint set (union-find) in Python?
- What is the time complexity of union-find operations?
- How do you implement a graph in Python?
- What is an adjacency list in a graph?
- What is an adjacency matrix in a graph?
- How do you implement a weighted graph in Python?
- How do you implement a directed graph in Python?
- How do you implement an undirected graph in Python?
- How do you find the shortest path in a graph?
- How do you detect a cycle in a graph?
- How do you implement a minimum spanning tree in Python?
- What is the difference between DFS and BFS?
- How do you implement a dynamic programming solution in Python?
- What is the knapsack problem, and how do you solve it in Python?
- What is the longest common subsequence problem?
- How do you solve the longest common subsequence in Python?
- What is the edit distance problem in Python?
- How do you solve the edit distance problem in Python?
- What is the coin change problem in Python?
- How do you solve the coin change problem in Python?
- What is the matrix chain multiplication problem?
- How do you solve matrix chain multiplication in Python?
- What is the longest increasing subsequence problem?
- How do you solve the longest increasing subsequence in Python?
- What is the subset sum problem in Python?
- How do you solve the subset sum problem in Python?
- What is a greedy algorithm in Python?
- How do you implement a greedy algorithm in Python?
- What is Huffman coding, and how do you implement it in Python?
- What is the activity selection problem?
- How do you solve the activity selection problem in Python?
- What is the fractional knapsack problem?
- How do you solve the fractional knapsack problem in Python?
- What is a backtracking algorithm in Python?
- How do you implement a backtracking algorithm in Python?
- What is the N-Queens problem in Python?
- How do you solve the N-Queens problem in Python?
- What is the Sudoku solver problem?
- How do you implement a Sudoku solver in Python?
- What is the graph coloring problem?
- How do you solve the graph coloring problem in Python?
- What is the traveling salesman problem (TSP)?
- How do you solve the TSP in Python?
- What is a divide-and-conquer algorithm in Python?
- How do you implement a divide-and-conquer algorithm in Python?
- What is the closest pair of points problem?
- How do you solve the closest pair of points in Python?
- What is the convex hull problem?
- How do you solve the convex hull problem in Python?
- What is the line sweep algorithm in Python?
- How do you implement a line sweep algorithm in Python?
- What is the KMP algorithm in Python?
- How do you implement the KMP algorithm in Python?
- What is the Rabin-Karp algorithm in Python?
- How do you implement the Rabin-Karp algorithm in Python?
- What is the Boyer-Moore algorithm in Python?
- How do you implement the Boyer-Moore algorithm in Python?
- What is the A* algorithm in Python?
- How do you implement the A* algorithm in Python?
- What is the time complexity of the A* algorithm?
- What is a heuristic function in the A* algorithm?
- How do you design a heuristic function for A*?
- What is the difference between A* and Dijkstra’s algorithm?
- How do you implement a trie for string searching?
- What is the time complexity of trie operations?
- How do you implement a suffix tree