0% found this document useful (0 votes)
7 views

Solution of Worksheet_List

The document contains a series of Python programming exercises focused on list manipulation, including finding largest/smallest elements, calculating sums, shifting elements, and removing duplicates. It provides code examples for each task, demonstrating how to implement various list operations such as searching, counting occurrences, and extending lists. Additionally, it includes user input handling for dynamic list operations.

Uploaded by

vunkersapperals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Solution of Worksheet_List

The document contains a series of Python programming exercises focused on list manipulation, including finding largest/smallest elements, calculating sums, shifting elements, and removing duplicates. It provides code examples for each task, demonstrating how to implement various list operations such as searching, counting occurrences, and extending lists. Additionally, it includes user input handling for dynamic list operations.

Uploaded by

vunkersapperals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

(List in Python)

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.

# Initialize an empty list to store student information


students = []

# Number of students to add


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

# Loop to add students


for i in range(num_students):
print("\nEnter details for Student", i + 1)
# Get information for each student
admission_no = input("Admission number: ")
name = input("Name: ")
percentage = float(input("Percentage: "))

# Add the information to the students list as a tuple


students.append((admission_no, name, percentage))

# Print the list of students with percentage above 75


print("\nStudents with percentage above 75%:")
for student in students:
if student[2] > 75:
print("\nAdmission No:", student[0])
print("Name:", student[1])
print("Percentage:", student[2])
25. Write a python code to check the frequency or number of occurrences of input
element/number in any list/or given list.
List1= [10, 20, 30, 40, 10, 50, 10]
Ans:
List1= [10, 20, 30, 40, 10, 50, 10]
print(List1.count(10))

or

List1= [10, 20, 30, 40, 10, 50, 10]


c=0
for i in List1:
if i==10:
c=c+1
print(c)

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

# Accept comma-separated sequence of words as input


input_sequence = input("Enter comma-separated sequence of words: ")

# Split the input sequence by commas


l = input_sequence.split(',')

# Remove duplicates while preserving order


unique_words = []
for x in l:
if x not in unique_words:
unique_words.append(x)

# Sort the list of unique words


unique_words.sort()

# Print the unique words in sorted form


print("Unique words in sorted form:", end=' ')
for word in unique_words:
print(word, end=', ')

29. Write a python program to remove duplicate from a list.


a=[10,20,30,20,10,50,60,40,80,50,40]

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)

print("List after removing duplicates:", unique_list)


30. Write a python program to count the number of element in a list within a specified
range.

# 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

print("Number of elements in the specified range:", count)

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]

# Extend the first list with the second list


first_list.extend(second_list)

# Print the extended list


print("Extended list:", first_list)

You might also like