Area of Square in Python. Writing a Python program to calculate the area of a square is a fundamental exercise that can help beginners understand the basics of mathematical operations and user input in Python.
Table of Contents
Formula of Area of Square
The formula for the area of the square is-
Area = side x side
Writing the Python Code for Area of Square
Open your text/code editor and create a new file named ‘area_square.py’. Then follow these steps to write your program.
Prompt the User for the Side Length
First, you need to get the length of the square’s side from the user. You can use the input()
function for this purpose and since input()
return a string, You will need to convert this input into a number (float or integer) to perform mathematical operations.
side_length = float(input("Enter the length of a side of a square "))
Calculate the Area
Next, calculate the area of the square using the formula mentioned earlier.
area = side_length * side_length
Printing the Result
Finally, display the result using the print()
function
print("The area of square is = ",area)
Python Program for Area of Square
side_length = float(input("Enter the length of a side of a square "))
area = side_length * side_length
print("The area of square is = ",area)
Congratulations!! You have just written a simple Python program to calculate the area of a square. This exercise not only demonstrates how to perform mathematical calculations in Python but also introduces basic concepts such as user input and output, variable usage, and type conversion.
Happy Coding & Learning
3 thoughts on “Area of Square in Python”