Python Program to find area of square

A square is a closed two-dimensional figure having 4 equal sides. Each angle of a square is 90 degrees. The area of a square is the space enclosed within its four sides. In this problem, we are given the side of a square, and we have to find the area of the square. In this tutorial, we are going to find the area of a given square in Python using different approaches.

Formula

The formula to calculate the area of a square is:

Area = side × side = side²

Examples

Example 1

Input: side = 6 units
Output: 36 square units

Using the formula to calculate the area of the square: side × side = 6 × 6 = 36 square units

Example 2

Input: 0
Output: 0

If the side length is zero, the square does not exist as a two-dimensional figure, and hence, there is no area.

Example 3

Input: side = 12 units
Output: 144 square units

Using the formula to calculate the area of the square: side × side = 12 × 12 = 144 square units.

Using Direct Formula

We use the direct formula to calculate the area of the square. The formula for calculating the area is: Area = side × side. After calculating the area, we return the result ?

side = 5
area = side * side
print(f"The area of the square with side {side} is: {area}")
The area of the square with side 5 is: 25

Using a Function

We use a function to calculate the area of the square. The logic and formula remain the same, but encapsulating the logic in a function makes it reusable and more modular ?

def calculate_area(side):
    return side * side

side = 6
area = calculate_area(side)
print(f"The area of the square with side length {side} is: {area}")
The area of the square with side length 6 is: 36

Using Exponentiation Operator

Python provides the exponentiation operator (**) to calculate powers. Since area = side², we can use this operator ?

def calculate_area_power(side):
    return side ** 2

side = 8
area = calculate_area_power(side)
print(f"The area of the square with side {side} is: {area}")
The area of the square with side 8 is: 64

Comparison

Method Formula Best For
Direct multiplication side * side Simple calculations
Function approach side * side Reusable code
Exponentiation side ** 2 Mathematical clarity

Conclusion

Calculating the area of a square in Python is straightforward using the formula side × side. Use functions for reusable code or the exponentiation operator for mathematical clarity.

Updated on: 2026-03-27T16:51:30+05:30

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements