0% found this document useful (0 votes)
1 views2 pages

Python Extra

The document explains Python decorators, which are functions that modify the behavior of other functions without altering their code. It also covers basic operations like modulus, division, and floor division, as well as the use of the len() function, initialization of empty data structures, anonymous functions with lambda, and methods for copying and deleting dictionaries. Additionally, it introduces map, filter, and reduce functions, and provides an example of counting character occurrences in a string.

Uploaded by

Ram
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Python Extra

The document explains Python decorators, which are functions that modify the behavior of other functions without altering their code. It also covers basic operations like modulus, division, and floor division, as well as the use of the len() function, initialization of empty data structures, anonymous functions with lambda, and methods for copying and deleting dictionaries. Additionally, it introduces map, filter, and reduce functions, and provides an example of counting character occurrences in a string.

Uploaded by

Ram
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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))

You might also like