Python Interview and Practice Questions
Prepared for: Devi Prasad Dubey
Date: May 17, 2025
Python Interview and Practice Questions
Basic Python Interview Questions
Q: What is Python?
A: Python is a high-level, interpreted programming language known for its simplicity and readability.
It supports multiple paradigms like object-oriented, procedural, and functional programming. Python
is widely used in data science, web development, and automation tasks.
Q: What are Python's basic data types?
A: Common data types include:
- int: Integer numbers (e.g., 5)
- float: Decimal numbers (e.g., 3.14)
- str: Strings (e.g., 'Hello')
- bool: Boolean values (True/False)
- list, tuple, set, dict: Collection types.
Q: What is a list in Python?
A: A list is an ordered, mutable collection of items. Defined using square brackets:
Example: my_list = [1, 2, 3, 'apple']
You can add, remove, or modify elements in a list.
Q: What is the difference between list and tuple?
A: - List is mutable: it can be changed.
- Tuple is immutable: once defined, it cannot be modified.
Syntax:
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
Q: How do you write a comment in Python?
A: - Use # for single-line comment
- Use triple quotes (''' or """) for multi-line comment
Q: How do you get input from a user?
Python Interview and Practice Questions
A: Use input() function. It always returns a string.
Example:
name = input('Enter your name: ')
Q: What is indentation in Python?
A: Indentation defines blocks of code. Python uses indentation instead of braces. Incorrect
indentation will raise an IndentationError.
Q: How can you check the type of a variable?
A: Use the type() function.
Example:
name = 'Alice'
print(type(name))
Q: What are conditional statements?
A: They are used to perform actions based on conditions:
- if
- elif
- else
Example:
if x > 0:
print('Positive')
Q: How do you loop through a list?
A: Use a for loop:
Example:
fruits = ['apple', 'banana']
for fruit in fruits:
print(fruit)
Intermediate Python Interview Questions
Python Interview and Practice Questions
Q: What are *args and **kwargs?
A: - args allows variable number of positional arguments.
- kwargs allows keyword arguments.
Example:
def func(*args, kwargs):
print(args)
print(kwargs)
Q: What is a lambda function?
A: A lambda function is an anonymous, one-liner function.
Example:
square = lambda x: x * x
print(square(4)) # 16
Q: What is list comprehension?
A: List comprehension is a concise way to create lists.
Example:
[x2 for x in range(5)] # [0, 1, 4, 9, 16]
Q: What is the difference between is and ==?
A: - == compares values
- is compares identity (memory address)
x = [1,2]
y = [1,2]
x == y # True
x is y # False
Q: How is exception handling done?
A: Using try-except blocks:
try:
risky()
Python Interview and Practice Questions
except Exception as e:
print(e)
Q: What is a module in Python?
A: A module is a .py file containing Python definitions. Use import module_name to use it.
Q: What are Python generators?
A: Generators use yield to return values one by one, saving memory.
Example:
def gen():
yield 1
yield 2
Q: What is the use of the 'with' statement?
A: It simplifies file handling and resource management.
Example:
with open('file.txt') as f:
data = f.read()
Q: What is a decorator?
A: A decorator modifies a function's behavior.
Example:
@decorator
def func():
pass
Q: How do you read and write files?
A: Use built-in open() function.
Example:
with open('file.txt', 'r') as f:
print(f.read())