Assignment def function
Assignment def function
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()
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 [ ]:
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=[]
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()
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,])
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()
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()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
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]])
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]])
In [ ]:
In [ ]:
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 [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()
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()
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 [ ]: volume()
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()
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()
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()
In [ ]:
In [ ]: area()
In [ ]:
In [ ]: def area(): #6a**2
a=6
area=(6*a**2)
print("area is =",area)
In [ ]: area()
area is = 216
In [ ]:
In [ ]: area()
In [ ]:
In [ ]:
In [ ]:
In [ ]: li=[]
def insertstudent(data):
li.append(data)
return li
In [ ]: insertstudent({"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 [ ]: unionset({1,2,3,4,5,6},{4,5,6,7,8,9},{10,11,12,9,8})
In [ ]:
In [ ]: ########### = Dictonary
In [ ]: def student():
stu={
"id":1,
"name":"amit",
"age":22,
"class":"bca"
}
return stu
In [ ]: student()
In [ ]:
student_dict=create_dictionary()
print(student_dict) # store a create_dictionary() to student_dict vari
student_dict["age"]=24 #update
print(student_dict)
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 [ ]: