Python Exercises

  1. Write code to calculate the area of a rectangle.
  2. Write code that converts temperature from Fahrenheit to Celsius.
  3. Given a list of numbers, write code to find the sum of all even numbers.
  4. Write code that checks if a number is positive, negative, or zero.
  5. Write code that determines if a given year is a leap year.
  6. Write a function that calculates the factorial of a number.
  7. Create a dictionary representing a student with keys like "name," "age," and "grades”. Add code to print out the key and value.

Answers to these exercises are provided in Exhibit 25.53.

Area of a Rectangle
# 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
Fahrenheit to Celsius
# 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
Sum of Even Numbers
# 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
Removes Duplicates
# 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]
Check Positive, Negative or Zero
# 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
Leap Year?
# 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
Fibonacci Sequence
# 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 

Exhibit 25.53 Exercise answers. Jupyter notebook.


Previous     Next

Use the Search Bar to find content on MarketingMind.