You are on page 1of 14

#11.

Largest number in the list

s=[]

l=int(input("Enter the number of list elements: "))

print("Enter the numbers: ")

for i in range(l):

x=int(input())

s.append(x)

print("The list is ",s)

print("The largest number is ",max(s))

Output:
#12. Swap elements in the List

n=[]

a=2

print("Enter" ,a," elements :")

for i in range(a):

x=int(input())

n.append(x)

print("the list is ",n)

for i in range(1,a,2):

n[i],n[i-1]=n[i-1],n[i]

print("The swapped elements in the list: ",n)

Output:
#13. Search element in the List

n=[]

a=int(input("Enter the number of elements in the list : "))

print("Enter" ,a," elements :")

for i in range (a):

x=int(input())

n.append(x)

print("The list is ",n)

b=int(input("Enter the element(number) which you want to search: "))

if b in n:

print(b, "is in the given list")

else:

print(b, "is not in the given list")

Output:
#14.Sum of the Cubes

n=[]

a=int(input("Enter the number of elements in the list : "))

print("Enter" ,a," elements :")

for i in range (a):

x=int(input())

n.append(x)

print("The list is ",n)

m=int(input("Enter a number from the list: "))

sum=0

b=m

while(b>0):

c=b%10

sum+=c**3

b=b//10

if(sum==m):

print(m, "is equal to the sum of the cubes of its digits")

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

tup=eval(input("Enter a tuple :"))

ln = len(tup)

lies = False

mn = min(tup)

print("Minimum value =",mn)

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:

print("Minimum lies at tuple's middle")

else:

print("Minimum doesn't lie at tuple's middle")


Output:
# 16. Ascending order [Tuple]

tup = eval(input("Enter a tuple : "))

ln = len(tup)

mid = ln//2

if ln % 2 == 1:

mid = mid+1

half = tup[:mid]

if sorted(half) == list(tup[:mid]):

print("First half is sorted")

else:

print("First half is not sorted")

Output:
#17. Mean, Median & Mode

import statistics as stat

n=[]

a=int(input("Enter the number of elements in the list : "))

print("Enter" ,a," elements :")

for i in range (a):

x=int(input())

n.append(x)

print("The list is ",n)

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]

if val not in result:

result[val]=1

else:

result[val]+=1

print("Voting result is :",result)

mx = 0

for k in result:

if result[k]>mx:

mx = result [k]

winner = k

print("The winner is ",winner," with ",mx," votes")

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("Counting frequencies in list \n",words)

print(json.dumps(d,indent =1))

Output:
#20 Marklist

n = int(input("Enter the number of students :"))

stu={}

for i in range(1,n+1):

print("Enter details of Student ",(i))

rollno = int(input("Roll number :"))

name = input("Name :")

marks = float(input("Marks :"))

d = {"Rollno": rollno,"Name":name,"Marks":marks}

key = "Stu"+str(i)

stu[key] = d

print("Students with marks > 75 are")

for i in range(1,n+1):

key ="Stu"+str(i)

if stu[key]["Marks"] >= 75:

print(stu[key])
Output:

You might also like