Use area = length * width to calculate a rectangle’s area in Python. This interactive program accepts decimal dimensions, multiplies them, and prints the result:
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 the user and returns what they type as text.float(...)converts that text to a number that can include a decimal.- The program stores each converted dimension in a clearly named variable:
lengthandwidth. length * widthapplies the rectangle area formula and stores the result inarea.print()displays a readable message. Thefbefore the string makes it an f-string, so Python inserts the value ofareawhere it appears inside braces.
Try an example
Enter 5 for the length and 3 for the width. The program prints The area of the rectangle is 15.0 square units. The decimal point appears because float represents the inputs as floating-point numbers.
As an Amazon Associate I earn from qualifying purchases.
Area uses square units. If the dimensions are measured in centimetres, the result is in square centimetres; if they are measured in metres, it is in square metres. The program does not ask for or infer a unit, so keep both dimensions in the same unit.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteUse two decimal places in the displayed result
To display the result with two digits after the decimal point, change the last line to:
#1 Best Overall
print(f"The area of the rectangle is {area:.2f} square units.")
The :.2f format rounds the displayed value to two decimal places. It changes how the number is shown, not the value stored in area.
Make the calculation reusable with a function
If another part of your program needs to calculate rectangle areas, put the multiplication in a function:
def rectangle_area(length, width):
return length * width
area = rectangle_area(5, 3)
print(area)
The function receives two values and returns their product. For a one-off calculation, the interactive program is simpler; a function is useful when you want to call the same calculation from different places.
Input limitations
This short program assumes the user enters numbers. Text such as five cannot be converted by float() and raises a ValueError. It also does not check that dimensions are positive; add input validation if your program needs to reject invalid measurements.
Rank #3
Run the code
Run the code in a Python interpreter, or save it in a text editor as a Python file and execute that file with your Python installation. The official Python tutorial’s interpreter section explains the interpreter and its use for trying examples; its tutorial introduction discusses preparing more involved programs in a text editor. The calculation uses Python’s multiplication operator, described in the informal introduction. For details on print() and f-strings, see the Python input and output tutorial.
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.




