Python part9 Functions and Lamda

preview_player
Показать описание
20 12 2021
Python 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.

Creating a Function
In Python a function is defined using the def keyword:

Example
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis:

Example
def my_function():
print("Hello from a function")

my_function()

def myname():
print("Hello, latif")

myname()

def cts(a,b):
print("a and b are", a,",",b)

cts(23,43)
cts(78,52)
#cts()
#cts(66)
cts(b=66,a=55)
#cts(22,33,44)

def myname(name,area="Chenni"):
print("Hello, ", name, "from",area)

myname("Prabhath","Hyd")
myname("Pooja","Pune")
myname("Latif")

def myname(name):
for x in name:
print("Hello, ", x)

namelist=["pooja","suman","anup","venkat","nikhil"]

myname(namelist)

def myfun(x):
return 5 * x

print(myfun(3))
print(myfun(8))

def myfun(x,y):
return print("x+y= ",x+y)

myfun(23,56)
myfun(12,77)

compiler - verify syntex
interpreter - converts high level language into machine understanding language
------------------------------------------
Python Lambda
A lambda function is a small anonymous function.

A lambda function can take any number of arguments,
but can only have one expression.

Syntax
lambda arguments : expression
The expression is executed and the result is returned:

Example
Add 10 to argument a, and return the result:

x = lambda a : a + 10
print(x(5))

x=lambda a:a+10
print(x(12))

x=lambda a,b:a+b
print(x(12,34))

def myfun(n):
return lambda a:a *n

mydoubler= myfun(2)

print(mydoubler(22))
Рекомендации по теме
welcome to shbcf.ru