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

XI IP_ Lab Worksheet #12 - Answer Key

This document is a lab worksheet for the Informatics Practices subject at Indian School Darsait, focusing on Python programming related to lists. It includes a series of exercises that guide students to write Python programs for various tasks involving lists, such as creating lists, calculating sums, finding minimum and maximum values, and manipulating list elements. Each exercise is accompanied by sample code and comments to assist students in understanding the concepts.

Uploaded by

dylan624589
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

XI IP_ Lab Worksheet #12 - Answer Key

This document is a lab worksheet for the Informatics Practices subject at Indian School Darsait, focusing on Python programming related to lists. It includes a series of exercises that guide students to write Python programs for various tasks involving lists, such as creating lists, calculating sums, finding minimum and maximum values, and manipulating list elements. Each exercise is accompanied by sample code and comments to assist students in understanding the concepts.

Uploaded by

dylan624589
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

INDIAN SCHOOL DARSAIT

DEPARTMENT OF ICT

Subject: Informatics Practices (065) Topic: Lists in Python Lab Worksheet No.: 12

Resource Person: Radha Krishan Badoni Date:


Name of the Student: ____________________________ Class: XI D Roll Number: ____

1. Write a Python program to create a list of natural numbers from 1 to 50 using a for loop.

# Initialize an empty list to store natural numbers


natural_numbers = []

# Use a for loop to append numbers from 1 to 50


for i in range(1, 51):
natural_numbers.append(i)

# Print the resulting list


print(natural_numbers)

2. Write a Python program to assign a list of 10 numbers. Calculate and display their sum and
average.

# Define the list of numbers


numbers = [5, 8, 12, 20, 7, 9, 15, 3, 10, 6]

# Initialize a variable to store the total sum


total = 0

# Use a for loop to calculate the sum of all numbers in the list
for num in numbers:
total += num

# Calculate the average by dividing the total by the number of elements


average = total / len(numbers)

# Print the sum and average


print("Sum:", total)
print("Average:", average)

3. Write a Python program to find the minimum and maximum elements from a list along with their
indices.

# Define the list of numbers


numbers = [5, 8, 12, 20, 7, 9, 15, 3, 10, 6]

# 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)

4. Write a Python program to search for an element in each list of numbers.

# Define the list of numbers


numbers = [5, 8, 12, 20, 7]

# Accept the target number to search for from the user


target = int(input("Enter the number to search: "))

# Initialize a flag to track if the number is found


found = False

# Use a for loop to iterate through the list


for i in range(len(numbers)):
# Check if the current number matches the target
if numbers[i] == target:
print(target, "found at index", i)
found = True
break # Exit the loop once the number is found

# If the number is not found, print a message


if not found:
print(target, "not found")

5. Write a Python program to count the number of occurrences of a given element in a list.

# Define the list of numbers


numbers = [5, 8, 12, 8, 7, 8, 5]

# Accept the target number from the user


target = int(input("Enter the number to count: "))

# Initialize a counter to track occurrences


count = 0

# Use a for loop to count occurrences of the target number


for num in numbers:
if num == target:
count += 1
ISD/ICT/XI/IP/2024-25 Page 2 of 11
# Print the count of the target number
print(target, "appears", count, "times.")

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.

Example: If A = [1, 2, 3] and B = [4, 5, 6], then C = [5, 7, 9].

# Define the two lists


A = [1, 2, 3]
B = [4, 5, 6]

# Initialize an empty list to store the results


C = []

# Use a for loop to add elements at the same indices


for i in range(len(A)):
C.append(A[i] + B[i])

# Print the resulting list


print("Resulting List:", C)

7. Write a Python program to accept a list and calculate the sum of even and odd elements separately.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3, 10]

# Initialize variables to store sums


even_sum = 0
odd_sum = 0

# 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

# Print the sums


print("Sum of Even Numbers:", even_sum)
print("Sum of Odd Numbers:", odd_sum)

8. Write a Python program to display all elements at even indices in a list.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3, 10]

# Initialize a list to store elements at even indices


even_index_elements = []

# Use a for loop to access elements at even indices


for i in range(0, len(numbers), 2):
even_index_elements.append(numbers[i])
ISD/ICT/XI/IP/2024-25 Page 3 of 11
# Print the elements
print("Elements at even indices:", even_index_elements)

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.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3, 10]

# Initialize an empty list to store the modified values


modified_list = []

# Use a for loop to modify elements based on their indices


for i in range(len(numbers)):
if i % 2 == 0:
modified_list.append(numbers[i] ** 2)
else:
modified_list.append(numbers[i] ** 3)

# Print the modified list


print("Modified List:", modified_list)

10. Write a Python program to input a list of strings and display only those starting with the letter 'S'
or 's'.

# Define the list of strings


strings = ["Sam", "Tom", "Sophie", "alex", "Sarah"]

# Initialize a list to store filtered strings


filtered = []

# Use a for loop to check each string


for s in strings:
if s.lower().startswith('s'):
filtered.append(s)

# Print the filtered strings


print("Strings starting with 'S':", filtered)

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”.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3]

# Initialize a variable to store the largest even number


largest_even = None

# Use a for loop to find the largest even number


for num in numbers:
if num % 2 == 0:
if largest_even is None or num > largest_even:
largest_even = num
ISD/ICT/XI/IP/2024-25 Page 4 of 11
# Check if an even number was found and print the result
if largest_even is not None:
print("Largest Even Number:", largest_even)
else:
print("No even number")

12. Write a Python program to input a list of integers, remove all odd numbers, and display the
updated list.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3, 10]

# Initialize a list to store even numbers


filtered = []

# Use a for loop to filter out odd numbers


for num in numbers:
if num % 2 == 0:
filtered.append(num)

# Print the updated list


print("List after removing odd numbers:", filtered)

13. Write a Python program to accept a list containing numbers between 1 and 12. Replace all numbers
greater than 10 with 10.

# Define the list of numbers


numbers = [1, 5, 12, 7, 15, 10]

# Initialize a modified list


modified = []

# Use a for loop to replace numbers greater than 10


for num in numbers:
if num > 10:
modified.append(10)
else:
modified.append(num)

# Print the modified list


print("Modified List:", modified)

14. Write a Python program to display the nth term of the Fibonacci series based on user input.

# Function to calculate the nth Fibonacci term


def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)

# Accept input for the Fibonacci term


n = int(input("Enter the term: "))

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.

# Define the list


L = [10, 20, 3, 100, 65, 87, 2]

# Initialize a modified list


modified = []

# Use a for loop to modify the values


for x in L:
if x % 2 == 0:
modified.append(x + 10)
else:
modified.append(x + 5)

# Print the modified list


print("Modified List:", modified)

16. Write a Python program to find unique and duplicate elements from a list.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3, 10, 8, 5]

# Initialize lists for unique and duplicate elements


unique = []
duplicates = []

# Use a for loop to identify unique and duplicate elements


for num in numbers:
if num not in unique:
unique.append(num)
elif num not in duplicates:
duplicates.append(num)

# Print the unique and duplicate elements


print("Unique elements:", unique)
print("Duplicate elements:", duplicates)

17. Write a Python program to input a string and count the number of words.

# Accept input from the user


text = input("Enter a string: ")

# Initialize a counter for words


word_count = 0

# Use a for loop to count words


for word in text.split():
word_count += 1

# Print the word count


ISD/ICT/XI/IP/2024-25 Page 6 of 11
print("Number of words:", word_count)

18. Write a Python program to calculate the sum of positive and negative numbers in a list.

# Define the list of numbers


numbers = [5, -8, 12, -7, 9, -3, 10]

# Initialize sums for positive and negative numbers


positive_sum = 0
negative_sum = 0

# Use a for loop to calculate sums


for num in numbers:
if num > 0:
positive_sum += num
else:
negative_sum += num

# Print the sums


print("Sum of positive numbers:", positive_sum)
print("Sum of negative numbers:", negative_sum)

19. Write a Python program to create a list of five students' names (take input from the user).

# Initialize an empty list for student names


students = []

# Use a for loop to accept names from the user


for i in range(5):
name = input("Enter name of student: ")
students.append(name)

# Print the list of student names


print("Students' names:", students)

20. Write a Python program to accept 10 numbers from the user. If a number is odd, add it to a
separate list.

# Initialize an empty list for odd numbers


odd_numbers = []

# 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)

# Print the list of odd numbers


print("List of odd numbers:", odd_numbers)

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]

# Define the list of numbers


ISD/ICT/XI/IP/2024-25 Page 7 of 11
numbers = [23, 12, 45, 67, 55]

# Initialize a variable to store the largest number


largest = numbers[0]

# Use a for loop to find the largest number


for num in numbers:
if num > largest:
largest = num

# Print the largest number


print("Largest number:", largest)

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]

# Define the list of numbers


numbers = [23, 12, 45, 67, 55]

# Initialize variables to track the largest and second-largest numbers


largest = second_largest = float('-inf')

# Use a for loop to find the second-largest number


for num in numbers:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num

# Print the second largest number


print("Second largest number:", second_largest)

23. Write a Python program to display all names from a list that start with the alphabet 'S'.

# Define the list of names


names = ["Sam", "Tom", "Sophia", "Alex", "Sarah"]

# Initialize a list to store filtered names


filtered_names = []

# Use a for loop to check for names starting with 'S'


for name in names:
if name.startswith('S'):
filtered_names.append(name)

# Print the filtered names


print("Names starting with 'S':", filtered_names)

24. Write a Python program to display all names from a list whose length is exactly four characters.

# Define the list of names


names = ["John", "Amy", "Dave", "Paul", "Mark"]

ISD/ICT/XI/IP/2024-25 Page 8 of 11
# Initialize a list to store names with exactly 4 characters
filtered_names = []

# Use a for loop to filter names with 4 characters


for name in names:
if len(name) == 4:
filtered_names.append(name)

# Print the names with 4 characters


print("Names with 4 characters:", filtered_names)

25. Write a Python program to convert all odd numbers in a list to even by adding 1.

# Define the list of numbers


numbers = [5, 8, 12, 7, 9, 3, 10]

# Initialize a modified list


modified = []

# Use a for loop to convert odd numbers to even


for num in numbers:
if num % 2 != 0:
modified.append(num + 1)
else:
modified.append(num)

# Print the modified list


print("Modified List:", modified)

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.

# Define the list of numbers


numbers = [23, 13, 101, 6, 81, 9, 4]

# Initialize a flag to check for even numbers


even_found = False

# 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

# If no even number is found


if not even_found:
print("No even number found in the list.")

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]

# Initialize an empty string to concatenate numbers


concatenated = ""

# Use a for loop to concatenate numbers


for num in numbers:
concatenated += str(num)

# Print the concatenated number


print("Concatenated number:", concatenated)

28. Write a Python program to display names from a list where the second-last character is 'i'.

# Define the list of names


names = ["Annie", "Niki", "Dani", "Tom", "Sami"]

# Initialize a list to store names with 'i' as the second last character
filtered_names = []

# Use a for loop to check the second-last character of each name


for name in names:
if len(name) > 1 and name[-2] == 'i':
filtered_names.append(name)

# Print the filtered names


print("Names with 'i' as second last character:", filtered_names)

29. Write a Python program to display names from a list where the first character is a vowel.

# Define the list of names


names = ["Aaron", "Eve", "Ian", "Uma", "Oscar", "Tom"]

# Define vowels
vowels = 'AEIOUaeiou'

# Initialize a list to store names starting with a vowel


filtered_names = []

# Use a for loop to check if the first character is a vowel


for name in names:
if name[0] in vowels:
filtered_names.append(name)

# Print the filtered names


print("Names starting with a vowel:", filtered_names)

30. Write a Python program to remove the last element from a list and insert it at the beginning.

Example: Original List = ['AARON', 300, 'MILAN', 100, 'ANANDANATARAJ'].

Updated List = ['ANANDANATARAJ', 'AARON', 300, 'MILAN', 100].


ISD/ICT/XI/IP/2024-25 Page 10 of
11
# Define the list
L = ['AARON', 300, 'MILAN', 100, 'ANANDANATARAJ']

# Remove the last element and insert it at the beginning


L.insert(0, L.pop())

# Print the updated list


print("Updated List:", L)

*********

ISD/ICT/XI/IP/2024-25 Page 11 of
11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy