Performing Mathematical Calculations with Python

Examples

Python can be used as a powerful calculator to perform various mathematical calculations. Here are some examples:

Addition

        x = 5
        y = 10
        result = x + y
        print(result)  # Output: 15
    

Subtraction

        x = 15
        y = 7
        result = x - y
        print(result)  # Output: 8
    

Multiplication

        x = 3
        y = 4
        result = x * y
        print(result)  # Output: 12
    

Division

        x = 10
        y = 2
        result = x / y
        print(result)  # Output: 5.0
    

Exponentiation

        x = 2
        y = 3
        result = x ** y
        print(result)  # Output: 8
    

Exercises

Now, it's time to practice! Solve the following exercises:

Exercise 1: Area of a Rectangle

Write a Python program to calculate the area of a rectangle. Ask the user to enter the length and width of the rectangle, then compute and display the area.

Exercise 2: Celsius to Fahrenheit Conversion

Write a Python program to convert a temperature from Celsius to Fahrenheit. Ask the user to enter a temperature in Celsius, then calculate and print the equivalent temperature in Fahrenheit. The formula for conversion is: Fahrenheit = (Celsius * 9/5) + 32.

Exercise 3: Hypotenuse of a Right Triangle

Write a Python program to calculate the length of the hypotenuse of a right triangle. Ask the user to enter the lengths of the two other sides of the triangle (a and b), then calculate and display the length of the hypotenuse using the Pythagorean theorem: hypotenuse^2 = a^2 + b^2.

Note: To work on the exercises, create a Python script file (e.g., calculations.py) and write the code for each exercise in separate functions. Then call the functions to test your solutions.