3 - Functions
3 - Functions
Python Functions
Defining a Function:
1) Keyword def is used to start the Function Definition. Def specifies the starting of
Function block.
2) def is followed by function-name followed by parenthesis.
3) Parameters are passed inside the parenthesis. At the end a colon is marked.
Example:
def add():
a=10
b=20
c=a+b
print(c)
add()
print('hello')
add()
output:
30
hello
30
# Function with arguments
def add(a,b):
c=a+b
print(c)
add(5,8)
print('hello ')
add(4,6)
output:
13
hello
10
x=add(5,8)
y=add(4,6)
print(x+y)
Output:
23
Types of Arguments
1) Positional argument
2) Keyword argument
3) Default argument.
1. Positional Arguments
def add(a,b):
print(a+b)
add(10,20)
print('one')
add(20,30)
output:
30
one
30
2. Keyword Arguments
def add(a,b):
print(a)
add(b=10,a=20)
print('one')
add(20,30)
Output:
30
one
50
3. default arguments
def add(a,b=5):
print(a)
add(20)
add(10,20)
add(b=10,a=20)
output:
25
30
30
Example 2
x=lambda a,b:a+b
print(x(10,20))
output:
30
Example 3
x=lambda a,b,c:a+b+c
print(x(10,20,30))
output:
60
Python *args
In the function, we should use an asterisk * before the parameter
name to pass variable length arguments.The arguments are passed as
a tuple and these passed arguments make tuple inside the function
with same name as the parameter excluding asterisk *.
for n in num:
sum = sum + n
print("Sum:",sum)
adder(3,5)
adder(4,5,6,7)
adder(1,2,3,5,6)
Python **kwargs
Python passes variable length non keyword argument to function
using *args but we cannot use this to pass keyword argument. For this
problem Python has got a solution called **kwargs, it allows us to
pass the variable length of keyword arguments to the function.
In the function, we use the double asterisk ** before the parameter
name to denote this type of argument. The arguments are passed as a
dictionary and these arguments make a dictionary inside function
with name same as the parameter excluding double asterisk **.
Example 3: Using **kwargs to pass the variable keyword arguments
to the function
def intro(**data):
print("\nData type of argument:",type(data))
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
Output
The factorial of 3 is 6
Modules
Save the file by the name addition.py. To import this file "import"
statement is used.
import addition
addition.add(10,20)
addition.add(30,40)
Output:
30
70
Eg:
1) msg.py:
def msg_method():
print ("Today the weather is rainy" )
return
2) display.py:
def display_method():
print ("The weather is Sunny" )
return
3) multiimport.py:
import msg,display
msg.msg_method()
display.display_method()
Output:
Today the weather is rainy
The weather is Sunny
1) area.py
def circle(r):
print (3.14*r*r)
return
def square(l):
print (l*l)
return
def rectangle(l,b):
print (l*b)
return
def triangle(b,h):
print (0.5*b*h )
return
2) area1.py
from area import square,rectangle
square(10)
rectangle(2,5)
Output:
100
10
1) area.py
def circle(r):
print 3.14*r*r
return
def square(l):
print l*l
return
def rectangle(l,b):
print l*b
return
def triangle(b,h):
print 0.5*b*h
return
2) area1.py
from area import *
square(10)
rectangle(2,5)
circle(5)
triangle(10,20)
Output:
100
10
78.5
100.0
Output:
5.0
4.0
3.0
20.0855369232
0.69314718056
8.0
0.0
1.0
1.61977519054