Functions (03 09 2024)
Functions (03 09 2024)
In [ ]: # functions
def function_name(parameters): #def means define
#statement 1
#statement 2
#statement 3
#statement 4
18
15
20
sravya and 22
sravya and 21
sravs and 21
std1 and 1
std2 and null
std3 and null
student and null
std5 and 5
In [ ]: # factorial of a number
n!= 1*2*3*4*5....*n
n=4
4*3*2*1
product=1
n=4
product*n=1*4=4 #n=n-1 4-1=3
product*n=4*3 #n=n-1 2
localhost:8888/lab? 1/3
9/3/24, 12:17 PM Untitled3
4*3*2 #n-1 1
4*2*3*1
In [ ]: n=4
product=1
1st iteration = product*n n=n-1 #1*4 n=3
2nd iteration = product*n n=n-1 #1*4*3 n=2
3rd iteration = product*n n=n-1 #1*4*3*2 n=2
4th iteration = product*n n=n-1 #1*4*3*2*1 n=1
stop
720
In [7]: n=int(input())
product=1
for i in range(n,1,-1):
product*=i
print(product)
720
In [33]: # built-in-functions
# length function len(iterable)
print(len('hello')) #hello=string
print(len([1,5,2,3,8,6,5,4])) #length of the list
#print(len(2035))
print(len(str(2035)))
a=10563
print(len(str(a)))
print(type(a))
b=str(a)
print(type(b))
a='2078' #string
a=int(a)
print(a)
print(type(a))
5
8
4
5
<class 'int'>
<class 'str'>
2078
<class 'int'>
localhost:8888/lab? 2/3
9/3/24, 12:17 PM Untitled3
178 8
17 7
1 1
In [ ]: n=15689023
# find the length of n
8
51
2
-6
55
In [ ]: http://210.212.210.89:31/thubfeedback
In [ ]: 51576292
localhost:8888/lab? 3/3