Solution of Worksheet_List
Solution of Worksheet_List
20. Write a program to find the largest / smallest / second largest element from list or
tuple.
Ans:
num=[31,21,11,30,46,40]
num.sort()
print(num[0])
print(num[-1])
print(num[-2])
21. Write a python program to calculate the sum of all item ended with 3.
Ans:
l = [13,23,35,42,53]
total=0
for i in l:
if str(i)[-1]=="3":
total=total+i
print("sum of items ending with 3:",total)
22. Write a program in python to shift element of list to the left by a numeric value
inputted by user.
Sample list=[10,20,30,40,12,11]
Inputted value= 2
Output list=[30,40,12,11,10,20]
Ans:
l = [10, 20, 30, 40, 12, 11]
print("Original list:", l)
num = int(input("Enter the numeric value to shift left by: "))
list = l[num:] + l[:num]
print("Shifted list:", list)
23. Give python code to accept values from the user up to a certain limit entered by
the user, if the number is even, then add it to a list.
Ans:
e=int(input("No. of entries:"))
l=[]
for i in range(e):
n=int(input("Enter any number:"))
if n%2==0:
l.append(n)
print(l)
24. a) Write a program to add element in list as each element containing these information
[admno , name, percentage]
b) Write program to display those student detail whose percentage is above 75.
or
26. Write a python code to input a number and perform linear search or search in a
list.
Ans:
List= [10, 20, 30, 40, 20, 50, 10]
print(List.index(20))
or
List= [10, 20, 30, 40, 20, 50, 10]
n=int(input("Enter any number:"))
L=len(List)
for i in range(L):
if List[i]==n:
print(i)
break
27. Input a list of numbers and swap elements at even location with the element at
the odd location.
Ans:
List= [10, 20, 30, 40, 20, 50]
#List= [20, 10, 40, 30, 50, 20]
l=len(List)
for i in range(l):
if i%2==0:
List[i],List[i+1]=List[i+1],List[i]
print(List)
28. Write a python program that accepts a comma separated sequence of words as
input and print the unique words in sorted form.
Sample: 'red,white,black,red,green, black'
Expected: black,green,red,white
a = [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]
unique_list = []
for item in a:
if item not in unique_list:
unique_list.append(item)
# Example list
a = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# Specified range
start_range = 30
end_range = 70
# Initialize count to 0
count = 0
# Loop through the list and count elements within the specified range
for element in a:
if start_range <= element <= end_range:
count += 1
31. Write a python program to extend the first list with the second list.
Sample data: [1,3,5,7,9,10],[2,4,6,8]
Expected output: [1,3,5,7,9,10,2,4,6,8]
# Sample data
first_list = [1, 3, 5, 7, 9, 10]
second_list = [2, 4, 6, 8]