Professional Documents
Culture Documents
s=[]
for i in range(l):
x=int(input())
s.append(x)
Output:
#12. Swap elements in the List
n=[]
a=2
for i in range(a):
x=int(input())
n.append(x)
for i in range(1,a,2):
n[i],n[i-1]=n[i-1],n[i]
Output:
#13. Search element in the List
n=[]
x=int(input())
n.append(x)
if b in n:
else:
Output:
#14.Sum of the Cubes
n=[]
x=int(input())
n.append(x)
sum=0
b=m
while(b>0):
c=b%10
sum+=c**3
b=b//10
if(sum==m):
else:
print(m, "is not equal to the sum of the cubes of its digits")
print("The smallest digit from the list is ",min(n),"The largest digit from the list is ",max(n))
Output:
# 15. Minimum element lies in the Middle of the tuples
ln = len(tup)
lies = False
mn = min(tup)
if ln % 2 == 0:
half = ln//2
if mn == tup[half] or mn == tup[half-1]:
lies = True
else:
half = ln//2
if mn == tup[half]:
lies = True
if lies == True:
else:
ln = len(tup)
mid = ln//2
if ln % 2 == 1:
mid = mid+1
half = tup[:mid]
if sorted(half) == list(tup[:mid]):
else:
Output:
#17. Mean, Median & Mode
n=[]
x=int(input())
n.append(x)
n_mean = stat.mean(n)
n_median=stat.median(n)
n_mode = stat.mode(n)
print("Mean = ",n_mean)
print("Median = ",n_median)
print("Mode = ",n_mode)
Output:
#18. Voting
votes={1:'a',2:'a',3:'c',4:'a',5:'c',6:'b',7:'b',8:'b',9:'a',10:'c',11:'a',12:'b',13:'b',14:'a',15:'c'}
result={}
for k in votes:
val = votes[k]
result[val]=1
else:
result[val]+=1
mx = 0
for k in result:
if result[k]>mx:
mx = result [k]
winner = k
Output:
#19. Frequency of list of elements
import json
sentence = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
words=sentence.split()
d={}
for i in words:
key =i
if key not in d:
count = words.count(key)
d[key]=count
print(json.dumps(d,indent =1))
Output:
#20 Marklist
stu={}
for i in range(1,n+1):
d = {"Rollno": rollno,"Name":name,"Marks":marks}
key = "Stu"+str(i)
stu[key] = d
for i in range(1,n+1):
key ="Stu"+str(i)
print(stu[key])
Output: