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, including procedural, object-oriented, and functional programming. It
is widely used in web development, data analysis, artificial intelligence, and more.
Q: What are variables in Python?
A: Variables in Python are containers for storing data values. You don't need to declare their type
explicitly. For example:
x=5
name = 'Alice'
Python automatically assigns the data type.
Q: How do you comment in Python?
A: Use the hash symbol (#) for single-line comments. For multi-line comments, use triple quotes:
# This is a single-line comment
'''
This is a
multi-line comment
'''
Q: What is the difference between list and tuple?
A: Lists are mutable and defined using square brackets []. Tuples are immutable and use
parentheses (). Example:
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
Q: What are Python's data types?
A: Common data types in Python include:
Python Interview and Practice Questions
- int
- float
- str
- list
- tuple
- dict
- set
- bool
Q: How do you check the type of a variable?
A: Use the type() function. Example:
x = 10
print(type(x)) # Output: <class 'int'>
Q: What is indentation in Python?
A: Indentation refers to spaces or tabs used to define blocks of code. It is mandatory in Python.
Improper indentation results in IndentationError.
Q: How do you take input from a user?
A: Use the input() function. It always returns a string:
name = input('Enter your name: ')
Q: What is the difference between = and == ?
A: = is an assignment operator. == is a comparison operator. Example:
x = 5 # assigns 5 to x
print(x == 5) # checks if x equals 5
Q: How do you convert data types?
A: Use typecasting functions:
- int('5') => 5
- str(10) => '10'
Python Interview and Practice Questions
- float('3.14') => 3.14
Intermediate Python Interview Questions
Q: What is a Python function?
A: A function is a block of reusable code defined using the def keyword. Example:
def greet(name):
return f'Hello, {name}'
Q: What are *args and **kwargs?
A: *args allows a function to accept any number of positional arguments. **kwargs allows any
number of keyword arguments.
Example:
def func(*args, **kwargs):
pass
Q: What is list comprehension?
A: It is a concise way to create lists. Example:
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
Q: What is a lambda function?
A: An anonymous, single-expression function:
square = lambda x: x * x
print(square(5)) # 25
Q: What is a Python module?
A: A file containing Python definitions and code. Use `import module_name` to include it.
Q: What is a package in Python?
Python Interview and Practice Questions
A: A package is a collection of modules in a directory with an __init__.py file.
Q: What is the difference between is and ==?
A: == checks value equality, while is checks identity (memory location).
x = [1]
y = [1]
x == y # True
x is y # False
Q: How is exception handling done in Python?
A: Using try-except blocks:
try:
risky_code()
except Exception as e:
print(e)
Q: What is the purpose of the 'with' statement?
A: It simplifies exception handling for resources like files. Example:
with open('file.txt') as f:
data = f.read()
Q: What are generators?
A: Generators yield items one at a time using `yield`. They are memory-efficient for large datasets.
Example:
def gen():
yield 1
Python Interview and Practice Questions
yield 2