Double Value - Python Programming Exercise

This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is calculated by adding up all four sides of the rectangle, the area is determined by multiplying the width and height, and the diagonal is found using the square root formula, where the diagonal is the hypotenuse of a right-angled triangle formed by the width and height. The math.sqrt(x) function is used to calculate the square root of the sum of the squares of the width and height, which gives the length of the diagonal. This exercise demonstrates the use of basic geometry formulas in Python, incorporating essential math functions and user input. By calculating the perimeter, area, and diagonal, this program helps in understanding how to manipulate and compute values in Python.

 Category

Python Data Types

 Exercise

Double Value

 Objective

Develop a Python program that calculates the perimeter, area, and diagonal of a rectangle, given its width and height.

(Hint: use y = math.sqrt(x) to calculate a square root)

 Example Python Exercise

 Copy Python Code
import math  # Import the math module to use sqrt function

# Prompt the user for the width and height of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))

# Calculate the perimeter, area, and diagonal
perimeter = 2 * (width + height)
area = width * height
diagonal = math.sqrt(width**2 + height**2)  # Using Pythagoras' theorem to calculate diagonal

# Display the results
print(f"The perimeter of the rectangle is: {perimeter}")
print(f"The area of the rectangle is: {area}")
print(f"The diagonal of the rectangle is: {diagonal}")

 Output

Case 1:
Enter the width of the rectangle: 5
Enter the height of the rectangle: 12
The perimeter of the rectangle is: 34.0
The area of the rectangle is: 60.0
The diagonal of the rectangle is: 13.0

Case 2:
Enter the width of the rectangle: 8
Enter the height of the rectangle: 15
The perimeter of the rectangle is: 46.0
The area of the rectangle is: 120.0
The diagonal of the rectangle is: 17.4642491965977

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Evaluate Function Outputs

    This Python program displays the values of the function y = x² - 2x + 1 for integer values of x ranging from -10 to +10. The function is a quadratic equation, ...

  •  Show Function

    This Python program "draws" the graph of the function y = (x-4)² by displaying a series of asterisks on the screen. For each x value ranging from -1 to 8, the ...

  •  Floating Point, Speed Units

    This Python program prompts the user to input a distance in meters and the time taken in hours, minutes, and seconds, and then calculates the speed in three different units:...

  •  Floating Point Sphere

    This Python program calculates both the surface area and volume of a sphere based on its radius. It uses the formulas for surface area (4 * pi * radius squared) and v...

  •  Vowel Check - Conditional

    This Python program prompts the user to input a symbol and categorizes it as a vowel (if it’s a lowercase vowel), a digit, or any other symbol. T...

  •  Northeast Triangle

    This Python program prompts the user to enter a width and then displays a triangle pattern where the number of stars decreases with each row, and underscores (...