Answers to these exercises are provided in Exhibit 25.53.
# Q1 Write a program that calculates the area of a rectangle (user inputs length and width).
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area of the rectangle:", area)
Enter length: 28
Enter width: 28
Area of the rectangle: 784.0
# Q2 Create a program that converts temperature from Fahrenheit to Celsius.
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print("Temperature in Celsius:", celsius)
Enter temperature in Fahrenheit: 86
Temperature in Celsius: 30.0
# Q3 Given a list of numbers, write a program to find the sum of all even numbers.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum_even = 0
for num in numbers:
# %: Modulo division returns the remainder of the division operation.
if num % 2 == 0:
sum_even += num
# short form of for statement:
sum_even = sum(num for num in numbers if num % 2 == 0)
print("Sum of even numbers:", sum_even)
Sum of even numbers: 30
# Q4 Create a program that removes duplicates from a list.
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print("List with duplicates removed:", unique_numbers)
List with duplicates removed: [1, 2, 3, 4, 5]
# Q5 Write a program that checks if a number is positive, negative, or zero.
num = float(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
Enter a number: 26
Positive
# Q6 Create a program that determines if a given year is a leap year.
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Enter a year: 2024
Leap year
# Q7 Write a program that prints the Fibonacci sequence up to a certain number (user input).Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones.
limit = int(input("Enter the limit for Fibonacci sequence: "))
a, b = 0, 1
while a <= limit:
print(a, end = " ")
a, b = b, a + b
Enter the limit for Fibonacci sequence: 100
0 1 1 2 3 5 8 13 21 34 55 89
Use the Search Bar to find content on MarketingMind.
Contact | Privacy Statement | Disclaimer: Opinions and views expressed on www.ashokcharan.com are the author’s personal views, and do not represent the official views of the National University of Singapore (NUS) or the NUS Business School | © Copyright 2013-2025 www.ashokcharan.com. All Rights Reserved.