Chapter 7solution
Chapter 7solution
Pos=[]
Neg=[]
for i in range(n):
e=int(input())
List.append(e)
if e>0:
Pos.append(e)
else:
Neg.append(e)
14. Write a program to find the largest and second largest elements in a given list of
elements.
List=[]
for i in range(n):
e=int(input())
List.append(e)
List.sort(reverse=True)
15. Write a program to read a list of n integers and find their median. Note: The median
value of a list of values is the middle one when they are arranged in order. If there are
two middle values, then take their average.
List=[]
for i in range(n):
e=int(input())
List.append(e)
List.sort(reverse=True)
m=n//2
if n%2==0:
median=(List[m]+List[m-1])/2
else:
median=List[m]
18. Write a program to read a list of elements. Modify this list so that it does not contain
any duplicate items.(i.e.) all elements occurring multiple times in the list should appear
only once
List=[]
newlist=[]
for i in range(n):
e=int(input())
List.append(e)
for i in List:
if i not in newlist:
newlist.append(i)
19. Write a program to create a list of elements. Input an element from the user that has
to be inserted in the list. Also, input the position at which it is to be inserted.
List=[10,20,30,40,50]
List.insert(pos-1,ele)
(i) The program should ask for the position of the element to be deleted from the list
and delete the desired element from the list.
(ii)The program should ask for the value to be deleted from the list and delete the
desired value from the list.
List=[]
for i in range(n):
e=int(input())
List.append(e)
print("List elements are : ",List)
List.pop(pos-1)
List.remove(val)
def add(List):
s=0
for i in List:
if i%2 !=0:
s+=i
return s
S=[1,4,7,8,3,7,8]
22. #Program to calculate mean of given list of numbers using user defined function
def mean(List):
s=0
for i in List:
s+=i
return (s/len(List))
S=[1,4,7,8,3,7,8]
newlist=[]
n=int(input("How many elements..."))
for i in range(n):
e=int(input())
List.append(e)
for i in List:
if i not in newlist:
newlist.append(i)
for i in range(n):
e=int(input())
List.append(e)
25. #Program to calculate and display total marks and percentage of a student
marks=[]
tot=0
avg=0
for i in range(5):
m=int(input())
marks.append(m)
tot+=marks[i]
avg=tot/5
print("Total :",tot)
print("Average :",avg)
26. #Program to multiply odd index of elements present in a list containing numbers and
strings by 2
List=[1,'A',2,'B','C',4,5,'D','E',6,7,8,'F','G']
for i in range(1,len(List),2):
print(List[i]*2)
for i in range(len(List)):
a=List.count(List[i])
for i in range(n):
a=int(input())
List.append(a)
T=List[n-1]
List[i]=List[i-1]
List[0]=T
for i in range(len(List)):
if List[i]%5==0:
List[i],List[i-1]=List[i-1],List[i]
stRecord=['Raman','A-36',[56,98,99,72,69],78.8]
Write python statements to retrieve the following information from the list strecord.
i) Percentage of student
stRecord[3]
max(stRecord[2])
stRecord[1]
stRecord[0]='Raghav'