1.What are the key features of Python?
Python is popular because it is:
- Interpreted – No need for compilation, executed line by line.
- Dynamically Typed – No need to declare variable types explicitly.
- Object-Oriented – Supports classes, objects, and inheritance.
- High-Level – Easy to read and write.
- Extensive Libraries – Built-in modules for AI, ML, Data Science, etc.
- Cross-Platform – Runs on Windows, Linux, MacOS.
2.What is the difference between Python 2 and Python 3?
Feature | Python 2 | Python 3 |
Print Statement | print "Hello" (no parentheses) | print("Hello") (parentheses required) |
Division | 5/2 = 2 (Integer division by default) | 5/2 = 2.5 (True division) |
Unicode Strings | Strings are ASCII by default | Strings are Unicode by default |
Support | No longer maintained | Actively maintained |
3.What are Python’s data types?
Python provides various built-in data types:
- Numeric –
int
,float
,complex
- Sequence –
list
,tuple
,range
,str
- Set –
set
,frozenset
- Mapping –
dict
- Boolean –
True
,False
- NoneType –
None
Example:
x = 10 # int
y = 3.14 # float
z = "Python" # str
lst = [1, 2, 3] # list
tpl = (4, 5, 6) # tuple
dct = {"key": "value"} # dict
4.What is the difference between a list and a tuple?
Feature | List | Tuple |
Definition | Mutable, ordered collection | Immutable, ordered collection |
Syntax | my_list = [1,2,3] | my_tuple = (1,2,3) |
Performance | Slower due to mutability | Faster as it is immutable |
Use Case | When modification is needed | When a fixed sequence is needed |
Example:
my_list = [1, 2, 3]
my_list.append(4) # Allowed
my_tuple = (1, 2, 3)
my_tuple[0] = 10 # TypeError (Tuples are immutable)
5.What is the difference between deep copy and shallow copy?
- Shallow Copy: Copies references to objects (changes affect both copies).
- Deep Copy: Creates a completely independent copy.
Example:
import copy
list1 = [[1, 2], [3, 4]]
shallow = copy.copy(list1)
deep = copy.deepcopy(list1)
list1[0][0] = 99
print(shallow) # [[99, 2], [3, 4]] (Changed)
print(deep) # [[1, 2], [3, 4]] (Unchanged)
6.What is Python’s lambda function?
A lambda function is an anonymous function defined with the lambda
keyword.
Example:
square = lambda x: x * x
print(square(5)) # Output: 25
7. What is the difference between is
and ==
?
==
compares values of objects.is
compares memory locations (whether they are the same object).
Example:
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True (Same values)
print(a is b) # False (Different objects)
print(a is c) # True (Same object)
8.What is Python’s garbage collection mechanism?
Python has automatic memory management using reference counting and garbage collection.
- Uses reference counting to track objects.
- If an object has zero references, it gets deleted.
- Garbage collector (gc module) removes circular references.
Example:
import gc
gc.collect() # Manually trigger garbage collection
9.What is the difference between @staticmethod
, @classmethod
, and instance methods?
Type | Defined Using | Access | Use Case |
Instance Method | def method(self) | self (instance) | Accesses instance data |
Class Method | @classmethod def method(cls) | cls (class) | Modifies class attributes |
Static Method | @staticmethod def method() | No self or cls | Utility function (no instance/class data needed) |
Example:
class MyClass:
def instance_method(self):
return "Instance method called", self
@classmethod
def class_method(cls):
return "Class method called", cls
@staticmethod
def static_method():
return "Static method called"
obj = MyClass()
print(obj.instance_method()) # Instance method
print(MyClass.class_method()) # Class method
print(MyClass.static_method()) # Static method
10.What is the difference between append()
and extend()
in Python?
append()
adds one element to the list.extend()
adds multiple elements (iterable) to the list.
Example:
lst = [1, 2, 3]
lst.append([4, 5])
print(lst) # [1, 2, 3, [4, 5]] (Nested list)
lst = [1, 2, 3]
lst.extend([4, 5])
print(lst) # [1, 2, 3, 4, 5] (Flattened)
11.What are Python generators?
A generator is a special function that returns an iterator using yield
.
- Uses
yield
instead ofreturn
. - Saves memory since it doesn’t store all values in memory.
Example:
def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
print(next(gen)) # 1
print(next(gen)) # 2
12.What is the difference between map()
, filter()
, and reduce()
?
Function | Purpose | Example |
map() | Applies a function to all elements | map(lambda x: x*2, [1,2,3]) → [2,4,6] |
filter() | Filters elements based on a condition | filter(lambda x: x>2, [1,2,3]) → [3] |
reduce() | Applies cumulative operation | reduce(lambda x,y: x+y, [1,2,3]) → 6 |
Example:
from functools import reduce
nums = [1, 2, 3, 4]
print(list(map(lambda x: x*2, nums))) # [2, 4, 6, 8]
print(list(filter(lambda x: x % 2 == 0, nums))) # [2, 4]
print(reduce(lambda x, y: x + y, nums)) # 10