XI IP_ Lab Worksheet #12 - Answer Key
XI IP_ Lab Worksheet #12 - Answer Key
DEPARTMENT OF ICT
Subject: Informatics Practices (065) Topic: Lists in Python Lab Worksheet No.: 12
1. Write a Python program to create a list of natural numbers from 1 to 50 using a for loop.
2. Write a Python program to assign a list of 10 numbers. Calculate and display their sum and
average.
# Use a for loop to calculate the sum of all numbers in the list
for num in numbers:
total += num
3. Write a Python program to find the minimum and maximum elements from a list along with their
indices.
# Initialize variables for minimum and maximum values and their indices
min_val = numbers[0]
max_val = numbers[0]
min_index = 0
max_index = 0
ISD/ICT/XI/IP/2024-25 Page 1 of 11
# Use a for loop to iterate through the list
for i in range(len(numbers)):
# Check if the current number is smaller than the current minimum
if numbers[i] < min_val:
min_val = numbers[i]
min_index = i
# Check if the current number is larger than the current maximum
if numbers[i] > max_val:
max_val = numbers[i]
max_index = i
# Print the minimum and maximum values along with their indices
print("Minimum:", min_val, "at index", min_index)
print("Maximum:", max_val, "at index", max_index)
5. Write a Python program to count the number of occurrences of a given element in a list.
6. Write a Python program to create two lists of the same size and create a third list by adding
elements at the same indices from both lists.
7. Write a Python program to accept a list and calculate the sum of even and odd elements separately.
# Use a for loop to check and sum even and odd numbers
for num in numbers:
if num % 2 == 0:
even_sum += num
else:
odd_sum += num
9. Write a Python program to replace numbers at even indices with their squares and at odd indices
with their cubes. Display the final list.
10. Write a Python program to input a list of strings and display only those starting with the letter 'S'
or 's'.
11. Write a Python program to return the largest even number in a list of integers. If no even number
is found, display “No even number”.
12. Write a Python program to input a list of integers, remove all odd numbers, and display the
updated list.
13. Write a Python program to accept a list containing numbers between 1 and 12. Replace all numbers
greater than 10 with 10.
14. Write a Python program to display the nth term of the Fibonacci series based on user input.
ISD/ICT/XI/IP/2024-25 Page 5 of 11
# Calculate and print the nth term
print("Fibonacci term is:", fibonacci(n - 1))
15. Write a Python program to modify a list by adding 5 to all odd values and 10 to all even values.
16. Write a Python program to find unique and duplicate elements from a list.
17. Write a Python program to input a string and count the number of words.
18. Write a Python program to calculate the sum of positive and negative numbers in a list.
19. Write a Python program to create a list of five students' names (take input from the user).
20. Write a Python program to accept 10 numbers from the user. If a number is odd, add it to a
separate list.
# Use a for loop to accept 10 numbers and check for odd numbers
for i in range(10):
num = int(input("Enter a number: "))
if num % 2 != 0:
odd_numbers.append(num)
21. Write a Python program to find the largest number in a list without using an inbuilt function.
Example: A = [23, 12, 45, 67, 55]
22. Write a Python program to find the second-largest number in a list using an inbuilt function.
Example: A = [23, 12, 45, 67, 55]
23. Write a Python program to display all names from a list that start with the alphabet 'S'.
24. Write a Python program to display all names from a list whose length is exactly four characters.
ISD/ICT/XI/IP/2024-25 Page 8 of 11
# Initialize a list to store names with exactly 4 characters
filtered_names = []
25. Write a Python program to convert all odd numbers in a list to even by adding 1.
26. Write a Python program to print the multiplication table of the first even number in a list.
Example: If the list is L = [23, 13, 101, 6, 81, 9, 4], print the table of 6.
# Use a for loop to find the first even number and print its multiplication table
for num in numbers:
if num % 2 == 0:
print("Multiplication table of", num)
for i in range(1, 11):
print(num, "x", i, "=", num * i)
even_found = True
break
27. Write a Python program to concatenate all numbers in a list into a single number.
Example: Original List = [12, 34, 43, 2, 34]. Output = 123443234
ISD/ICT/XI/IP/2024-25 Page 9 of 11
# Define the list of numbers
numbers = [12, 34, 43, 2, 34]
28. Write a Python program to display names from a list where the second-last character is 'i'.
# Initialize a list to store names with 'i' as the second last character
filtered_names = []
29. Write a Python program to display names from a list where the first character is a vowel.
# Define vowels
vowels = 'AEIOUaeiou'
30. Write a Python program to remove the last element from a list and insert it at the beginning.
*********
ISD/ICT/XI/IP/2024-25 Page 11 of
11