Shaik Reshma - Python Interview Guide
Lists
1. What is a list? - A list is an ordered and mutable collection of items that can store elements of any data
type and allows duplicates.
2. Common list methods: - append(element) → Adds element at the end -
insert(index, element) → Inserts element at a specific index - pop(index=-1) → Removes and
returns element at index (default last) - remove(element) → Removes first occurrence of element -
clear() → Removes all elements - index(element) → Returns index of first occurrence -
count(element) → Returns number of occurrences - sort() → Sorts list in ascending order
(reverse=True for descending) - reverse() → Reverses the order
3. How to iterate a list?
my_list = [1, 2, 3]
for item in my_list:
print(item)
Tuples
1. What is a tuple? - A tuple is an ordered and immutable collection of items. Allows duplicates.
2. Can tuples be modified? Why? - No, tuples are immutable, so elements cannot be changed once
created.
Sets
1. What is a set? - A set is an unordered collection of unique elements.
2. Why set cannot contain duplicate values? - Sets automatically remove duplicates to maintain
uniqueness.
Dictionaries
1. What is a dictionary? - A dictionary stores key-value pairs. Keys must be unique.
1
2. Dictionary methods: - get(key) → Returns value for key - items() → Returns all key-value pairs -
keys() → Returns all keys - values() → Returns all values - pop(key) → Removes key and returns its
value - update(dict) → Updates with another dictionary
Modules / File Handling
1. What is a module? - A module is a Python file with functions, classes, or variables that can be reused.
2. How to import a module?
import math
from math import sqrt
3. What is file handling? - File handling allows reading from and writing to files in Python.
4. File modes: - r → Read (default) - w → Write (overwrites) - a → Append (adds at end) - rb , wb →
Read/write binary
Error Handling
1. What is an exception? - An exception is an error detected during execution.
2. What are try / except? - try → Code block to test for exceptions - except → Handles the exception if
it occurs
try:
print(10/0)
except ZeroDivisionError:
print("Cannot divide by zero")
3. What is finally? - finally → Code that runs no matter what, often for cleanup
try:
f = open('[Link]')
finally:
[Link]()
2
OOP (Object-Oriented Programming)
1. What is OOP? - OOP is a programming paradigm based on objects and classes.
2. What is a class? - A class is a blueprint for creating objects.
3. What is an object? - An object is an instance of a class.
4. What is a constructor? - A constructor ( __init__ ) initializes object properties when created.
5. What is inheritance? - Allows a class to inherit attributes and methods from another class.
6. What is polymorphism? - Ability to use the same method or operator in different ways.
7. What is encapsulation? - Restricting access to class members using private or protected attributes.
8. What is abstraction? - Hiding internal details and showing only functionality.
Lambda, Map, Filter, Reduce, Comprehensions, Generators
1. Lambda functions: - Anonymous one-line functions
f = lambda x: x*2
print(f(5)) # 10
2. map() , filter() , reduce() : - map(func, iterable) → Applies function to all items -
filter(func, iterable) → Returns items where func(item) is True - reduce(func, iterable) →
Applies function cumulatively (from functools import reduce)
3. List comprehension:
squares = [x**2 for x in range(5)] # [0,1,4,9,16]
4. Generator: - Returns one item at a time, memory efficient
def gen():
for i in range(5):
yield i
5. Docstring: - A string used to document a function, class, or module
3
def add(a,b):
"""Returns sum of two numbers"""
return a+b
End of Python Interview Guide