List Examples in Python
Question 1. A program to find minimum element from a list of element along with its index in the list.
Answer:
element=eval(input("Enter list"))
length= len(element)
minelement=element[0]
minindex=0
for i in range(1,length):
if element[i]< minelement:
minelement=element[i]
minindex=i
print("Given List is : ",element)
print("The minimum element of the given list is : ")
print(minelement," at index ",minindex)
Screenshot:
Question 2. A program to calculate mean of a given list of numbers.
Answer :
element=input("Enter list : ")
length= len(element)
mean=sum=0
for i in range(0,length):
sum= sum + element[i]
mean=sum/length
print("Given list is : ",element)
print("The mean of the given list is : ",mean)
Screenshot:
Question 3. A program to search an element in a given list of numbers.
Answers:
element=eval(input("Enter list : "))
length= len(element)
find=int(input("Enter element to be search for : "))
for i in range(0,length):
if(find==element[i]):
print(find," found at index ",i)
break;
else:
print(find," is not found in the given list.")
Screenshot:
Question 4. A program to count frequency of the given element in a list of numbers.
Answer:
element=eval(input("Enter list : "))
length= len(element)
find=int(input("Enter element to be search for : "))
count=0
for i in range(0,length):
if(find==element[i])
count+=1
if count==0:
print(find," is not found.")
else:
print(find, " has frequency as ",count, " in the given list.")
Screenshot:
Question 5. A program to find the second largest number of a list .
Answer:
element=eval(input("Enter list : "))
length= len(element)
largest= seclarge=element[0]
for i in range(1,length):
if element[i]>largest:
seclarge=largest
largest=element[i]
elif element[i]>seclarge:
seclarge=element[i]
print("Largest number of the list is : ",largest)
print("Second Largest number of the list is : ",seclarge)
No comments:
Post a Comment