1.
PYTHON DECORATOR
A decorator is a function that modifies the behavior of another function without changing
its actual code
def my_decorator(func):
def wrapper():
print("Before function call")
func()
return wrapper
Explanation
• my_decorator(func)
o This function accepts another function as an argument.
• Inside it, a new function wrapper() is created.
o This wrapper function controls what happens before calling the original
function.
• print("Before function call")
o This line runs before the original function executes.
• func()
o This calls the original function passed into the decorator.
• return wrapper
o The decorator returns the wrapper function instead of the original function.
Modulus % vs Division / vs Floor Division //
Answer:
/ returns normal division result (float),
// returns integer division result (floor value),
% returns remainder after division.
5/2 = 2.5
5//2 = 2
5%2 = 1
What does len() do
Answer:
len() returns the number of elements in a string, list, tuple, set, or dictionary.
Initialize empty list tuple set dict
list = []
tuple = ()
set = set()
dict = {}
Anonymous vs Lambda Function
Anonymous function is a function without a name, and in Python it is created using
lambda keyword
lambda x: x+1
Copy and Delete Dictionary
d2 = [Link]()
del d1
del d1["key"]
Map Filter Reduce in Python
map() → applies function to all elements
filter() → selects elements based on condition
reduce() → reduces list into single value
Count number of characters and occurrence
s = "python"
for i in s:
print(i, [Link](i))