Signup/Sign In

Python Program to calculate area of a square

In this tutorial, we will learn how to do a simple program of calculating the area of a square in python. The area is defined as the total space a shape occupies. It is measured in square units like cm², m², km² depending on the unit of the dimensions. The formula for calculating the area of a square is given as

The formula for Area of Square

Area of a square= Side x Side

Area= Side²

For example, following as input and give the output accordingly.

Input- 2.5

Output- 6.25

Input- 5

Output- 25

Here are two simple methods for calculating and printing the area of a square where the measurement of a side is given by the user.

  1. Using multiplication operator (*)
  2. Using pow() function

Approach 1: Using multiplication operator (*)

Given below is a simple program for calculating area using the multiplication operator (*). The input is taken as float and the area is calculated up to 4 decimal places. We will use the "%.4f" specifier for getting 4 digits after the decimal point. In "%.4f" the number after the dot is used to indicate the decimal places and f specifies float.


Step 1- Take input of side from user

Step 2 - Calculate area

Step 3- Print area using "%.4f"

Python Program

Look at the program to understand the implementation of the above-mentioned approach.

#area of square
s=float(input("Enter side of square"))
print("Area of square=",'%.4f'%area)

Enter side of square3.2
Area of square= 10.2400

Approach 2: Using pow() function

pow() is a predefined math function in python which returns the value of x to the power y. To know more about pow() and other built-in math functions. I advise you to read the article on Python math function.


Step 1- Define a function area_square() to calculate area Take input of side from user

Step 2 - Call pow() and set parameters as n,2 to calculate the area

Step 3- Take input from the user

Step 4- Call area_square() and pass input as a parameter

Step 5- Print the area

Python Program

Look at the program to understand the implementation of the above-mentioned approach.

def area_square(n):      
    area = pow(n,2)    
    return area
num=float(input("Enter number") )
print("Sum of digits",area_square(num))

Enter side of square2.4
Area of square= 5.7600


In this tutorial, we learned how to calculate the area of a square using 2 approaches. One, by using simple statements for multiplication and printing the output. Two, using a predefined math function called pow(). You can also define a function to calculate area by simply using the code from the first approach.