0% found this document useful (0 votes)
73 views3 pages

Functions (03 09 2024)

This is about functions in python

Uploaded by

sravyachadaram6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views3 pages

Functions (03 09 2024)

This is about functions in python

Uploaded by

sravyachadaram6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/3/24, 12:17 PM Untitled3

In [ ]: # functions
def function_name(parameters): #def means define
#statement 1
#statement 2
#statement 3
#statement 4

In [ ]: def add1(a,b): #a=3 b=4


return a+b
add1(3,4)

In [5]: def multiple(a=4,b=5): #a=9 b=2


return a*b
print(multiple(9,2)) #function with 2 parameters
print(multiple(3)) #a=3 b=5 #function with 1 parameter
print(multiple()) #function with 0 parameters

18
15
20

In [7]: def check(name='sravs',age=21): #called function


return f'{name} and {age}'
print(check('sravya',22)) #sravya and 22
print(check('sravya')) #sravya and 21
print(check()) #sravs and 21 #check() is the calling function

sravya and 22
sravya and 21
sravs and 21

In [ ]: ## print 5 student details, if any of the details were missing,


# then take the default vale, parameters= student_name,roll_no
# default parameters are student_name=student, roll_no = null

In [9]: def student_details(std_name='student',roll_no='null'):


return f'{std_name} and {roll_no}'
print(student_details('std1',1))
print(student_details('std2'))
print(student_details('std3'))
print(student_details())
print(student_details('std5',5))

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

In [3]: n=int(input()) #n=4 #6


product=1
while n!=1: #4!=1 3!=1 2!=1
product*=n #4 12 24 #product=product*n
n-=1 #3 2 1 #n=n-1
print(product) #24

720

In [7]: n=int(input())
product=1
for i in range(n,1,-1):
product*=i
print(product)

720

In [ ]: ## write a program to find the factorial of a number using functions

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'>

In [ ]: ## find the length of a number without using built-in functions


# 17834 5
n=17834
n%10 =4
1783 1783%10=3 17834//10=1783

localhost:8888/lab? 2/3
9/3/24, 12:17 PM Untitled3

178 8
17 7
1 1

In [41]: n=int(input()) #17834


length=0
while n!=0:
n=n//10 #1783 178 17 1 0
length+=1 #1+1+1+1+1
print(length)#5

In [ ]: n=15689023
# find the length of n

In [35]: # max - returns the maximum value


# max()
print(max(5,8,3,6,4))
print(max([51,23,4]))

8
51

In [39]: # min - returns the minimum value


#min()
print(min(5,8,2,3))
print(min([54,23,1,-2,-6]))

2
-6

In [52]: # sum - return the sum of values


#range - range(start,end)
#print 1 to 10
#
print(sum(range(1,11)))

55

In [ ]: http://210.212.210.89:31/thubfeedback

In [ ]: 51576292

localhost:8888/lab? 3/3

You might also like