Grade IX-Program List
Grade IX-Program List
L1. Create a list in Python of children selected for a science quiz with the following names:
Solution:
# Create a list of children selected for a science quiz
children = ["Arjun", "Sonakshi", "Vikram", "Sandhya", "Sonal", "Isha", "Kartik"]
Output:
Initial list:
['Arjun', 'Sonakshi', 'Vikram', 'Sandhya', 'Sonal', 'Isha', 'Kartik']
List after removing 'Vikram':
['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik']
List after adding 'Jay':
['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Jay']
List after removing the item at the second position:
['Arjun', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Jay']
b. Print the elements from second to fourth position using positive indexing.
Solution:
# Create a list of numbers
num = [23, 12, 5, 9, 65, 44]
# b. Print the elements from second to fourth position using positive indexing
print("\nElements from second to fourth position (positive indexing):")
print(num[1:4])
Output:
Length of the list: 6
Elements from second to fourth position (positive indexing):
[12, 5, 9]
Elements from position 3 to position 5 (negative indexing):
[5, 9, 65]
L3. Write a Python program to find the sum of all numbers stored in a list.
Solution:
# Create a list of numbers
numbers = [12, 45, 7, 23, 56, 89, 34]
Output:
The sum of the numbers in the list is: 266
L4. Create a list of the first 10 even numbers, add 1 to each list item, and print the final
list.
Solution:
# Create a list of the first 10 even numbers
even_numbers = list(range(0, 20, 2))
# Print the original list
print("Original List:")
print(even_numbers)
Output:
Original List:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Final List:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
L5. Create a list List_1 = [10,20,30,40]. Add the elements [14,15,12] using the extend
function. Now, sort the final list in ascending order and print it.
Solution:
# Create a list
list_1 = [10, 20, 30, 40]
Output:
Original List:
[10, 20, 30, 40]
List after adding elements:
[10, 20, 30, 40, 14, 15, 12]
Sorted List:
[10, 12, 14, 15, 20, 30, 40]
L6. Create a list sub = [‘English’, ‘Hindi’, ‘French’, ‘Math’, ‘Science’].
d. Add the names ‘AI’ and ‘Science’ at the end of the list using extend() function and
display the final list.
Solution:
# Create a list of subjects
sub = ['English', 'Hindi', 'French', 'Math', 'Science']
# Add the names 'AI' and 'Python' at the end of the list using extend() function
sub.extend(['AI', 'Python'])
Output:
List after deleting 'Hindi':
['English', 'French', 'Math', 'Science']
Final List:
['English', 'French', 'Math', 'Science', 'AI', 'Python']
b. Print the smallest and largest number from the above list.
Solution:
# Create a list of numbers
numbers = [23, 56, 12, 55, 60, 23]
# b. Print the smallest and largest number from the above list
print("\nSmallest number:", min(numbers))
print("Largest number:", max(numbers))
Output:
Number '12' using negative indexing:
12
Smallest number: 12
Largest number: 60
Occurrence of number '23': 2
Q2.
F1. Write a program to find numbers that are divisible by 7 and multiples of 5 between
1200 and 2200.
Solution:
start = 1200
end = 2200
numbers = []
print("Numbers between " + str(start) + " and " + str(end) + " that are divisible by 7 and
multiples of 5:")
print(numbers)
Output:
Numbers between 1200 and 2200 that are divisible by 7 and multiples of 5:
[1225, 1260, 1295, 1330, 1365, 1400, 1435, 1470, 1505, 1540, 1575, 1610, 1645, 1680,
1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170]
F2. Write a program to input the monthly income of an employee between 40 and 60
years old and calculate the annual income tax on the basis of the following:
Tax Slab Rates
3 lakhs NIL
10 lakhs 30.00%
Solution:
# Input monthly income and age
monthly_income = float(input("Enter your monthly income: "))
age = int(input("Enter your age: "))
Output:
Enter your monthly income: 600000
Enter your age: 56
Annual Income: 7200000.0
Income Tax: 1970000.0
F3. Write a program to print the sum of even numbers from 1 to 15 using for loop.
Solution:
sum_even = 0
for i in range(1, 16):
if i % 2 == 0:
sum_even += i
print("The sum of even numbers from 1 to 15 is:", sum_even)
Output:
The sum of even numbers from 1 to 15 is: 56
F4. Write a program that determines if the user can ride a roller coaster or not.
(Hint: age should be greater than or equal to 18 and height at least 1.5 meters tall).
Solution:
print("Roller Coaster Ride Eligibility Checker")
Output:
Roller Coaster Ride Eligibility Checker
Enter your age: 12
Enter your height in meters: 1.5
Sorry, you are not eligible to ride the roller coaster.
You are too young to ride the roller coaster. You must be at least 18 years old.
11111
2222
333
44
5
Solution:
# outer loop to control the number of rows
for i in range(1, 6):
# inner loop to control the number of repetitions
for j in range(6 - i):
print(i, end="")
print()
Output:
11111
2222
333
44
5
F6. Write a Python program using a while loop to print even numbers from 1 to 20.
Solution:
i=1
# loop through numbers 1 to 20
while i <= 20:
if i % 2 == 0:
print(i)
#increment i by 1 after each iteration
i += 1
Output:
2
4
6
8
10
12
14
16
18
20
F7. A sports coach has conducted a fitness test of 200 marks. He awarded ‘Gold’ grade to
all the students who scored more than 180 marks, 'Silver' grade to the students who
scored between 160 and 180 marks, 'Bronze' grade to all the students who scored
between 140 to 160 marks, and 'Fail' grade to all the students who scored below 140
marks. Write a Python code that shows his grading strategy.
Solution:
marks = float(input("Enter the student's marks (out of 200): "))
B1. Write a program to check whether the entered number is positive and even, positive
and odd, negative and even, or negative and odd.
Solution:
num = int(input("Enter a number: "))
if num > 0:
if num % 2 == 0:
print(num, "is a positive even number.")
else:
print(num, "is a positive odd number.")
elif num < 0:
if num % 2 == 0:
print(num, "is a negative even number.")
else:
print(num, "is a negative odd number.")
else:
print("You entered zero. Zero is neither positive nor negative, and it is even.")
Output:
Enter a number: -8
-8 is a negative even number.
B2. Write a program to find the sum of numbers from 1 to 10 using the range(n) function.
Solution:
sum_of_numbers = 0
for i in range(1, 11):
sum_of_numbers += i
print("The sum of numbers from 1 to 10 is:", sum_of_numbers)
Output:
The sum of numbers from 1 to 10 is: 55
B3. Write a program calculate the surface area and volume of a cuboid.
Solution:
# Get the dimensions of the cuboid from the user
length = float(input("Enter the length of the cuboid: "))
width = float(input("Enter the width of the cuboid: "))
height = float(input("Enter the height of the cuboid: "))
# Calculate the surface area of the cuboid
surface_area = 2 * (length * width + width * height + length * height)
Output:
Enter the length of the cuboid: 7.5
Enter the width of the cuboid: 4
Enter the height of the cuboid: 2
Surface Area of the Cuboid: 106.0
Volume of the Cuboid: 60.0
B4. Write a program check whether the applicant is eligible to vote in the election or not.
Solution:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
citizenship = input("Are you an Indian citizen? (yes/no): ")
Output:
Enter your name: Anila
Enter your age: 22
Are you an Indian citizen? (yes/no): YES
Anila is eligible to vote in the election.