Introduction to Python

Activity 2: Area Calculator

Lesson 4: Variables and Data Types

In this activity, we will build an Area Calculator that can compute the area of three different shapes — a square, a rectangle, and a circle — based on the user's choice.

Shapes and Their Area Formulas

A blue square with a label showing Area = side x side
Square: side²
A green rectangle with a label showing Area = length x width
Rectangle: l × w
An orange circle with a label showing Area = pi x radius squared
Circle: π × r²

Area Formulas

ShapeFormulaVariables
SquareArea = side × sideside
RectangleArea = length × widthlength, width
CircleArea = π × radius²radius (use math.pi for π)

Writing the Script

# Area Calculator import math print("Area Calculator") print("1. Square") print("2. Rectangle") print("3. Circle") choice = int(input("Choose a shape (1/2/3): ")) if choice == 1: side = float(input("Enter the side length: ")) area = side ** 2 print("Area of the square: " + str(area)) elif choice == 2: length = float(input("Enter the length: ")) width = float(input("Enter the width: ")) area = length * width print("Area of the rectangle: " + str(area)) elif choice == 3: radius = float(input("Enter the radius: ")) area = math.pi * radius ** 2 print("Area of the circle: " + str(round(area, 2))) else: print("Invalid choice. Please enter 1, 2, or 3.")

Understanding the Code

Code Breakdown

LineWhat it Does
import mathImports the math module to access math.pi
choice = int(input(...))Gets the user's shape choice as an integer
side ** 2Calculates the square of the side length
math.piThe value of π (approximately 3.14159)
round(area, 2)Rounds the result to 2 decimal places

Testing the Program

  • Run with choice = 1, side = 5 → Area should be 25.0
  • Run with choice = 2, length = 6, width = 4 → Area should be 24.0
  • Run with choice = 3, radius = 7 → Area should be approximately 153.94
Python console showing three test runs of the area calculator — one for square, one for rectangle, and one for circle with correct results printed
Save your file with the name Area Calculator before moving on!

Subscribe to our newsletter.

Get updates to news and events.