Python Practice Worksheet – Term 1 Revision
Q1. Write a Python program to input marks of 5 subjects,
calculate total marks, average marks, and print the grade.
marks = []
for i in range(5):
m = int(input(f"Enter marks of subject {i+1}: "))
marks.append(m)
total = sum(marks)
avg = total / 5
print("Total:", total)
print("Average:", avg)
if avg >= 90:
print("Grade A")
elif avg >= 75:
print("Grade B")
elif avg >= 50:
print("Grade C")
else:
print("Grade D")
Q2. Write a Python program to print the multiplication table of any
number using while loop.
n = int(input("Enter a number: "))
i = 1
while i <= 10:
print(n, "x", i, "=", n*i)
i += 1
Q3. Explain any three list functions with examples.
fruits = ["apple", "banana", "cherry"]
# append() - adds element
fruits.append("mango")
print("After append:", fruits)
# remove() - removes element
fruits.remove("banana")
print("After remove:", fruits)
# sort() - sorts list
fruits.sort()
print("After sort:", fruits)
Q4. A teacher maintains roll numbers of students in a list. Write
Python code to: (i) display all roll numbers, (ii) count total, (iii)
display in ascending order.
roll = [23, 11, 45, 32, 19]
print("Roll numbers:", roll)
print("Total students:", len(roll))
roll.sort()
print("In ascending order:", roll)
Q5. Create a dictionary of 3 players with names as keys and runs
as values. Display all key-value pairs and player with maximum
runs.
players = {"Rohit": 87, "Virat": 120, "Dhoni": 99}
print("All players:", players)
max_player = max(players, key=players.get)
print("Highest runs scored by:", max_player, "with", players[max_player], "runs")
Q6. Consider list = [34, 56, 78, 21, 90, 11]. Write code to: (i) display
odd numbers, (ii) display product, (iii) sort descending, (iv)
display first 3 elements.
nums = [34, 56, 78, 21, 90, 11]
odds = [n for n in nums if n % 2 != 0]
print("Odd numbers:", odds)
product = 1
for n in nums:
product *= n
print("Product:", product)
nums.sort(reverse=True)
print("Sorted descending:", nums)
print("First 3 elements:", nums[:3])
Q7. Write a Python program to input 8 numbers and display: (i)
average, (ii) count of negative numbers, (iii) second largest, (iv)
second smallest, (v) sorted list.
nums = []
for i in range(8):
n = int(input("Enter number: "))
nums.append(n)
print("List:", nums)
avg = sum(nums)/len(nums)
print("Average:", avg)
neg = len([n for n in nums if n < 0])
print("Negative numbers count:", neg)
sorted_nums = sorted(nums)
print("Second largest:", sorted_nums[-2])
print("Second smallest:", sorted_nums[1])
print("Sorted list:", sorted_nums)
Q8. Create a dictionary of books with titles as keys and prices as
values. Display: (i) all items, (ii) book with lowest price, (iii)
average price.
books = {"Maths": 250, "Science": 300, "English": 200}
print("All books:", books)
min_book = min(books, key=books.get)
print("Lowest price book:", min_book, "with price", books[min_book])
avg_price = sum(books.values()) / len(books)
print("Average price:", avg_price)
Q9. A company gives bonus based on years of service. Write a
program to input years of service and print bonus.
years = int(input("Enter years of service: "))
if years >= 10:
bonus = 10000
elif years >= 5:
bonus = 7000
elif years >= 2:
bonus = 4000
else:
bonus = 2000
print("Bonus amount: ■", bonus)