Python Programming Worksheet – Grade 11
Instructions: 1. Write Python programs to solve the following problems. 2. Test your code with sample
inputs.
Question 1: Swapping between two numbers
Program:
# Swapping two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping: a =", a, ", b =", b)
Question 2: Swapping between three numbers
Program:
# Swapping three numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
a, b, c = b, c, a
print("After swapping: a =", a, ", b =", b, ", c =", c)
Question 3: Check if a year is a leap year
Program:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
1
Question 4: Area of a rectangle
Program:
length = float(input("Enter length of rectangle: "))
breadth = float(input("Enter breadth of rectangle: "))
area = length * breadth
print("Area of rectangle is", area)
Question 5: Area of a circle
Program:
import math
radius = float(input("Enter radius of circle: "))
area = [Link] * radius ** 2
print("Area of circle is", area)
Question 6: Total and percentage of marks
Program:
marks = []
num_subjects = int(input("Enter number of subjects: "))
for i in range(num_subjects):
m = float(input(f"Enter marks for subject {i+1}: "))
[Link](m)
total = sum(marks)
percentage = total / num_subjects
print("Total marks:", total)
print("Percentage:", percentage)
Question 7: Print first 10 numbers
Using while loop:
count = 1
while count <= 10:
2
print(count, end=' ')
count += 1
Using for loop:
for i in range(1, 11):
print(i, end=' ')
Question 8: Repeat a greeting 5 times
Program:
for i in range(5):
print("Hello!")
Question 9: Check if a number is even or odd
Program:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")