Advanced - Chapter 02
Advanced - Chapter 02
• Lambda functions
• Decorators
Advanced Python
• Imperative programming
• Functional programming
• Procedural programming
Advanced Python
Pure functions:
Example 01:
def square(x):
return x ** 2
sequare(3)
Advanced Python
Pure functions:
Example 02:
return a + b
Example 03:
return a * b
Example 04:
return s1 + s2
Higher-order functions
return func(x, y)
return x + y
return x * y
Lambda functions
Syntax:
Example 01:
square = lambda x: x ** 2
Example 02:
add = lambda x, y: x + y
result = add(2, 3)
print(result) # prints 5
Advanced Python
Example 03:
multiply = lambda x, y, z: x * y * z
result = multiply(2, 3, 4)
print(result) # prints 24
Advanced Python
print(upper(str1))
Advanced Python
Example 06:
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(13))
print(mytripler(13))
Advanced Python
for item in l:
print(item()) # [10,20,30,40]
Note: A Python list comprehension consists of brackets containing the expression, which is executed
for each element along with the for loop to iterate over each element in the Python list.
Advanced Python
● It uses lambda functions to sort each sublist and find the second-largest element in
each sublist. The result is a list of second-largest elements, which is then printed.
The output displays the second-largest element from each sublist in the original list.
Advanced Python
True or False:
1. Lambda functions can have multiple expressions.
2. Lambda functions have only one argument.
Advanced Python
Map:
Syntax:
map(function, iterable)
Where function is the function to apply to each element of the iterable, and
iterable is the object that you want to apply the function to.
Advanced Python
Example 01:
my_list = [1, 2, 3, 4, 5]
my_list1 = [1, 2, 3]
# List of strings
print(test)
#[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
Advanced Python
Filter:
● In Python, filter() is a built-in function that applies a given function to each element of
an iterable and returns a new iterable with only the elements that satisfy the condition.
Returns: an iterator that is already filtered.
Syntax:
filter(function, iterable)
Where function is the function to apply to each element of the iterable, and iterable is the object
that you want to apply the function to.
Advanced Python
my_list = [1, 2, 3, 4, 5]
Example 2: Filtering out strings that contain a specific substring using filter()
Example 3: Filtering out non negative numbers from a list using filter()
Example 4: Filtering out not empty strings from a list using filter()
my_list = [1, 2, 3, 4, 5]
def is_multiple_of_3(num):
return num % 3 == 0
#[3, 6, 9]
Advanced Python
Reduce function
● In Python, reduce() is a built-in function that applies a given function to the elements of
an iterable and returns a single value. The reduce() function is useful for performing
operations that combine multiple elements of a list, tuple, or other iterable.
Syntax:
Where function is the function to apply to each element of the iterable, and iterable is the object that you want to
apply the function to. The initializer parameter is optional and is used to specify an initial value for the
reduction.
Advanced Python
print(sum) # prints 15
Advanced Python
my_list = [1, 2, 3, 4, 5]
my_list = [1, 5, 3, 4, 2]
print(maximum) # prints 5
Advanced Python
Generator
● A generator function is a special type of function that returns an iterator, which can be
iterated over using the yield keyword.
● A generator function in Python is defined like a normal function, but whenever it needs
to generate a value, it does so with the yield keyword rather than return. If the body of
a def contains yield, the function automatically becomes a Python generator function.
● Each time the yield statement is encountered, the function's state is saved, and the
yielded value is returned.
● It can have one or more yield.
● It return a generator object
Advanced Python
Generator
● The next() function is used to retrieve the next value from the iterator.
● Python Generator functions return a generator object that is iterable, i.e., can be
used as an Iterator. Generator objects are used either by calling the next method of
the generator object or using the generator object in a “for in” loop.
● Standard Functions: Compute a value and return it once. They use the return
statement.
● Generators: Use the yield statement to return values one at a time, allowing iteration
over a sequence of values without storing the entire sequence in memory.
Advanced Python
yield z print(next(gen)) #
Output: 6
print(next(gen)) #
Output: 7
Advanced Python
Decorators
Example 00:
Example 01:
return wrapper
Advanced Python