Here’s an example of how to solve a geometry problem using Python.
Area of a Rectangle
Problem Statement
A rectangle has a base of 8 cm and a height of 5 cm.
Calculate the area of the rectangle using Python.
Formula
Area = base × height
Python Program
# Problem data
base = 8
height = 5
# Area calculation
area = base * height
# Print the result
print("The area of the rectangle is:", area, "cm²") # this is one way to concatenate strings
Output
The area of the rectangle is: 40 cm²
Version with user input
base = float(input("Enter the base of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
area = base * height
print("The area of the rectangle is:", area, "cm²")
Want to try other problems?
Here are some ideas:
- Perimeter of a polygon
- Area of a triangle or a circle
- Check if a triangle is a right triangle
- Volume of a cube or a pyramid