Python is a very flexible and powerful programming language that finds applications in many areas, such as web, data and automation. Regarding the more experienced software developers and those who have worked with Python for a while, there are advanced concepts and best practices that need to be understood. Here is a complete collection of 50 advanced Python programming questions and answers to develop and test your abilities.
1. What is a Python decorator and how do you use it?
Answer: A decorator in Python is a function that modifies the behavior of another function or method. Decorators are often used for logging, access control, memoization, and other cross-cutting concerns. They are applied using the @decorator_name
syntax above the function to be decorated.
def my_decorator(func):
def wrapper():
print("Before the function call")
func()
print("After the function call")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
2. Explain the difference between @staticmethod
and @classmethod
.
Answer:
@staticmethod
: Declares a method that does not access or modify class or instance data. It behaves like a regular function but belongs to the class’s namespace.@classmethod
: Declares a method that takes the class as the first parameter (usually namedcls
). It can modify class state that applies across all instances of the class.
class MyClass:
@staticmethod
def static_method():
print("Static method called")
@classmethod
def class_method(cls):
print("Class method called")
MyClass.static_method()
MyClass.class_method()
3. What is the Global Interpreter Lock (GIL) and how does it affect Python’s multi-threading?
Answer: The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes simultaneously. This can limit the performance of CPU-bound multi-threaded programs. However, I/O-bound programs can still benefit from multi-threading.
4. How does Python manage memory?
Answer: Python manages memory using a combination of reference counting and a cyclic garbage collector. Reference counting keeps track of the number of references to an object, and when this count reaches zero, the object is deallocated. The cyclic garbage collector handles reference cycles that reference counting alone cannot clean up.
5. What are some built-in data structures in Python?
Answer: Python provides several built-in data structures:
- Lists: Ordered, mutable collections.
- Tuples: Ordered, immutable collections.
- Dictionaries: Unordered collections of key-value pairs.
- Sets: Unordered collections of unique elements.
6. Explain deepcopy
vs copy
in Python.
Answer:
copy.copy()
: Creates a shallow copy of an object, copying the structure but not the elements within nested objects.copy.deepcopy()
: Creates a deep copy of an object, recursively copying all objects found within the original object.
import copy
original = [1, [2, 3], 4]
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original[1][0] = 'changed'
print(shallow_copy) # Output: [1, ['changed', 3], 4]
print(deep_copy) # Output: [1, [2, 3], 4]
7. How can you implement a singleton pattern in Python?
Answer: A singleton pattern ensures that a class has only one instance and provides a global point of access to it. This can be implemented using the __new__
method or a decorator.
using __new__
:
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance
8. What is the difference between __str__
and __repr__
?
Answer:
__str__
: Defines a human-readable representation of an object, used bystr()
andprint()
.__repr__
: Defines an unambiguous representation of an object, ideally one that can be used to recreate the object. Used byrepr()
.
class MyClass:
def __str__(self):
return "This is the str representation"
def __repr__(self):
return "MyClass()"
obj = MyClass()
print(str(obj)) # Output: This is the str representation
print(repr(obj)) # Output: MyClass()
9. How can you handle exceptions in Python?
Answer: Exceptions in Python are handled using try
, except
, else
, and finally
blocks. The try
block contains code that might throw an exception, the except
block handles the exception, the else
block executes code if no exceptions were raised, and the finally
block executes code regardless of whether an exception was raised.
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
else:
print("Division successful")
finally:
print("This will always execute")
10. What are Python’s data types?
Answer: Python has several built-in data types, including:
- Numeric types:
int
,float
,complex
- Sequence types:
list
,tuple
,range
- Text type:
str
- Mapping type:
dict
- Set types:
set
,frozenset
- Boolean type:
bool
- Binary types:
bytes
,bytearray
,memoryview
11. What are list comprehensions and how do you use them?
Answer: List comprehensions provide a concise way to create lists. The syntax consists of an expression followed by a for
clause, then zero or more for
or if
clauses.
squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
12. How do you create a generator in Python?
Answer: Generators are created using functions and the yield
statement. They allow you to iterate over data without storing it all in memory.
def generator_example():
for i in range(5):
yield i
gen = generator_example()
print(next(gen)) # Output: 0
print(next(gen)) # Output: 1
13. What is the difference between __init__
and __new__
?
Answer:
__init__
: Initializes a newly created object and is called after__new__
.__new__
: Creates a new instance of a class and returns it. It is called before__init__
.
14. How can you merge two dictionaries in Python?
Answer: You can merge two dictionaries using the update()
method, the **
unpacking operator, or the |
operator (Python 3.9+).
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
# Using update()
dict1.update(dict2)
print(dict1) # Output: {'a': 1, 'b': 3, 'c': 4}
# Using unpacking
merged_dict = {**dict1, **dict2}
print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4}
# Using |
merged_dict = dict1 | dict2
print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4}
15. Explain the use of with
statement in Python.
Answer: The with
statement simplifies exception handling by encapsulating common preparation and cleanup tasks in context managers. It ensures that resources are properly managed.
with open('file.txt', 'r') as file:
content = file.read()
# No need to explicitly close the file; it's done automatically
16. What are Python’s built-in modules for handling dates and times?
Answer: Python provides several modules for handling dates and times:
datetime
: For manipulating dates and times.time
: For time-related functions.calendar
: For calendar-related functions.
17. How do you perform unit testing in Python?
Answer: Unit testing in Python can be performed using the unittest
module, which is part of the standard library. It provides a framework for writing and running tests.
import unittest
def add(a, b):
return a + b
class TestMathOperations(unittest.TestCase):
def test_add(self):
self.assertEqual(add(1, 2), 3)
if __name__ == '__main__':
unittest.main()
18. What is the difference between is
and ==
in Python?
Answer:
is
: Checks for object identity, i.e., whether two references point to the same object.==
: Checks for value equality, i.e., whether the values of two objects are the same.
19. How can you create a virtual environment in Python?
Answer: You can create a virtual environment using the venv
module in Python 3.
python -m venv myenv
source myenv/bin/activate # On Windows, use `myenv\Scripts\activate`
20. How do you handle multiple exceptions in a single except
block?
Answer: You can handle multiple exceptions in a single except
block by specifying a tuple of exceptions.
try:
result = 10 / 0
except (ZeroDivisionError, TypeError) as e:
print(f"An error occurred: {e}")
21. What is a metaclass in Python?
Answer: A metaclass is a class of a class that defines how a class behaves. A class is an instance of a metaclass. Metaclasses are usually used for creating APIs, enforcing coding standards, and modifying class creation.
22. How do you convert a string to a number in Python?
Answer: You can convert a string to a number using int()
for integers and float()
for floating-point numbers.
num_str = "123"
num_int = int(num_str)
num_float = float(num_str)
23. What are Python’s built-in functions for working with sequences?
Answer: Python provides several built-in functions for working with sequences, including:
len()
: Returns the length of a sequence.min()
: Returns the smallest item in a sequence.max()
: Returns the largest item in a sequence.sum()
: Returns the sum of all items in a sequence.sorted()
: Returns a sorted list of the items in a sequence.
24. How do you use the map()
function in Python?
Answer: The map()
function applies a given function to all items in an input list.
def square(x):
return x * x
numbers = [1, 2, 3, 4]
squared_numbers = list(map(square, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16]
25. What is the purpose of the zip()
function in Python?
Answer: The zip()
function combines multiple sequences (lists, tuples, etc.) into a single sequence of tuples, where the i-th tuple contains the i-th element from each of the input sequences.
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
zipped = list(zip(list1, list2))
print(zipped) # Output: [(1, 'a'), (2, 'b'), (3, 'c')]
26. How do you handle file I/O operations in Python?
Answer: File I/O operations in Python are handled using built-in functions like open()
, read()
, write()
, and close()
. The with
statement is commonly used to ensure files are properly closed after operations.
with open('example.txt', 'w') as file:
file.write("Hello, World!")
27. What is the difference between append()
and extend()
methods in lists?
Answer:
append()
: Adds a single element to the end of the list.extend()
: Adds all elements of an iterable (like a list or tuple) to the end of the list.
list1 = [1, 2, 3]
list1.append(4)
print(list1) # Output: [1, 2, 3, 4]
list2 = [1, 2, 3]
list2.extend([4, 5])
print(list2) # Output: [1, 2, 3, 4, 5]
28. How can you sort a list in Python?
Answer: You can sort a list in Python using the sort()
method for in-place sorting or the sorted()
function for returning a new sorted list.
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort()
print(numbers) # Output: [1, 1, 3, 4, 5, 9]
numbers = [3, 1, 4, 1, 5, 9]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [1, 1, 3, 4, 5, 9]
29. What are lambda functions in Python?
Answer: Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of arguments but only one expression.
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5
30. How do you handle command-line arguments in Python?
Answer: Command-line arguments in Python can be handled using the sys.argv
list or the argparse
module for more complex parsing.
using sys.argv
:
import sys
if __name__ == "__main__":
print(sys.argv)
using argparse
:
import argparse
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
31. Explain the use of defaultdict
in Python.
Answer: defaultdict
is a subclass of dict
from the collections
module. It provides a default value for the key that does not exist.
from collections import defaultdict
dd = defaultdict(int)
dd['a'] += 1
print(dd) # Output: defaultdict(<class 'int'>, {'a': 1})
32. What is the use of itertools
in Python?
Answer: The itertools
module provides a collection of tools for handling iterators, such as creating infinite iterators, combining iterators, and grouping elements.
import itertools
counter = itertools.count(start=1, step=2)
print(next(counter)) # Output: 1
print(next(counter)) # Output: 3
33. How do you create a custom exception in Python?
Answer: Custom exceptions can be created by subclassing the built-in Exception
class.
class MyCustomError(Exception):
def __init__(self, message):
self.message = message
try:
raise MyCustomError("This is a custom error")
except MyCustomError as e:
print(e.message)
34. What is the use of the enumerate()
function?
Answer: The enumerate()
function adds a counter to an iterable and returns it as an enumerate object.
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
for index, season in enumerate(seasons, start=1):
print(index, season)
# Output:
# 1 Spring
# 2 Summer
# 3 Fall
# 4 Winter
35. How do you use the filter()
function in Python?
Answer: The filter()
function constructs an iterator from elements of an iterable for which a function returns true.
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(is_even, numbers))
print(even_numbers) # Output: [2, 4, 6]
36. What is the purpose of the reduce()
function in Python?
Answer: The reduce()
function from the functools
module applies a rolling computation to sequential pairs of values in a list.
from functools import reduce
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5]
sum = reduce(add, numbers)
print(sum) # Output: 15
37. How can you read and write binary files in Python?
Answer: Binary files can be read and written using the rb
and wb
modes in the open()
function.
# Writing binary data
with open('binary_file.bin', 'wb') as file:
file.write(b'\x00\x01\x02\x03')
# Reading binary data
with open('binary_file.bin', 'rb') as file:
data = file.read()
print(data) # Output: b'\x00\x01\x02\x03'
38. What are Python’s built-in functions for working with iterables?
Answer: Some built-in functions for working with iterables include:
len()
: Returns the length of an iterable.enumerate()
: Adds a counter to an iterable.filter()
: Filters elements of an iterable based on a function.map()
: Applies a function to all items in an iterable.zip()
: Combines multiple iterables into tuples.sorted()
: Returns a sorted list from an iterable.sum()
: Returns the sum of all items in an iterable.
39. How do you handle JSON data in Python?
Answer: JSON data can be handled using the json
module in Python. It provides functions to parse JSON strings into Python objects and convert Python objects into JSON strings.
import json
# JSON string to Python object
json_str = '{"name": "John", "age": 30}'
data = json.loads(json_str)
print(data) # Output: {'name': 'John', 'age': 30}
# Python object to JSON string
person = {'name': 'Jane', 'age': 25}
json_str = json.dumps(person)
print(json_str) # Output: {"name": "Jane", "age": 25}
40. What is the purpose of the pass
statement in Python?
Answer: The pass
statement is a null operation; it is used as a placeholder in loops, functions, classes, or conditionals where code is syntactically required but you do not want to execute any code.
def empty_function():
pass
41. How do you handle exceptions with multiple except
blocks?
Answer: You can handle different exceptions with multiple except
blocks, each specifying a different exception type.
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
except TypeError:
print("Type error occurred")
42. How do you create a class property in Python?
Answer: Class properties can be created using the @property
decorator, which allows you to define methods that can be accessed like attributes.
class MyClass:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
@value.setter
def value(self, new_value):
self._value = new_value
obj = MyClass(10)
print(obj.value) # Output: 10
obj.value = 20
print(obj.value) # Output: 20
43. What are __slots__
in Python?
Answer: __slots__
is a mechanism to limit the attributes that an instance of a class can have, thereby saving memory by preventing the creation of a __dict__
for each instance.
class MyClass:
__slots__ = ['name', 'age']
def __init__(self, name, age):
self.name = name
self.age = age
obj = MyClass("John", 30)
print(obj.name) # Output: John
# obj.address = "NY" # This will raise an AttributeError
44. How do you use the Counter
class in Python?
Answer: The Counter
class from the collections
module is used for counting hashable objects.
from collections import Counter
data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counter = Counter(data)
print(counter) # Output: Counter({'apple': 3, 'banana': 2, 'orange': 1})
45. What is the purpose of the re
module in Python?
Answer: The re
module provides support for working with regular expressions, allowing you to search, match, and manipulate strings based on patterns.
Example:
import re
pattern = r'\d+'
text = 'There are 123 apples'
match = re.search(pattern, text)
if match:
print(match.group()) # Output: 123
46. How do you use the namedtuple
in Python?
Answer: namedtuple
is a factory function for creating tuple subclasses with named fields.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # Output: 10 20
47. How do you implement a stack in Python?
Answer: A stack can be implemented using a list, utilizing append()
to push elements and pop()
to remove elements.
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
print(stack) # Output: [1, 2, 3]
print(stack.pop()) # Output: 3
print(stack) # Output: [1, 2]
48. How do you implement a queue in Python?
Answer: A queue can be implemented using the deque
class from the collections
module.
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
queue.append(3)
print(queue) # Output: deque([1, 2, 3])
print(queue.popleft()) # Output: 1
print(queue) # Output: deque([2, 3])
49. What is the purpose of the functools
module in Python?
Answer: The functools
module provides higher-order functions that act on or return other functions. This includes tools for function memoization, partial function application, and more.
using lru_cache
for memoization:
from functools import lru_cache
@lru_cache(maxsize=32)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10)) # Output: 55
50. How do you use the heapq
module in Python?
Answer: The heapq
module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
import heapq
heap = []
heapq.heappush(heap, 3)
heapq.heappush(heap, 1)
heapq.heappush(heap, 2)
print(heap) # Output: [1, 3, 2]
print(heapq.heappop(heap)) # Output: 1
print(heap) # Output: [2, 3]
By understanding and mastering these advanced Python concepts, experienced developers can improve their coding efficiency and problem-solving skills.
Conclusion
As it was previously mentioned, python is a powerful and versatile programming language that has many features and capabilities. This list comprises fifty questions and answers most of will be encoutered by advanced python developers. It is also about advanced abilities, which, for example, include wrapping sequential function application, using metaclasses and decorators, organizing constructors’ and other resources’ disposal, and exception handling.
If you make it a habit to focus on coming up with new problems that can be solved using a language, you can easily remain alert on the most recent developments in the best practices. These questions serve as a good base whether you are preparing for a tech interview, involved in a large and tricky project or just seeking to understand more on the issue at hand.
Do not hesitate to come back and this list to your friends. Post any queries and additional information that you might have in the ejtion box below. Happy coding!