Function is used to reuse the specific code block, instead of repeat the same lines
multiple times
In [2]: # tax calculation
name=input('enter the name of the employee:')
salary=eval(input('enter the salary:'))
tax_per=eval(input('enter the tax_per:'))
tax_cal=salary*tax_per/100
print(f'the {name} tax is:{tax_cal}')
the rames tax is:1000.0
with out arguments/parameters
In [ ]: def <functionname>():
<write your code here>
In [5]: def tax_calculation():
name=input('enter the name of the employee:')
salary=eval(input('enter the salary:'))
tax_per=eval(input('enter the tax_per:'))
tax_cal=salary*tax_per/100
print(f'the {name} tax is:{tax_cal}')
call the function inorder to run the code
In [12]: tax_calculation()
the ramesh tax is:20000.0
function name rules same as variable name rules
function name same as any other variable name that we are using n the code
function name should not be any package or module name
function name should not be same as your file name
In [15]: import random
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
the avg of 38,51 and 4 is:31
In [19]: import random # packages are above functions
def AVERAGE():
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
AVERAGE()
the avg of 10,32 and 77 is:40
In [21]: import random # packages are above functions
def AVERAGE1():
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n222+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
In [23]: AVERAGE1()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[23], line 1
----> 1 AVERAGE1()
Cell In[21], line 6, in AVERAGE1()
4 n2=[Link](1,100)
5 n3=[Link](1,100)
----> 6 avg=round((n1+n222+n3)/3)
7 print(f'the avg of {n1},{n2} and {n3} is:{avg}')
NameError: name 'n222' is not defined
In [25]: import random # packages are above functions
AVERAGE2() # call
def AVERAGE2(): # def
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[25], line 2
1 import random # packages are above functions
----> 2 AVERAGE2() # call
3 def AVERAGE2(): # def
4 n1=[Link](1,100)
NameError: name 'AVERAGE2' is not defined
In [ ]: import random # packages are above functions
try
def AVERAGE3():
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n222+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
excpect
In [ ]:
In [ ]: import random # packages are above functions
def AVERAGE4():
try
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n222+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
excpet
In [31]: import random # packages should be above the function
def AVERAGE22():
try:
n1=[Link](1,100)
n2=[Link](1,100)
n3=[Link](1,100)
avg=round((n1+n2234+n3)/3)
print(f'the avg of {n1},{n2},{n3} is:{avg}')
except Exception as e:
print(e)
AVERAGE22()
#<function __main__.AVERAGE2()>
name 'n2234' is not defined
In [ ]: # wap ask the user take two number print subtraction multiplication and division
# wap ask the user take the radius find the area of circle
# area_of_circle=pi*r*r
# wap ask the user take height and breadth find the area of traingle
# area_of_traingle=0.5*b*h
# wap ask the user take 3 numbers find the average
# wap ask the user take the bill amount and tip anount calculate total bill
# wap ask the user take the salary, percetage of tax calculate tax amount
In [1]: n1=eval(input('enter n1:'))
n2=eval(input('enter n2:'))
add=n1+n2
sub=n1-n2
mul=n1*n2
div=round(n1/n2,2)
print(f'the addition of {n1} and {n2} are:{add}')
print(f'the Subtraction of {n1} and {n2} are:{sub}')
print(f'the Multiplcation of {n1} and {n2} are:{mul}')
print(f'the Division of {n1} and {n2} are:{div}')
the addition of 20 and 30 are:50
the Subtraction of 20 and 30 are:-10
the Multiplcation of 20 and 30 are:600
the Division of 20 and 30 are:0.6666666666666666
In [17]: def math_func():
n1=eval(input('enter n1:'))
n2=eval(input('enter n2:'))
add=n1+n2
sub=n1-n2
mul=n1*n2
div=round(n1/n2,2)
print(f'the addition of {n1} and {n2} are:{add}')
print(f'the Subtraction of {n1} and {n2} are:{sub}')
print(f'the Multiplcation of {n1} and {n2} are:{mul}')
print(f'the Division of {n1} and {n2} are:{div}')
math_func()
the addition of 20 and 30 are:50
the Subtraction of 20 and 30 are:-10
the Multiplcation of 20 and 30 are:600
the Division of 20 and 30 are:0.67
In [ ]: ################### Wrong way######################################
try:
def math_func():
n1=eval(input('enter n1:'))
n2=eval(input('enter n2:'))
add=n1+n2
sub=n1-n2
mul=n1*n2
div=round(n1/n2,2)
print(f'the addition of {n1} and {n2} are:{add}')
print(f'the Subtraction of {n1} and {n2} are:{sub}')
print(f'the Multiplcation of {n1} and {n2} are:{mul}')
print(f'the Division of {n1} and {n2} are:{div}')
In [ ]: ####################### WRONG
def math_func():
try:
n1=eval(input('enter n1:'))
n2=eval(input('enter n2:'))
add=n1+n2
sub=n1-n2
mul=n1*n2
div=round(n1/n2,2)
print(f'the addition of {n1} and {n2} are:{add}')
print(f'the Subtraction of {n1} and {n2} are:{sub}')
print(f'the Multiplcation of {n1} and {n2} are:{mul}')
print(f'the Division of {n1} and {n2} are:{div}')
math_func()
except
In [21]: def math_func():
try:
n1=eval(input('enter n1:'))
n2=eval(input('enter n2:'))
add=n1+n2
sub=n1-n2
mul=n1*n2
div=round(n1/n2,2)
print(f'the addition of {n1} and {n2} are:{add}')
print(f'the Subtraction of {n1} and {n2} are:{sub}')
print(f'the Multiplcation of {n1} and {n2} are:{mul}')
print(f'the Division of {n1} and {n2} are:{div}')
except Exception as e:
print(e)
math_func()
the addition of 20 and 30 are:50
the Subtraction of 20 and 30 are:-10
the Multiplcation of 20 and 30 are:600
the Division of 20 and 30 are:0.67
In [3]: import math
r=eval(input('enter radius:'))
area=round([Link]*r**2,2)
print(f'The area of traingle is:{area}')
The area of traingle is:1256.6370614359173
In [23]: import math
def area_of_traingle():
r=eval(input('enter radius:'))
area=round([Link]*r**2,2)
print(f'The area of traingle is:{area}')
area_of_traingle()
The area of traingle is:1256.64
In [5]: b=eval(input('enter n1:'))
h=eval(input('enter n2:'))
area=0.5*b*h
print(f'the area of traingle of {b} and {h} are:{area}')
the area of traingle of 20 and 30 are:300.0
In [27]: def area_of_traingle():
b=eval(input('enter b:'))
h=eval(input('enter h:'))
area=0.5*b*h
print(f'the area of traingle of {b} and {h} are:{area}')
area_of_traingle()
the area of traingle of 20 and 40 are:400.0
In [9]: n1=eval(input('enter n1:'))
n2=eval(input('enter n1:'))
n3=eval(input('enter n1:'))
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
the avg of 2,3 and 4 is:3
In [29]: def average():
n1=eval(input('enter n1:'))
n2=eval(input('enter n1:'))
n3=eval(input('enter n1:'))
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
average()
the avg of 20,33 and 4 is:19
In [11]: bill_amount=eval(input('enter the bill amount:'))
tip_per=eval(input('enter the tax_per:'))
tip_cal=bill_amount*tip_per/100
total_bill=bill_amount+tip_cal
print(f'the total_bill is:{total_bill}')
the total_bill is:1100.0
In [31]: def bill_cal():
bill_amount=eval(input('enter the bill amount:'))
tip_per=eval(input('enter the tax_per:'))
tip_cal=bill_amount*tip_per/100
total_bill=bill_amount+tip_cal
print(f'the total_bill is:{total_bill}')
bill_cal()
the total_bill is:24000.0
In [ ]:
In [13]: salary=eval(input('enter the salary:'))
tax_per=eval(input('enter the tax_per:'))
tax_cal=salary*tax_per/100
print(f'the tax is:{tax_cal}')
the tax is:40000.0
In [33]: def Tax():
salary=eval(input('enter the salary:'))
tax_per=eval(input('enter the tax_per:'))
tax_cal=salary*tax_per/100
print(f'the tax is:{tax_cal}')
Tax()
the tax is:20000.0
Define function first
Call the function next
If you miss brackets it will give indication as function
apply try-except inside function
if any error is there we will not get untill call the function
In [36]: import random
[Link]()
Out[36]: 0.4501275978610372
In [38]: import math
def area_of_circle():
"""
it will take no arguments just call it it will give the answer
"""
r=eval(input('enter radius:'))
area=round([Link]*r**2,2)
print(f'The area of traingle is:{area}')
area_of_circle()
The area of traingle is:1256.64
In [ ]: ############# With out arguments ##############
math_func()
area_of_traingle()
area_of_circle()
average()
bill_cal()
Tax()
With arguments
when we define function we can provide arguments also
That is called with arguments
how many total variables = 6
how many input variables =2 n1,n2
how many output variables=4 add,sub,mul,div
DO NOT TOUCH OUTPUT VARIABLES
keep input varaibles inside bracket
once you are keep inside bracket then remove those inside function
In [49]: def math_func1(n1,n2):
add=n1+n2
sub=n1-n2
mul=n1*n2
div=round(n1/n2,2)
print(f'the addition of {n1} and {n2} are:{add}')
print(f'the Subtraction of {n1} and {n2} are:{sub}')
print(f'the Multiplcation of {n1} and {n2} are:{mul}')
print(f'the Division of {n1} and {n2} are:{div}')
math_func1(100,200)
the addition of 100 and 200 are:300
the Subtraction of 100 and 200 are:-100
the Multiplcation of 100 and 200 are:20000
the Division of 100 and 200 are:0.5
In [51]: math_func1(eval(input()),
eval(input()))
math_func1(100,200)
the addition of 20 and 30 are:50
the Subtraction of 20 and 30 are:-10
the Multiplcation of 20 and 30 are:600
the Division of 20 and 30 are:0.67
In [53]: import math
def area_of_circle1(r):
"""
it will take no arguments just call it it will give the answer
"""
area=round([Link]*r**2,2)
print(f'The area of traingle is:{area}')
area_of_circle1(20)
The area of traingle is:1256.64
In [57]: def area_of_traingle1(b):
print('b:',b)
h=eval(input('enter h:'))
area=0.5*b*h
print(f'the area of traingle of {b} and {h} are:{area}')
area_of_traingle1(100)
b: 100
the area of traingle of 100 and 20 are:1000.0
In [59]: def average1(n1,n2,n3):
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
average1(10,20,40)
the avg of 10,20 and 40 is:23
In [61]: def bill_cal1(bill_amount,tip_per):
tip_cal=bill_amount*tip_per/100
total_bill=bill_amount+tip_cal
print(f'the total_bill is:{total_bill}')
bill_cal1(eval(input('enter the bill amount:')),
eval(input('enter the tax_per:')))
bill_cal1(1000,10)
bill_cal1([Link](1,10000),
[Link](1,20))
the total_bill is:11000.0
In [63]: def Tax1(salary,tax_per):
tax_cal=salary*tax_per/100
print(f'the tax is:{tax_cal}')
Tax1(20000,20)
the tax is:4000.0
In [65]: math_func1(n1=20,n2=30)
the addition of 20 and 30 are:50
the Subtraction of 20 and 30 are:-10
the Multiplcation of 20 and 30 are:600
the Division of 20 and 30 are:0.67
In [ ]: math_func1(n1=20,n2=30)
area_of_circle1(r=20)
area_of_traingle1(b=20)
average1(n1=20,n2=30,n3=40)
bill_cal1(bill_amount=2000,tip_per=10)
Tax1(salary=200000,tax_per=20)
In [67]: def tax_calulation(name, tax_per, salary):
"""
takes 3 args
"""
tax_cal = salary*(tax_per/100)
print(f"the {name} tax is: {tax_cal}")
tax_calulation(name = "sravya", salary = 3000, tax_per= 10)
the sravya tax is: 300.0
Default Arguments
It is also under function with arguments
but the argument value is fixed
So no need of provide explicitly again
if we want to change,we can change those values by the time of calling
and default arguments always at last we need to keep
In [74]: def Tax2(salary,tax_per=30):
print('Salary:',salary)
print('Tax_per:',tax_per)
tax_cal=salary*tax_per/100
print(f'the tax is:{tax_cal}')
In [76]: Tax2(20000) # s=20000, tp=30
Tax2(20000,10) # s=20000 tp=10
Salary: 20000
Tax_per: 30
the tax is:6000.0
Salary: 20000
Tax_per: 10
the tax is:2000.0
In [80]: def average2(n1,n2=30,n3):
print('n1:',n1)
print('n2:',n2)
print('n3:',n3)
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
average2(10,20)
Cell In[80], line 1
def average2(n1,n2=30,n3):
^
SyntaxError: parameter without a default follows parameter with a default
In [82]: def average2(n1,n3,n2=30):
print('n1:',n1)
print('n2:',n2)
print('n3:',n3)
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
average2(10,20)
n1: 10
n2: 30
n3: 20
the avg of 10,30 and 20 is:20
In [ ]: n1,n2,n3=30 # w
n1,n2=30,n2 # f
n1=30,n2,n3 # f
n1,n2=30,n3=30 # w
n1=20,n2,n3=30 # f
n1=20,n2=30,n3=40 # w
n1=20,n2=20,n3 #f
with out arguments
with arguments
Defualt arguments
In [4]: def average1():
n1=eval(input('enter the n1:'))
n2=eval(input('enter the n2:'))
n3=eval(input('enter the n3:'))
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
def average2(n1):
n2=eval(input('enter the n2:'))
n3=eval(input('enter the n3:'))
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
def average3(n1,n2):
n3=eval(input('enter the n3:'))
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
def average4(n1,n2,n3):
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
def average5(n1,n2,n3=30):
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
def average6(n1,n2=20,n3=30):
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
def average7(n1=10,n2=20,n3=30):
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
In [ ]: average1() # with out arguments
average2() # n1 : with argument, n2,n3: with out
average3() # n1,n2: with n3:with out
average4() # n1,n2,n3: with
average5() # n1,n2: with n3: defualt
average6() # n1: with n2,n3: defualt
average7() # n1,n2,n3: defualt
In [6]: average1()
the avg of 20,30 and 40 is:30
In [10]: average2(20)
the avg of 20,30 and 40 is:30
In [ ]: average3(20,30)
average3(n1=20,n2=30)
In [ ]: average4(10,20,30)
average4(n1=10,n2=20,n3=30)
In [ ]: average5(10,20)
average5(n1=10,n2=20)
In [ ]: average6(10)
average6(n1=10)
In [12]: average7()
the avg of 10,20 and 30 is:20
In [14]: complex(2,3)
# a+jb a+bj a+ib a+bi
# a= real
# b= imag
# 0+0j
Out[14]: 0j
In [18]: average7(100,200,300)
the avg of 100,200 and 300 is:200
In [20]: complex(2,3)
Out[20]: (2+3j)
In [22]: import random
[Link](10,20)
[Link](a=10,b=20)
[Link]() # works
In [26]: import math
[Link](5) # works
[Link](x=5) # fail because slash dont use argument name
Out[26]: 2.23606797749979
In [28]: def add1(a,/):
print(a+10)
def add2(a):
print(a+10)
In [32]: add1(10) #
add2(10)
add2(a=10)
20
20
20
In [34]: add1(a=10)
#what is the use of this signature add1(a,/)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[34], line 1
----> 1 add1(a=10)
TypeError: add1() got some positional-only arguments passed as keyword arguments:
'a'
In [ ]: def add3(a,/,b):
print(a+b)
def add4(a,b):
print(a+10)
add3(10,20) # w
add3(a=10,20) # slash(/) before dont mention
add3(a=10,b=20) # slash (/) before dont mention
add3(10,b=20) # w slash (/) after you can mention
add4(10,20)
add4(a=10,b=20)
In [ ]: [Link](x=90) # fail
In [ ]: def average1():
n1=300
n2=200
n3=100
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
In [36]: n1=300
n2=200
n3=100
def average8():
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
In [38]: average8()
the avg of 300,200 and 100 is:200
local vs Global variables
parameters/arguments/variables outside the functions is called as global variables
parameters/arguments/variables inside the functions is called as local variables
global variables can use anywhere inthe notebook, It can use inside the function also
local variables can not use outside the function
In [ ]: - function will define
- function call
- function code lines run
print('hello')
def average1():
n1=300
n2=200
n3=100
avg=round((n1+n2+n3)/3)
print(f'the avg of {n1},{n2} and {n3} is:{avg}')
print('good')
average1()
In [42]: def add():
a=100
b=200
c=300
summ=a+b+c
print(summ)
add()
600
In [44]: def add(a):
a=100
b=200
c=300
summ=a+b+c
print(summ)
add(200)
# step-1: define fun a
# step-2: call a= 200
# step-3: runn a=100
600
In [46]: def add(a=300):
a=100
b=200
c=300
summ=a+b+c
print(summ)
add(200)
# a=300 === 200 === 100
600
In [48]: def add(a=300):
b=200
c=300
summ=a+b+c
print(summ)
add(200)
# a=300 ===> a=200
700
In [60]: a=400
def add(): # 400
b=200
c=300
summ=a+b+c
a=600
print(summ)
a=500
add() # 500
# a=400 ==> 400 > 500 ==> 500 > 500
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
Cell In[60], line 9
7 print(summ)
8 a=500
----> 9 add()
Cell In[60], line 5, in add()
3 b=200
4 c=300
----> 5 summ=a+b+c
6 a=600
7 print(summ)
UnboundLocalError: cannot access local variable 'a' where it is not associated wi
th a value
unbound local error
In [63]: print(x)
x=10
# Name error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[63], line 1
----> 1 print(x)
2 x=10
NameError: name 'x' is not defined
In [65]: def f1():
print(y)
y=10
f1()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
Cell In[65], line 4
2 print(y)
3 y=10
----> 4 f1()
Cell In[65], line 2, in f1()
1 def f1():
----> 2 print(y)
3 y=10
UnboundLocalError: cannot access local variable 'y' where it is not associated wi
th a value
In [71]: y=10
def f1():
print(y)
y=10
f1()
10
In [ ]: print(y)
y=10 # Name error
def f1(): # unbound local error
print(y)
y=10
In [77]: summ=0
def f2():
a=10
b=20
summ=a+b
print(summ)
f2()
30
In [79]: summ
Out[79]: 0
using global keyword
using return statement
In [92]: def f3():
global summ1
a=10
b=20
summ1=a+b
f3()
In [94]: summ1
Out[94]: 30
In [120… summ1=1
sub=10
def f3():
global summ1,sub,a,b
a=10
b=20
summ1=a+b
sub=a-b
f3()
In [122… summ1,sub,a,b
Out[122… (30, -10, 10, 20)
return statements
In [2]: def avg(a,b,c):
average=(a+b+c)/3
print(average)
avg(10,20,30)
20.0
here the average is local variable
i can not use outside the function
we can use global variable to use average outside the function
In [5]: average
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[5], line 1
----> 1 average
NameError: name 'average' is not defined
In [7]: def avg(a,b,c):
global average
average=(a+b+c)/3
print(average)
avg(10,20,30)
20.0
In [9]: average
Out[9]: 20.0
In [11]: def avg(a,b,c):
global average
average=(a+b+c)/3
avg(10,20,30)
In [19]: def avg1(a,b,c):
average1=(a+b+c)/3
print(average1) # show me
return(average1)# give me
average1=avg1(100,20,30)
50.0
In [21]: average1
Out[21]: 50.0
In [23]: def avg1(a,b,c):
summ=a+b+c
average1=(a+b+c)/3
print(average1,summ) # show me
return(average1,summ)# give me
values=avg1(100,20,30) # 150,50
average1,summ=avg1(100,20,30)
# bag=150,50
# bag1,bag2=150,50
50.0 150
50.0 150
In [ ]: a=10,20
a,b=10,20
a,b=10
In [ ]: # wap ask the user take two number print subtraction multiplication and division
# wap ask the user take the radius find the area of circle
# area_of_circle=pi*r*r
# wap ask the user take height and breadth find the area of traingle
# area_of_traingle=0.5*b*h
# wap ask the user take 3 numbers find the average
# wap ask the user take the bill amount and tip anount calculate total bill
# wap ask the user take the salary, percetage of tax calculate tax amount
# with out
# with
# default
# global
# return
# try-except
In [25]: def tax_cal(salary,tax_per):
total_tax=salary*tax_per/100
return(total_tax)
total_tax=tax_cal(200000,20)
total_tax
Out[25]: 40000.0
function in functions
In [28]: def greet1():
print('hello')
def greet2():
print('bye')
In [30]: greet1()
hello
In [32]: greet2()
bye
In [40]: def greet1():
print('hello')
def greet2():
print('bye')
greet1()
greet3()
In [44]: def greet3():
print('good bye')
In [46]: greet2()
bye
hello
good bye
In [48]: def greet1():
print('hello')
def greet2():
print('bye')
greet1()
def greet4():
print('okay')
greet4()
greet2()
bye
hello
okay
In [ ]: def greet1():
print('hello')
def greet2():
print('bye')
greet1()
greet4()
greet2()
In [50]: def greet2():
print('bye')
def greet44():
print('okay')
greet44()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[50], line 6
3 def greet44():
4 print('okay')
----> 6 greet44()
NameError: name 'greet44' is not defined
In [52]: def greet1():
print('hello')
greet2()
def greet2():
print('bye')
greet1()
greet1()
hello
bye
In [72]: def greet6():
print('hey')
def greet5(name):
print(f'hello {name}')
greet5('omkar sir')
greet6()
hey
hello omkar sir
In [76]: def greet6(name):
print('hey')
print(name)
def greet5():
print(name)
print(f'hello {name}')
print(name)
greet5()
greet6('omkar sir')
hey
hello omkar sir
In [ ]: def f2():
print("bye")
f2()
In [80]: def f():
print("hello")
def f2():
print("bye")
f2()
f()
hello
bye
In [82]: # recursive
def greet7():
print('hello')
greet7()
greet7()
In [ ]: def f1(n):
print(n)
f1(5) # 5
def f1(n):
print(n-1)
f1(5) 4
n
n-1
n-2
n-3
In [88]: def f1(n):
val=n*f1(n-1)
print(val)
f1(3)
# step-1: 3*f(2) = 3*2*f(1)=3*2*1*0
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
Cell In[88], line 4
2 val=n*f1(n-1)
3 print(val)
----> 4 f1(3)
Cell In[88], line 2, in f1(n)
1 def f1(n):
----> 2 val=n*f1(n-1)
3 print(val)
Cell In[88], line 2, in f1(n)
1 def f1(n):
----> 2 val=n*f1(n-1)
3 print(val)
[... skipping similar frames: f1 at line 2 (2974 times)]
Cell In[88], line 2, in f1(n)
1 def f1(n):
----> 2 val=n*f1(n-1)
3 print(val)
RecursionError: maximum recursion depth exceeded
In [ ]: 5
4
3
2
1
5!= 5*4*3*2*1