0% found this document useful (0 votes)
0 views

Assignment def function

The document contains various Python function definitions that perform different tasks, such as checking if a number is even or odd, identifying prime and non-prime numbers, and performing matrix operations. It also includes functions for calculating sums, averages, and volumes, as well as implementing loops for printing patterns. Overall, it serves as a collection of examples demonstrating basic programming concepts and operations in Python.

Uploaded by

sushantsaini3333
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)
0 views

Assignment def function

The document contains various Python function definitions that perform different tasks, such as checking if a number is even or odd, identifying prime and non-prime numbers, and performing matrix operations. It also includes functions for calculating sums, averages, and volumes, as well as implementing loops for printing patterns. Overall, it serves as a collection of examples demonstrating basic programming concepts and operations in Python.

Uploaded by

sushantsaini3333
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/ 16

In [ ]:

In [ ]: ############ if Else program using def function (1)

In [ ]: def evenodd():
no=int(input("please enter a number to check"))
if no%2==0:
print(no,"is even")
else:
print(no,"is odd")

In [ ]: evenodd()

please enter a number to check25


25 is odd

In [ ]:

In [ ]: def prime_nonprime(li):
x=[]
y=[]
for data in li:
if data <2:
x.append(data)
else:
prime=True
for i in range(2,data):
if data % i==0:
prime=False
if prime:
x.append(data)
else:
y.append(data)
print(x,"prime")
print(y,"non_prime")

In [ ]:

In [ ]: prime_nonprime([3, 4, 7, 8, 10, 11, 20, 10, 8, 7, 5, 6, 2,])

[3, 7, 11, 7, 5, 2] prime


[4, 8, 10, 20, 10, 8, 6] non_prime

In [ ]:

In [ ]: def big_brother(a,b,c):
if a>b and a>c:
print("a is a big brother")
elif b>a and b>c:
print("b is a big brother")
else:
print("c is a big brother")

In [ ]: big_brother(15,10,5)

a is a big brother
In [ ]:

In [ ]: def evdn_odd(li):
x=[]
y=[]

for data in li:


if data%2==0:
x.append(data)
else:
y.append(data)
x.sort()
y.sort()
print("even",x)
print("odd",y)

In [ ]: evdn_odd([3, 4, 7, 8, 10, 11, 20, 30, 8, 7, 5, 6, 2,])

even [2, 4, 6, 8, 8, 10, 20, 30]


odd [3, 5, 7, 7, 11]

In [ ]:

In [ ]: def char():
ch=(input("please enter any char:"))
if ch=="A" or ch=="B" or ch=="C":
print("your input character is B/W A-C")
elif ch=="D" or ch=="E" or ch=="F":
print("your input character is B/W D-F")
elif ch=="G" or ch=="H" or ch=="I":
print("your input character is B/W G-I")
else:
print("out of the range")

In [ ]: char()

please enter any char:C


your input character is B/W A-C

In [ ]:

In [ ]: def check_number(li):
x=[]
y=[]
for data in li:
if data > 0:
x.append(data)
elif data < 0:
y.append(data)
x.sort()
y.sort()
print(x, "is positive")
print(y, "is negative")

In [ ]: check_number([1,-3,4,7,-1,4,7,4,-7,5,-41,32,-813,121,])

[1, 4, 4, 4, 5, 7, 7, 32, 121] is positive


[-813, -41, -7, -3, -1] is negative
In [ ]:

In [ ]: def check_number():
no=int(input("please enter a number:"))
if no > 0:
print(no, "is positive")
elif no < 0:
print(no, "is negative")
else:
print(no, "is Zero")

In [ ]: check_number()

please enter a number:-5


-5 is negative

In [ ]:

In [ ]: def check_number(li):
x=[]
y=[]
for data in li:
if data <20:
x.append(data)
else:
y.append(data)
x.sort()
y.sort()
x_avg = sum(x)/len(x)
y_avg = sum(y)/len(y)
print("Less then 20> ",x)
print("Greater then 20 <",y)
print("Average of less then 20==>",x_avg)
print("Average of Greater then 20==<",y_avg)

In [ ]: check_number([10,4,8,5,3,8,10,9,14,13,12,19,40,88,108,20,5,10])

Less then 20> [3, 4, 5, 5, 8, 8, 9, 10, 10, 10, 12, 13, 14, 19]
Greater then 20 < [20, 40, 88, 108]
Average of less then 20==> 9.285714285714286
Average of Greater then 20==< 64.0

In [ ]:

In [ ]: def avg():
a=int(input("Enter your number: "))
b=int(input("Enter your number: "))
c=int(input("Enter your number: "))

average=(a+b+c)/3
print(average)

In [ ]: avg()

Enter your number: 5


Enter your number: 4
Enter your number: 7
5.333333333333333
In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]: ############# MATRIX (2)

In [ ]:

In [ ]: def cal_sum(li):
s=0
for i in range(0,3):
s=s+li[0][i]
print(s)

s=0
for i in range(0,3):
s=s+li[1][i]
print(s)

s=0

for i in range(0,3):
s=s+li[2][i]
print(s)
print()
s=0
for i in range(0,3):
s=s+li[i][0]
print(s,end=" ")
s=0
for i in range(0,3):
s=s+li[i][1]
print(s,end=" ")
s=0
for i in range(0,3):
s=s+li[i][2]
print(s)
print()
s=0
for i in range(0,3):
s=s+li[i][i]
print(s) #diagonal

print()
s=0
for i in range(0,3):
s=s+li[i][2-i]
print(s) #diagonal

In [ ]: cal_sum([[1,2,3],[4,5,6],[7,8,9]])
6
15
24

12 15 18

15

15

In [ ]:

In [ ]: def cal_totalsum(x,y):

s_x = 0
for i in range(0,3):
s_x += x[i][i]
print("Sum of diagonal of x: ",s_x)

s_y = 0
for j in range(0,3):
s_y += y[j][j]
print("Sum of diagonal of y: ",s_y)

totalsum=s_x+s_y
print("total sum of both diagonals",totalsum)

In [ ]: cal_totalsum(x=[[1,2,3],[4,5,6],[7,8,9]] ,y=[[2,1,1],[2,1,1],[1,2,2]])

Sum of diagonal of x: 15
Sum of diagonal of y: 5
total sum of both diagonals 20

In [ ]:

In [ ]: def sum(x,y):
output=[]

for i in range(len(x)):
for j in range(len(x[i])):
output.append(x[i][j]**2 +y[i][j]**2)

print(output)

In [ ]: sum(x=[[1,2],[4,1]] ,y=[[2,2],[4,4]])

[5, 8, 32, 17]

In [ ]:

In [ ]: def Multiply(li):
s=1

for i in range(len(li)):
for j in range(len(li[i])):
print(li[i][j]**2,end=" ")
print()
s *=li[i][j]**2
print("Multiply all of these squared values together. :",s)
In [ ]: Multiply([[1,2,3],[4,5,6],[7,8,9]])

1
4
9
16
25
36
49
64
81
Multiply all of these squared values together. : 131681894400

In [ ]:

In [ ]: def onlymultiply(li):

s=1
for i in range(0,len(li)):
for j in range(0,len(li[i])):
s *= li[i][j]
print(s)

In [ ]: onlymultiply([[1,2,3],[4,5,6],[7,8,9]])

362880

In [ ]:

In [ ]: def above_five(li):
s=1
for i in range(len(li)):
for j in range(len(li[i])):
if li[i][j] > 5:
s *= li[i][j]

print(s)

In [ ]: above_five([[1,2,3],[4,5,6],[7,8,9]])

3024

In [ ]:

In [ ]: def above_fiveplus(li):
s=0
for i in range(len(li)):
for j in range(len(li[i])):
if li[i][j] > 5:
s += li[i][j]

print(s)

In [ ]: above_fiveplus([[1,2,3],[4,5,6],[7,8,9]])

30

In [ ]:
In [ ]: def matrixindexes(li):
for i in range(0,len(li)):
for j in range(0,len(li)):
print(i,j,end=" | ")
print()

In [ ]: matrixindexes([[1,2,3],[2,3,1],[4,2,1]])

0 0 | 0 1 | 0 2 |
1 0 | 1 1 | 1 2 |
2 0 | 2 1 | 2 2 |

In [ ]:

In [ ]: def matrixvalue(li):
for i in range(0,len(li)):
for j in range(0,len(li)):
print(li[i][j],end=" | ")
print()

In [ ]: matrixvalue([[1,2,3],[2,3,1],[4,2,1]])

1 | 2 | 3 |
2 | 3 | 1 |
4 | 2 | 1 |

In [ ]:

In [ ]: def cube(li):
for data in li:
demo=[]
for x in data:
z=x**3
demo.append(z)
print(demo)
print()

In [ ]: cube([[1,2,3],[3,2,1],[1,2,3]])

[1, 8, 27]

[27, 8, 1]

[1, 8, 27]

In [ ]:

In [ ]: def cube_appen2dlist(li):
output=[]
for data in li:
demo=[]
for x in data:
z=x**3
demo.append(z)
output.append(demo)
print()
print(output)
In [ ]: cube_appen2dlist([[1,2,3],[3,2,1],[1,2,3]])

[[1, 8, 27], [27, 8, 1], [1, 8, 27]]

In [ ]:

In [ ]:

In [ ]: ############# for while Lopps (3)

In [ ]:

In [ ]: def while_loop(x,y):
i=1
while i<x:
j=1
while j<y:
print(j, end=" ")
j=j+1
print()
i=i+1

In [ ]: while_loop(5,5)

1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

In [ ]:

In [ ]: def forloop(x):
for i in range(1,x):
for j in range(1,i):
print(j,end=" ")
print()

In [ ]: forloop(6)

1
1 2
1 2 3
1 2 3 4

In [ ]:

In [4]: def forloop2(): #############


x=int(input("enter a number:"))
y=int(input("enter a number above x number :"))
print()
for i in range(1,x):
for j in range(y,i,-1):
print(" ",end=" ")
for j in range(0,i):
print("*",end=" ")
for j in range(1,i):
print("*",end=" ")
print()

In [5]: forloop2()

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *

In [ ]:

In [ ]: def forloop3():
x=int(input("enter a numner above 64<"))
y=int(input("enter a numner above 65<"))
for i in range(x,y):
for j in range(64,i):
print(chr(i),end=" ")
print()

In [ ]: forloop3()

enter a numner above 64<65


enter a numner above 65<70
A
B B
C C C
D D D D
E E E E E

In [ ]:

In [ ]: def printtable():
x=int(input("please enter any value to print table above 0 <"))
i=1
while i<11:
print(x*i,end=" ")
i=i+1

In [ ]: printtable()

please enter any value to print table above 0 <5


5 10 15 20 25 30 35 40 45 50

In [ ]:

In [ ]: def printcharr_reverse(x,y):
i=x
while i>= y:
print(chr(i),end =" ")
i -= 1

In [ ]: printcharr_reverse(90,65)

Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
In [ ]:

In [ ]:

In [ ]: def forloop(x,y):
for i in range(0,x):
for j in range(y,i,-1):
print("*",end=" ")
print()

In [ ]: forloop(7,6)

* * * * * *
* * * * *
* * * *
* * *
* *
*

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]: ####### MATH EQUATION (4)

In [ ]: def volume(): #lbh


l=float(input("please enter l value"))
b=float(input("please enter b value"))
h=float(input("please enter h value"))
volume=(l*b*h)
print("volume is =",volume)

In [ ]: volume()

please enter l value1.2


please enter b value2.1
please enter h value3.2
volume is = 8.064

In [ ]:

In [ ]: def volume():
pie=3.14
radius=float(input("please enter a number="))
volume = (4/3) * pie * (radius ** 3)
print("volume is =",volume)

In [ ]: volume()
please enter a number=2.2
volume is = 44.57962666666668

In [ ]:

In [ ]: def volum_ehemispere():
pie=3.14
radius=float(input("please enter a number="))
volume = (2/3) * pie * (radius ** 3)
print("volume is =",volume)

In [ ]: volum_ehemispere()

please enter a number=2


volume is = 16.746666666666666

In [ ]:

In [ ]: def area():
pie=3.14
radius=float(input("please enter a number="))
surfacearea = (4) *pie* (radius**2)
print("volume is",surfacearea)

In [ ]: area()

please enter a number=2


volume is 50.24

In [ ]:

In [ ]: def total_surfacearea():
pie=3.14
radius=float(input("please enter a number="))
surfacearea = (2) *pie* (radius**2)
print("volume is",surfacearea)

In [ ]: total_surfacearea()

please enter a number=2.2


volume is 30.395200000000006

In [ ]:

In [ ]: def area(): #2(lb+bh+hl)


l=float(input("please enter a number="))
b=float(input("please enter a number="))
h=float(input("please enter a number="))
area=(2*(l*b+b*h+h*l))
print("area is =",area)

In [ ]: area()

please enter a number=4


please enter a number=2
please enter a number=3
area is = 52.0

In [ ]:
In [ ]: def area(): #6a**2
a=6
area=(6*a**2)
print("area is =",area)

In [ ]: area()

area is = 216

In [ ]:

In [ ]: def area(): #pie r(l+r)


pie=float(input("please enter pie value"))
r=float(input("please enter r value"))
l=float(input("please enter l value"))
area=(pie*r*(l*r))
print("area is =",area)

In [ ]: area()

please enter pie value4


please enter r value4
please enter l value4
area is = 256.0

In [ ]:

In [ ]:

In [ ]: ################# set, union, intersection, Disctonary

In [ ]:

In [ ]: li=[]
def insertstudent(data):
li.append(data)
return li

In [ ]: insertstudent({"id":1,"name":"amit","age":23},)

Out[ ]: [{'id': 1, 'name': 'amit', 'age': 23}]

In [ ]: insertstudent({"id":2,"name":"smit","age":24})

Out[ ]: [{'id': 1, 'name': 'amit', 'age': 23}, {'id': 2, 'name': 'smit', 'age': 24}]

In [ ]: def searchstu(name,li):
flag=False
for data in li:
if data["name"]==name:
print(data)
flag=True
if flag==False:
print("not found")

In [ ]: searchstu("smit",li)
{'id': 2, 'name': 'smit', 'age': 24}

In [ ]:

In [ ]: def unionset(s1,s2):
return s1 | s2

In [ ]: unionset({1,2,3,4,5},{3,4,5,6,7,8})

Out[ ]: {1, 2, 3, 4, 5, 6, 7, 8}

In [ ]:

In [ ]: def intersectionset(s1,s2):
return s1 & s2

In [ ]: intersectionset({1,2,3,4,5},{3,4,5,6,7,8})

Out[ ]: {3, 4, 5}

In [ ]:

In [ ]: def difference(s1,s2):
return s1 - s2

In [ ]: difference({1,2,3,4,5},{3,4,5,6,7,8})

Out[ ]: {1, 2}

In [ ]:

In [ ]: def symmetricdfifference(s1,s2):
return s1 | s2 - (s1 & s2)

In [ ]: symmetricdfifference({1,2,3,4,5},{3,4,5,6,7,8})

Out[ ]: {1, 2, 3, 4, 5, 6, 7, 8}

In [ ]:

In [ ]: def issuperset(s1,s2):
return s1 > s2

In [ ]: issuperset({1,2,3,4,5,5,6,7,8},{3,4,5,6,7,8})

Out[ ]: True

In [ ]: def issuperset(s2,s1):
return s2 > s1

In [ ]: issuperset({1,2,3,4,5},{1,2,3,4,5,6,7,8})

Out[ ]: False
In [ ]: ########## - TYPE CASTING

In [ ]: def unionset(tu1,tu2):
return set(tu1) | set(tu2)

In [ ]: unionset((1,2,3,4,5,6,),(4,5,6,7,8,9,))

Out[ ]: {1, 2, 3, 4, 5, 6, 7, 8, 9}

In [ ]:

In [ ]: def unionset(li1,li2):
return set(li1) | set(li2)

In [ ]: unionset([1,2,3,4,5,6,],[4,5,6,7,8,9,])

Out[ ]: {1, 2, 3, 4, 5, 6, 7, 8, 9}

In [ ]:

In [ ]: def unionset(s1, s2 ,s3):


return list(set(s1) | set(s2) | set(s3))

In [ ]: unionset({1,2,3,4,5,6},{4,5,6,7,8,9},{10,11,12,9,8})

Out[ ]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

In [ ]:

In [ ]: ########### = Dictonary

In [ ]: def student():

stu={
"id":1,
"name":"amit",
"age":22,
"class":"bca"
}
return stu

In [ ]: student()

Out[ ]: {'id': 1, 'name': 'amit', 'age': 22, 'class': 'bca'}

In [ ]:

In [3]: def create_dictionary():


student = {"id": 1, "name": "Amit", "age": 22, "class": "BCA"}
return student

student_dict=create_dictionary()
print(student_dict) # store a create_dictionary() to student_dict vari
student_dict["age"]=24 #update
print(student_dict)

student_dict["Gender"]="male" #ADD KEY AND VALUE


print(student_dict)
del student_dict["Gender"]
print(student_dict)

{'id': 1, 'name': 'Amit', 'age': 22, 'class': 'BCA'}


{'id': 1, 'name': 'Amit', 'age': 24, 'class': 'BCA'}
{'id': 1, 'name': 'Amit', 'age': 24, 'class': 'BCA', 'Gender': 'male'}
{'id': 1, 'name': 'Amit', 'age': 24, 'class': 'BCA'}

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:
In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

You might also like