This article is created to cover some programs in Python, to find and print area of a square based on the length of its side, entered by user at run-time. Here are the list of programs: Show
Formula to Find Area of SquareTo find area of a square, use: Here area indicates to area value and len indicates to length value of square Find Area of SquareTo find area of a square in Python, you have to ask from user to enter the side length. Now use the formula to find and print the area value as shown in the program given below: print("Enter the Side Length of Square: ") l = float(input()) a = l*l print("\nArea = ", a) Here is the initial output produced by this program: Now supply the input say 6 as side length of square to find and print its area based on the given value of its side length as shown in the snapshot given below: Find Area of Square using FunctionThis program is created using a user-defined function named areaOfSquare(). This function takes side length as its argument and returns the area value. The return value of this function gets initialized to a. Now print the value of a as output. That's it: def areaOfSquare(s): return s*s print("Enter the Side Length of Square: ", end="") l = float(input()) a = areaOfSquare(l) print("\nArea = {:.2f}".format(a)) Here is its sample run with user input, 43.23 as side length of square: Find Area of Square using ClassThis is the last program on finding area of a square, using class and object, an object-oriented feature of Python. The program is little similar to the program created using function. The only difference is, we've to call the member function areaOfSquare() of class using its object through dot (.) operator. Note - Before accessing any member function of a class using an object say obj, the properties of class must be assigned to it. Therefore through the statement, obj = CodesCracker(), we've done the job. class CodesCracker: def areaOfSquare(self, s): return s*s print("Enter the Side Length of Square: ", end="") l = float(input()) obj = CodesCracker() a = obj.areaOfSquare(l) print("\nArea = {:.2f}".format(a)) This program produces the same output as of previous program. Same Program in Other Languages
Python Online Test « Previous Program Next Program » This is a Python Program to find the area of a rectangle using classes. Problem Description The program takes the length and breadth from the user and finds the area of the rectangle using classes. Problem Solution 1. Take the value of length and breadth from the user. Program/Source Code Here is the source code of the Python Program to take the length and breadth from the user and find the area of the rectangle. The program output is also shown below. class rectangle(): def __init__(self,breadth,length): self.breadth=breadth self.length=length def area(self): return self.breadth*self.length a=int(input("Enter length of rectangle: ")) b=int(input("Enter breadth of rectangle: ")) obj=rectangle(a,b) print("Area of rectangle:",obj.area()) print() Program Explanation 1. User must enter the value of length and breadth. Runtime Test Cases Case 1: Enter length of rectangle: 4 Enter breadth of rectangle: 5 Area of rectangle: 20 Case 2: Enter length of rectangle: 15 Enter breadth of rectangle: 13 Area of rectangle: 195 Sanfoundry Global Education & Learning Series – Python Programs. To practice all Python programs, here is complete set of 150+ Python Problems and Solutions. Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses. How do you code an area in Python?Python program to find area and perimeter of a rectangle. Firstly, we will take input from the user for length and breadth using the input() function.. Now, we will calculate the area of a rectangle by using the formula Area = w * h.. Next, we are calculating the perimeter of a rectangle Perimeter = 2 * (w + h). What does square () do in Python?Square Roots in Mathematics
The Python ** operator is used for calculating the power of a number. In this case, 5 squared, or 5 to the power of 2, is 25. The square root, then, is the number n, which when multiplied by itself yields the square, x. In this example, n, the square root, is 5.
How do you program the area of a rectangle in Python?Python Program. width=5.. height=10.. area=width*height.. print("Area of rectangle="+str(area)). |