filmov
tv
(B13) Python Part9 Python functions

Показать описание
20220225 170946
#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 myfun():
print("This is my function..")
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.
def myfun(name):
print("My Name is",name)
myfun("Rohan")
myfun("Vineela")
-output:
My Name is Rohan
My Name is Vineela
def myfun(name,age):
print("My Name is",name," ,""Age ",age)
myfun("Rohan",21)
myfun("Vineela",22)
--output--
My Name is Rohan ,Age 21
My Name is Vineela ,Age 22
---------------------------
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.
----------------------------------
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
def myfun(name,age,Area="Hyderabad"):
print("My Name is",name," ,""Age ",age,"from ",Area)
myfun("Rohan",21,"Pune")
myfun("Vineela",22)
--output--
My Name is Rohan ,Age 21 from Pune
My Name is Vineela ,Age 22 from Hyderabad
-------------------------------------------------------
def myfun(name,age,Area="Hyderabad"):
print("My Name is",name," ,""Age ",age,"from ",Area)
myfun("Rohan",21,"Pune")
myfun("Vineela",22)
myfun(name="Bharat",Area="Delhi",age=20)
--output----
My Name is Rohan ,Age 21 from Pune
My Name is Vineela ,Age 22 from Hyderabad
My Name is Bharat ,Age 20 from Delhi
--------------------------------------------------
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.
E.g. if you send a List as an argument, it will still be
a List when it reaches the function:
def mylist(anylist):
for x in anylist:
print(x)
list1=[23,55,66,22,88,44,33]
str1="Savantis"
mylist(list1)
mylist(str1)
print("Bye!")
--output--
23
55
66
22
88
44
33
S
a
v
a
n
t
i
s
Bye!
-----------------------
Return Values
To let a function return a value, use the return statement
def myfun(n):
return n * 10
print(myfun(2))
print(myfun(6))
--Output--
20
60
----------------------
def myavg(a,b):
print("a=",a,"b=",b)
return (a+b)/2
print("Avg of a ,b is",myavg(10,20))
--output--
a= 10 b= 20
Avg of a ,b is 15.0
--------------------------------------
The pass Statement
function definitions cannot be empty, but if you for some reason
have a function definition with no content, put in the pass statement
to avoid getting an error.
def fun_a():
print("function a")
def fun_b():
pass
fun_a()
---output---
function a
-----------------------------------------------------
#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 myfun():
print("This is my function..")
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.
def myfun(name):
print("My Name is",name)
myfun("Rohan")
myfun("Vineela")
-output:
My Name is Rohan
My Name is Vineela
def myfun(name,age):
print("My Name is",name," ,""Age ",age)
myfun("Rohan",21)
myfun("Vineela",22)
--output--
My Name is Rohan ,Age 21
My Name is Vineela ,Age 22
---------------------------
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.
----------------------------------
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
def myfun(name,age,Area="Hyderabad"):
print("My Name is",name," ,""Age ",age,"from ",Area)
myfun("Rohan",21,"Pune")
myfun("Vineela",22)
--output--
My Name is Rohan ,Age 21 from Pune
My Name is Vineela ,Age 22 from Hyderabad
-------------------------------------------------------
def myfun(name,age,Area="Hyderabad"):
print("My Name is",name," ,""Age ",age,"from ",Area)
myfun("Rohan",21,"Pune")
myfun("Vineela",22)
myfun(name="Bharat",Area="Delhi",age=20)
--output----
My Name is Rohan ,Age 21 from Pune
My Name is Vineela ,Age 22 from Hyderabad
My Name is Bharat ,Age 20 from Delhi
--------------------------------------------------
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.
E.g. if you send a List as an argument, it will still be
a List when it reaches the function:
def mylist(anylist):
for x in anylist:
print(x)
list1=[23,55,66,22,88,44,33]
str1="Savantis"
mylist(list1)
mylist(str1)
print("Bye!")
--output--
23
55
66
22
88
44
33
S
a
v
a
n
t
i
s
Bye!
-----------------------
Return Values
To let a function return a value, use the return statement
def myfun(n):
return n * 10
print(myfun(2))
print(myfun(6))
--Output--
20
60
----------------------
def myavg(a,b):
print("a=",a,"b=",b)
return (a+b)/2
print("Avg of a ,b is",myavg(10,20))
--output--
a= 10 b= 20
Avg of a ,b is 15.0
--------------------------------------
The pass Statement
function definitions cannot be empty, but if you for some reason
have a function definition with no content, put in the pass statement
to avoid getting an error.
def fun_a():
print("function a")
def fun_b():
pass
fun_a()
---output---
function a
-----------------------------------------------------