(B17) Python part11 Python Function -def

preview_player
Показать описание
20220521 133213
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:

Ex.
def my_function():
print("Hello from a function")

my_function()
------------------
def myfun():
print("my 1st function")

myfun()
----------------------
def myfun():
print("my 1st function")

myfun()
myfun()
myfun()
myfun()
myfun()
-----------------------
Arguments
Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

Example
def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")
----------------------------
def myfun(name):
print("My name is ", name)

myfun('latif')

--output--
My name is latif
-----------
def myfun(name):
print("My name is ", name)

myfun('latif')
myfun('Manisha')
myfun('Tanu')
---
Arguments are often shortened to args in Python documentations.

Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that are passed into a function.

From a function's perspective:

A parameter is the variable listed inside the parentheses in the function definition.

An argument is the value that is sent to the function when it is called.
---------------------------------------------------------
def myfun(name,age):
print(f"My name is {name}, Age is {age}")

myfun('latif', 30)
myfun('Manisha',20)
myfun('Tanu',21)
-----------------------------------
Default Parameter Value
The following example shows how to use a default parameter value.

If we call the function without argument, it uses the default value:

Example
def my_function(country = "Norway"):
print("I am from " + country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
----------------------------------------------
def myfun(name,age,area='Hyd'):
print(f"My name is {name}, Age is {age}. I am from {area}")

myfun('latif', 30,'Vizag')
myfun('Manisha',20,'Nasik')
myfun('Tanu',21)
----------------------------------
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.

This way the function will receive a tuple of arguments, and can access the items accordingly:

Ex
If the number of arguments is unknown, add a * before the parameter name:

def my_function(*kids):
print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")
Arbitrary Arguments are often shortened to *args in Python documentations.

Keyword Arguments
You can also send arguments with the key = value syntax.

This way the order of the arguments does not matter.

Example
def my_function(child3, child2, child1):
print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
--------------------------------------------------------
def myfun(name,age,area='Hyd'):
print(f"My name is {name}, Age is {age}. I am from {area}")

myfun('latif', 30,'Vizag')
myfun(name='Manisha',area='Nasik',age=21)
myfun(age=22,area='Pune',name='Tanu')
------------------------------------
The pass Statement

def myfunction():
pass

----------------
Passing a List as an Argument
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

def myfun(abc):
for x in abc:
print(x)

fruits = ['apple', 'banana','mango','chico','guva']
flowers=['lilly', 'jasmine','rose','sunflower','tuplips']

myfun(fruits)
myfun(flowers)
---------------------------------
Return Values
To let a function return a value, use the return statement:

Example
def my_function(x):
return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))
----------------------------------
def myAVG(m1,m2):
print(f"m1 ={m1}, m2={m2}")
return print("avg marks=", (m1+m2)/2)

myAVG(55,64)
myAVG(44,64)
myAVG(77,54)
-------
def myAVG(m1,m2):
print(f"m1 ={m1}, m2={m2}")
return (m1+m2)/2

print('AVG Marks',myAVG(55,64))
print('AVG Marks',myAVG(44,64))
print('AVG Marks',myAVG(77,54))

---
def myAVG(m1,m2):
print(f"m1 ={m1}, m2={m2}")
avg = (m1+m2)/2
if avg -- 40:
print("failed")
elif avg -- 40 and avg -- 50:
print("2nd class")
else:
print("1st class")
return (m1+m2)/2

print('AVG Marks',myAVG(55,64))
print('AVG Marks',myAVG(34,44))
print('AVG Marks',myAVG(45,52))
--
Рекомендации по теме
join shbcf.ru