Python qs
Python qs
• Mapping: dict
• Boolean: bool
• None: NoneType
print(args, kwargs)
a = [1, 2, 3]
b=a
c = [1, 2, 3]
print(a == c) # True (same values)
import copy
shallow = copy.copy(list1)
deep = copy.deepcopy(list1)
list1[0][0] = 99
def decorator(func):
def wrapper():
func()
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
Output:
Hello!
def count():
for i in range(3):
yield i
gen = count()
print(next(gen)) # 0
print(next(gen)) # 1
add = lambda x, y: x + y
print(add(2, 3)) # 5
try:
x=1/0
except ZeroDivisionError:
finally:
print("Execution completed")
print("_".join(words)) # hello_world
20. What is the difference between classmethod, staticmethod, and instance methods?
@staticmethod
def static():
print("Static method")
@classmethod
def class_method(cls):
print("Class method")
def instance_method(self):
print("Instance method")
Demo.static()
Demo.class_method()
Demo().instance_method()
class A: pass
b = B()
class Demo:
def __init__(self):
self.x = 10
import copy
shallow = copy.copy(list1)
deep = copy.deepcopy(list1)
list1[0][0] = 99
class Meta(type):
class Demo(metaclass=Meta):
pass
# Output: Creating class: Demo
class A:
def hello(self):
return "Hello"
def new_hello(self):
return "Hi"
print(A().hello()) # Hi
• __init__(): Constructor
class Demo:
self.val = val
def __str__(self):
obj = Demo(10)
print(obj) # Value: 10
28. What is the difference between @staticmethod and @classmethod?
class Demo:
@staticmethod
def static():
print("Static method")
@classmethod
def class_method(cls):
print("Class method")
Demo.static()
Demo.class_method()
f.write("Hello, World!")
b = bytearray("hello", "utf-8")
m = memoryview(b)
print(index, name)
Output:
0 Alice
1 Bob
a = [1, 2, 3]
nums = [1, 2, 3, 4]
print(reduce(lambda x, y: x + y, nums)) # 10
x = 10
def test():
y = 20
test()
x = 10
fs = frozenset([1, 2, 3])
39. What is the difference between mutable and immutable objects in Python?
a = [1, 2, 3]
print(a) # [100, 2, 3]
b = (1, 2, 3)
def decorator(func):
def wrapper():
func()
return wrapper
@decorator
def hello():
print("Hello!")
hello()
Output:
Hello!
add = lambda x, y: x + y
print(add(5, 10)) # 15
def gen():
yield 1
yield 2
yield 3
g = gen()
print(next(g)) # 1
print(next(g)) # 2
44. How does Python handle memory management?
• Reference counting
import gc
a = [1, 2, 3]
b = [4, 5, 6]
46. What is the difference between a shallow copy and a deep copy?
import copy
shallow = copy.copy(lst)
deep = copy.deepcopy(lst)
lst[0][0] = 99
print("Executed directly")
else:
print("Imported as module")
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
s1 = Singleton()
s2 = Singleton()
class Duck:
def quack(self):
print("Quack!")
def make_quack(obj):
obj.quack()
d = Duck()
3. Mapping: dict
5. Boolean: bool
lst = [1, 2, 3]
tup = (1, 2, 3)
# tup[0] = 100 # TypeError: 'tuple' object does not support item assignment
lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = list(set(lst))
print(unique_lst) # [1, 2, 3, 4, 5]
lst = [1, 2, 3, 4]
print(lst[::-1]) # [4, 3, 2, 1]
lst.reverse()
print(lst) # [4, 3, 2, 1]
54. How do you sort a list in Python?
lst = [5, 1, 4, 2]
print(sorted(lst)) # [1, 2, 4, 5]
lst.sort()
print(lst) # [1, 2, 4, 5]
print(lst.index('banana')) # 1
Tuple Questions
• Tuples are immutable, meaning Python can optimize their storage better than lists.
lst = [1, 2, 3]
tup = tuple(lst)
print(tup) # (1, 2, 3)
• Yes, a tuple itself is immutable, but it can hold mutable objects like lists.
tup = ([1, 2], [3, 4])
tup[0].append(5)
a, b, c = tup
print(a, b, c) # 10 20 30
Set Questions
• Unordered collection
• No duplicate elements
• Mutable
d = {} # Empty dictionary
s = {1, 2, 3}
s.add(4) # Adds 4
s.remove(2) # Removes 2
print(s) # {1, 3, 4}
64. How do you find the union and intersection of two sets?
a = {1, 2, 3}
b = {3, 4, 5}
a = {1, 2}
b = {1, 2, 3, 4}
print(a.issubset(b)) # True
print(b.issuperset(a)) # True
Dictionary Questions
print(d["name"]) # Alice
d = {"a": 1, "b": 2}
print(key, value)
68. What is the difference between get() and direct key access in a dictionary?
d = {"x": 10}
print(d.get("x")) # 10
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
del d["age"]