30 Python Interview & Practice Questions (Easy to Tough)
Q1 (Easy - Basics): What are Python's key features?
Explanation: Python is interpreted, dynamically typed, high-level, and supports multiple paradigms like OOP
and functional programming.
Q2 (Easy - Variables): What is the difference between a variable and a constant in Python?
Explanation: Python doesn't have constants by default, but variables are used to store data that can change.
Q3 (Easy - Data Types): What are Python's built-in data types?
Explanation: int, float, str, list, tuple, dict, set, bool, NoneType are some built-in data types.
Q4 (Easy - Strings): How do you reverse a string in Python?
s = 'hello'
print(s[::-1])
Explanation: Slicing with a step of -1 reverses a string.
Q5 (Easy - Lists): How do you append an item to a list?
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
Explanation: Use append() to add an item at the end of the list.
Q6 (Easy - Conditionals): How does if-elif-else work in Python?
x = 5
if x > 0:
print('Positive')
elif x < 0:
print('Negative')
else:
print('Zero')
Explanation: It checks conditions in order; the first true condition block is executed.
Q7 (Easy - Functions): What is a function in Python and how do you define one?
def greet(name):
return f'Hello, {name}'
print(greet('Alice'))
Explanation: Functions help organize reusable blocks of code using def keyword.
Q8 (Moderate - Loops): What is the difference between a for loop and a while loop?
Explanation: for loops iterate over sequences; while loops run until a condition is false.
Q9 (Moderate - Lists): How do list comprehensions work in Python?
squares = [x**2 for x in range(5)]
print(squares)
Explanation: List comprehensions provide a concise way to create lists.
Q10 (Moderate - Dictionaries): How do you loop through a dictionary?
my_dict = {'a': 1, 'b': 2}
for key, value in my_dict.items():
print(key, value)
Explanation: Use .items() to get key-value pairs.
Q11 (Moderate - OOP): What is a class and how do you instantiate it?
class Car:
def __init__(self, brand):
self.brand = brand
c = Car('Toyota')
print(c.brand)
Explanation: A class defines a blueprint; instantiate using class name followed by parentheses.
Q12 (Moderate - OOP): What is inheritance in Python?
class Animal:
def speak(self): return 'sound'
class Dog(Animal): pass
d = Dog()
print(d.speak())
Explanation: Inheritance lets one class inherit attributes and methods from another.
Q13 (Moderate - Exceptions): How does exception handling work in Python?
try:
x = 1 / 0
except ZeroDivisionError:
print('Cannot divide by zero')
Explanation: Use try-except blocks to handle exceptions and avoid crashing.
Q14 (Moderate - Files): How do you read a file line by line in Python?
with open('file.txt') as f:
for line in f:
print(line)
Explanation: Using with automatically closes the file after use.
Q15 (Tough - Decorators): What are decorators in Python?
def decorator(func):
def wrapper():
print('Before')
func()
print('After')
return wrapper
@decorator
def greet():
print('Hello')
greet()
Explanation: Decorators modify the behavior of functions without changing their code.
Q16 (Tough - Generators): What is a generator function?
def gen():
yield 1
y = gen()
print(next(y))
Explanation: Generators use yield to produce values one at a time.
Q17 (Tough - Comprehensions): What is a dictionary comprehension?
squares = {x: x**2 for x in range(5)}
print(squares)
Explanation: It's a concise way to create dictionaries.
Q18 (Tough - OOP): What is multiple inheritance?
class A: pass
class B: pass
class C(A, B): pass
Explanation: Multiple inheritance allows a class to inherit from multiple classes.
Q19 (Tough - Multithreading): How do you implement multithreading in Python?
import threading
def print_num():
print('Number')
t = threading.Thread(target=print_num)
t.start()
Explanation: Threading allows multiple operations to run concurrently.
Q20 (Tough - Interview): What are Python's memory management features?
Explanation: Python uses reference counting and garbage collection for memory management.
Q21 (Tough - Interview): What are Python's limitations?
Explanation: Includes speed limitations due to GIL, high memory usage, and lack of mobile development.
Q22 (Tough - Interview): What is the Global Interpreter Lock (GIL)?
Explanation: GIL is a mutex that protects access to Python objects, affecting multithreading.
Q23 (Tough - Interview): How is Python interpreted internally?
Explanation: Python source code is compiled to bytecode, then executed by the CPython interpreter.
Q24 (Tough - Interview): How is memory allocated for variables in Python?
Explanation: Python stores variables as objects in heap memory, with reference counting.