Python

Python Program to Calculate Area and Perimeter of Square2 min read

To find the perimeter of a square, just add up all the lengths of the sides.

The area of a square: To find the area of a square, multiply the lengths of two sides together. Another way to say this is to say “square the length of a side.




In this example, I’ll show how to calculate area and perimeter of rectangle in C#.

Python Code:

This code defines a program in Python that calculates the area and perimeter of a square. The program begins by prompting the user to enter the side of the square using the input function and storing the value in a variable s. It then calculates the area of the square by multiplying s by itself and assigns the result to a variable area. It calculates the perimeter by multiplying s by 4 and assigns the result to a variable perimeter. Finally, the program prints the values of area and perimeter to the terminal using the print function.

You may also like: Python Tutorial with Exercises(100+ Examples)

To run this code and see the output, you can follow these steps:

  1. Save the code to a file or type it into a text editor.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the code is saved.
  4. Run the command python filename.py, where filename.py is the name of the file containing the code.
  5. The program will prompt you to enter the side of the square. Type in a value for this variable and press enter.
  6. The program will then calculate the area and perimeter of the square and print the results to the terminal.

For example, if you enter 5 for the side of the square, the output will be:

1 Comment

Leave a Comment