2024-02-02T17-09-04.518Z-Day-4 - Functions
2024-02-02T17-09-04.518Z-Day-4 - Functions
By Aksadur Rahman
aksadur@yahoo.com
Functions
Functions
Parameters/Arguments
Keyword Parameters/Arguments
Default Parameter Value
Return Statement
Lambda Function
Map and Filter function
Zip Function
Recursion
Functions
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a
function. A function can return data as a result.
x = max(5, 10)
#You can also send arguments with the key = value syntax.
#This way the order of the arguments does not matter.
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Return Statement
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Docstring
print(Add(10, 20))
print(Add.__doc__)
Lambda Function
Python Lambda Functions are anonymous function means that the function is without a name.
x = lambda a : a + 10
print(x(5))
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
def cube(y):
return y * y * y
lambda_cube = lambda y: y * y * y
print(cube(5))
print(result)
ages = [5, 12, 17, 18, 24, 32]
num = [5, 12, 17, 18, 25, 32]
def myFunc(x):
if x < 18: result = list(filter(lambda x: x%2==0, num))
return False
else: print(result)
return True print(num)
print(adults)