Top 25 Python Interview Questions
Top 25 Python Interview Questions
Top 25 Python Interview Questions
ADVANCE LEVEL
Python
INTERVIEW QUESTIONS
While the GIL makes Python threads less efficient for CPU-bound
tasks, it is not a problem for I/O-bound tasks, as threads can yield
the GIL during I/O operations. To achieve parallelism in CPU-
bound tasks, you can use the multiprocessing module, which
creates separate processes, each with its own Python interpreter
and memory space.
import threading
def count_up():
for _ in range(1000000):
pass
def count_down():
for _ in range(1000000):
pass
thread1 = threading.Thread(target=count_up)
thread2 = threading.Thread(target=count_down)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Shallow Copy: A shallow copy creates a new object but does not
create copies of nested objects within the original object. Instead,
it references the same nested objects. You can create a shallow
copy using the copy module's copy() function or by slicing.
import copy
shallow_copied_list = copy.copy(original_list)
import copy
deep_copied_list = copy.deepcopy(original_list)
import time
def timing_decorator(func):
start_time = time.time()
end_time = time.time()
seconds to execute.")
return result
return wrapper
@timing_decorator
def some_function():
time.sleep(2)
some_function()
From To
Placed with
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Usage:
fib_gen = fibonacci_generator()
for _ in range(10):
print(next(fib_gen))
class MyClass:
self.x = x
class MyClass:
def __del__(self):
obj = MyClass()
Example of a closure:
def outer_function(x):
def inner_function(y):
return x + y
return inner_function
closure = outer_function(10)
Tutort Benefits
class Parent:
self.x = x
class Child(Parent):
super().__init__(x)
self.y = y
print(child_obj.x) # Output: 10
print(child_obj.y) # Output: 20
From To
Placed with
Code- class A:
def method(self):
print("A method")
class B(A):
def method(self):
print("B method")
class C(A):
def method(self):
print("C method")
pass
d = D()
The async and await keywords are used to define and work with
asynchronous code in Python, particularly in asynchronous
functions (coroutines). async marks a function as asynchronous,
and await is used to pause the execution of an asynchronous
function until an asynchronous operation completes.
import asyncio
asyncio.to_thread(requests.get, url),
asyncio.to_thread(requests.get, url)
return response
# Usage:
result = asyncio.run(fetch_data("https://tutort.com"))
import multiprocessing
def worker_function(x):
return x * x
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
pool.close()
pool.join()
While the GIL makes Python threads less efficient for CPU-bound
tasks, it is not a problem for I/O-bound tasks, as threads can yield
the GIL during I/O operations. To achieve parallelism in CPU-
bound tasks, you can use the multiprocessing module, which
creates separate processes, each with its own Python interpreter
and memory space.
import threading
def count_up():
for _ in range(1000000):
pass
def count_down():
for _ in range(1000000):
pass
thread1 = threading.Thread(target=count_up)
thread2 = threading.Thread(target=count_down)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Shallow Copy: A shallow copy creates a new object but does not
create copies of nested objects within the original object. Instead,
it references the same nested objects. You can create a shallow
copy using the copy module's copy() function or by slicing.
import copy
shallow_copied_list = copy.copy(original_list)
import copy
deep_copied_list = copy.deepcopy(original_list)
import time
def timing_decorator(func):
start_time = time.time()
end_time = time.time()
seconds to execute.")
return result
return wrapper
@timing_decorator
def some_function():
time.sleep(2)
some_function()
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Usage:
fib_gen = fibonacci_generator()
for _ in range(10):
print(next(fib_gen))
From To
Placed with
class MathUtility:
@staticmethod
return x + y
result = MathUtility.add(5, 3)
@classmethod
class MyClass:
def get_instance_count(cls):
count = 0
return cls.count
obj1 = MyClass(10)
self.value = value
obj2 = MyClass(20)
MyClass.count += 1
count = MyClass.get_instance_count() # count is 2
class Circle:
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
if value < 0:
raise ValueError("Radius
cannot be negative")
self._radius = value
@property
def area(self):
circle = Circle(5)
class MyClass:
def __str__(self):
class MyClass:
self.x = x
def __repr__(self):
return f"MyClass({self.x})"
class MyContextManager:
def __enter__(self):
# Usage:
class MyClass:
def __str__(self):
class MyClass:
self.x = x
def __repr__(self):
return f"MyClass({self.x})"
class MyContextManager:
def __enter__(self):
# Usage:
Guaranteed
Hiring
Highest
# Original class
class Calculator:
return x + y
return x * y
Calculator.multiply = multiply
# Usage
calc = Calculator()
class MyMeta(type):
pass
class MyClass(metaclass=MyMeta):
self.x = x
Service Based
Sayan
Banerjee
Company
import multiprocessing
def worker_function(x):
return x * x
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
pool.close()
pool.join()
variable_name = "x"
variable_value = 42
exec(dynamic_code)
print(x) # Output: 42
Explore More
www.tutort.net
Follow us on