What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use Python’s * operator to multiply a rectangle’s length by its width. This short program accepts decimal dimensions and prints the area:
length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
area = length * width
print(f"The area of the rectangle is {area} square units.")
How the program works
input()prompts for a dimension and returns what the user typed as text.float(...)converts that text into a number, allowing decimal measurements.- The program stores the converted values in
lengthandwidth. length * widthapplies the rectangle-area formula: area equals length multiplied by width.print()displays the result. The f-string places the value ofareainto the output sentence.
For inputs of 5 and 3, the program prints The area of the rectangle is 15.0 square units. The .0 appears because float represents the entered values as floating-point numbers. Python’s tutorial covers arithmetic operators and assigning values to variables; its input/output documentation explains printing and formatted string literals.
Choose the units and output precision
The program does not determine the measurement unit. If the dimensions are in centimetres, the result is in square centimetres; if they are in metres, it is in square metres.
To show two digits after the decimal point, replace the final line with:
#1 Best Overall
print(f"The area of the rectangle is {area:.2f} square units.")
This formats the displayed value to two decimal places; it does not change the value stored in area.
Use a function when you need to reuse the calculation
For a one-off script, the four-line version is straightforward. If another part of a program needs to calculate rectangle areas, define a function:
def rectangle_area(length, width):
return length * width
Call it with dimensions and store or display the returned value:
area = rectangle_area(5, 3)
print(area)
The function receives two values and returns their product; it does not prompt for input or print a result by itself. Delft University of Technology’s 2025 course resource uses this same function pattern for rectangle area: Functions — Python for Civil Engineers.
Rank #3
What the basic program does not check
- If a user enters text that cannot be converted to a number,
float()raises a conversion error and the script stops. - The script does not check that dimensions are positive. Add validation if your application must reject zero or negative measurements.
- It treats both inputs as numbers only; it does not attach or convert measurement units.
You can run the example in a Python interpreter. The official Python Tutorial describes using the interpreter for hands-on examples and a text editor when preparing longer programs.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

