In the last session, we discussed the Python functions Operations. Now, we will look at Python's advanced function methods and Operations. The sets are the most important topic in Python.

Let’s start

Definition of Lambda Functions in Python

  1. Syntax of Lambda Function

Code
lambda arguments: expression
  1. Simple example of a lambda function

Code
#input
#without lambda function
def addition(a,b):
    return a+b
addition(2,3)  

#output
5

#input
#With lambda function
addition=lambda a,b:a+b
print(addition(2,3))

#output
5

3.Check the Even number or Not

Code
#input
even=lambda num:num%2==0
even(12)

#output
True

4.lambda with multiple parameters

Code
#input
addition1=lambda x,y,z:x+y+z
addition1(12,13,14)

#output
39

5.Map Function

Code
#input
#without map function
numbers=[1,2,3,4,5,6]
def square(number):
    return number**2
square(2)

#output
4

#with Map function
#input
list(map(lambda x:x**2,numbers))

#output
[1, 4, 9, 16, 25, 36]

6.Lambda function with map

Code
#input
numbers=[1,2,3,4,5,6,7,8]
list(map(lambda x:x*x,numbers))

#output
[1, 4, 9, 16, 25, 36, 49, 64]

7.MAp multiple iterables

Code
#input
numbers1=[1,2,3]
numbers2=[4,5,6]

added_numbers=list(map(lambda x,y:x+y,numbers1,numbers2))
print(added_numbers)

#output
[5, 7, 9]

8.Use map function to convert strings to integers

Code
#input
str_numbers = ['1', '2', '3', '4', '5']
int_numbers = list(map(int, str_numbers))
print(int_numbers)

#output
[1, 2, 3, 4, 5]

9.Upper the words using MAP function

Code
#input
words=['apple','banana','cherry']
upper_word=list(map(str.upper,words))
print(upper_word)

#output
['APPLE', 'BANANA', 'CHERRY']

10.Filter method

Code
#input
def even(num):
    if num%2==0:
        return True
even(24)

lst=[1,2,3,4,5,6,7,8,9,10,11,12]

list(filter(even,lst))

#output
[2, 4, 6, 8, 10, 12]

11.filter with a Lambda Function

Code
#input
numbers=[1,2,3,4,5,6,7,8,9]
greater_than_five=list(filter(lambda x:x>5,numbers))
print(greater_than_five)

#output
[6, 7, 8, 9]

12.Filter with a lambda function and multiple conditions

Code
#input
numbers=[1,2,3,4,5,6,7,8,9]
even_and_greater_than_five=list(filter(lambda x:x>5 and x%2==0,numbers))
print(even_and_greater_than_five)

#output
[6, 8]
  1. Filter() to check if the age is greate than 25 in dictionaries

Code
#input
people=[
    {'name':'peter','age':32},
    {'name':'Jack','age':33},
    {'name':'John','age':25}
]

def age_greater_than_25(person):
    return person['age']>25

list(filter(age_greater_than_25,people))

#output
[{'name': 'peter', 'age': 32}, {'name': 'Jack', 'age': 33}]

14.Function with args and *kwargs

Code
#input
def print_args_kwargs(*args, **kwargs):
    print('args:', args)
    print('kwargs:', kwargs)

print_args_kwargs(1, 2, 3, a='apple', b='banana')
print_args_kwargs('hello', 'world', x=10, y=20)

#output
args: (1, 2, 3)
kwargs: {'a': 'apple', 'b': 'banana'}
args: ('hello', 'world')
kwargs: {'x': 10, 'y': 20}

15.Returning Multiple Values

Code
#input
def powers(x):
    return x ** 2, x ** 3, x ** 4

print(powers(2))  
print(powers(3))  

#output
(4, 8, 16)
(9, 27, 81)
  1. Nested Functions

Code
#input
def outer_function(x, y):
    def inner_function(a, b):
        return a * b
    return inner_function(x, y)

print(outer_function(2, 3)) 
print(outer_function(4, 5)) 

#output
6
20

Now, We have covered advanced function operations with various examples and methods. In the next article, we will dive into Modules in Python.