Python Programs Related to Squares
Python Program #1 write a python program to accept side of a square and calculate the area of a square
Explanation
==============
In this program will ask the user to enter the side of a square which is of type float and store it into a variable.
Let's take another variable named area and calculate the area of the square by squaring the side to the power of two using the double star operator.
Finally using the print() function call print the area of square
Complete Python Program
==========================
side = float(input("Enter One Side of Square : "))
area = side ** 2
print("Area of Square is : %.2f" %area)
Output
===========
Enter One Side of Square : 12.33434
Area of Square is : 152.14
Python Program #2 write a python program to take input from the user to find the area and perimeter of square
Explanation
==============
In this program let's ask the user to enter the side of a square using input function call and then convert it into float.
Take Another variable named as area and calculate the area of the square by raising the side variable to power of two.
Take another variable named perimeter and calculate perimeter by multiplying side variable it with 4
Finally use the print() function call to print the area and perimeter of the square.
Complete Python Program
=========================
side = float(input("Enter One Side of Square : "))
area = side ** 2
perimeter = 4 * side
print("Area of Square is : %.2f" %area)
print("Perimeter of Square is : %.2f" %perimeter)
Output
===========
Enter One Side of Square : 8.456
Area of Square is : 71.50
Perimeter of Square is : 33.82
Python Program #3 write a python function to calculate area of square
Explanation
==============
Let's ask the user to enter the side of the square using input() function call. Define the function to find the area of square by raising the side of square to 2.
Area = side ^ 2
Call the function to calculate the area of square and store the area into new variable, finally print the area of square using print() function call
Complete Python Program
======================
def area_of_square(s):
return s ** 2
side = float(input("Enter One Side of Square : "))
area = area_of_square(side)
print("Area of Square is : %.2f" %area)
Output
===========
Enter One Side of Square : 23.223232
Area of Square is : 539.32
Python Programs Related to Rectangle
Python Program #1 write a python program to obtain length and breadth of a rectangle and calculate its area
Explanation
======================
Take two variables from the user i.e, length and breadth of rectangle. Ask the user to enter these two values and convert it into floating point numbers.
Area of Rectangle = length * breadth
Use the above formula to calculate the area of the rectangle and using print() function call print the area of the rectangle.
Complete Python Program
===============================
length = float(input("Enter Length of Rectangle : "))
breadth = float(input("Enter Breadth of Rectangle : "))
area = length * breadth
print("Area of Rectangle is : %.2f" %area)
Output
==============
Enter Length of Rectangle : 12.9
Enter Breadth of Rectangle : 93.23
Area of Rectangle is : 1202.67
Python Program #2 write a python program to calculate area of a rectangle with length = 20 and breadth=12
Explanation
================
Take to variables of rectangle that is length and breadth and initialize it to 20 and 12
Area of a triangle is given by the formula length multiplied by breadth. Take another variable named area and find the product of length and breadth and story into area variable.
Finally using the print function call print the area of the given rectangle.
Complete Python Program
======================
length = 20
breadth = 12
area = length * breadth
print("Area of Rectangle is : %.2f" %area)
Output
=================
Area of Rectangle is : 240.00
Python Program #3 : write a python program to take length and breadth of rectangle and print area and perimeter
Explanation
============
Take two variables named as length and breadth of the rectangle and ask the user to enter these two values using the input function call and then convert it into a float.
Now take another to variables named as area and perimeter and calculate the area of rectangle by multiplying product of length and breadth
Now to calculate the perimeter of a rectangle find the sum of length and breadth and multiplied the sum by 2 this will give the perimeter of a triangle.
Use the print function call to print but the variables area and perimeter of a rectangle
Complete Python Program
==============================
length = float(input("Enter Length of Rectangle : "))
breadth = float(input("Enter Breadth of Rectangle : "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("Area of Rectangle is : %.2f" %area)
print("Perimeter of Rectangle is : %.2f" %perimeter)
Output
================
Enter Length of Rectangle : 40.1
Enter Breadth of Rectangle : 67.90
Area of Rectangle is : 2722.79
Perimeter of Rectangle is : 216.00
Python Program #4 : write a python code to calculate the area of a rectangle using parameterized and default constructors
Explanation
===================
Taking a class named as rectangle and using the constructor init() function to initialize the length and breadth of a rectangle to zero and in the body initialize them with actual values when the user calls parameterized constructor.
Take another function named as area and return the product of length and breadth as shown below
In the main program, take the first object of the rectangle without passing any arguments to the rectangle class. This is said to call the default constructor now using print function print the area of a rectangle it will print zero.
Complete Python Program
==============================
class rectangle:
def __init__(self, length=0, breadth=0):
self.length = length
self.breadth = breadth
def area(self):
return self.length * self.breadth
object1 = rectangle()
area = object1.area()
print("Area of Rectangle Using Default Constructor is : %.2f" %area)
l = float(input("Enter Length of Rectangle : "))
b = float(input("Enter Breadth of Rectangle : "))
object2 = rectangle(l, b)
area = object2.area()
print("Area of Rectangle Using Parameterized Constructor is : %.2f" % area)
Output
================
Area of Rectangle Using Default Constructor is : 0.00
Enter Length of Rectangle : 12
Enter Breadth of Rectangle : 2
Area of Rectangle Using Parameterized Constructor is : 24.00
Python Programs Related to Triangle
Python Program #1: write a python program to accept the base and height of a triangle and calculate area of triangle
Explanation
====================
Take two floating point variables base and height, using input() function call prompt the user and get these values as input.
Now to calculate the area of the triangle multiply base and height and divide the product by 2.
Using print() function call print the area of the triangle.
Complete Python Program
=======================
base = float(input("Enter Base of Triangle : "))
height = float(input("Enter Height of Triangle : "))
area = (base * height) / 2
print("Area of of Triangle is : %.2f" % area)
Output
==================
Enter Base of Triangle : 5
Enter Height of Triangle : 2
Area of of Triangle is : 5.00
Python Program #2: write a python program to calculate perimeter/circumference and area of shapes such as triangle
Explanation
==================
Take a b and c three floating point variables and ask a user to enter the first, second and third sides of a triangle.
Take another variable named perimeter and and all the three sides of the triangle a, b and c, this will be your perimeter of a triangle.
Now using the print function call print the perimeter of a triangle.
Complete Python Program
===============================
a = float(input("Enter First Side of Triangle : "))
b = float(input("Enter Second Side of Triangle : "))
c = float(input("Enter Third Side of Triangle : "))
perimeter = a + b + c
print("Perimeter of of Triangle is : %.2f" % perimeter)
Output
================
Enter First Side of Triangle : 1
Enter Second Side of Triangle : 2
Enter Third Side of Triangle : 3
Perimeter of of Triangle is : 6.00
Python Program #3: write a python script to find the area of a triangle whose sides are provided by a user
Explanation
==================
Again take three floating point variables a, b and c. Using the input function call as the user to enter first second and third side effect Triangle then convert it into float.
First calculate the semi perimeter of a triangle by finding the sum of all the three sides and dividing it by 2.
Now using Heron's formula subtract all the sides by semi and multiplied by semi perimeter and find the sqrt of the product by raising it to power of 0.5.
Using the print function call print the area of a triangle
Complete Python Program
================================
a = float(input("Enter first side of Triangle : "))
b = float(input("Enter second side of Triangle : "))
c = float(input("Enter third side of Triangle : "))
semi_perimeter = (a + b + c) / 2
area = (semi_perimeter * (semi_perimeter - a) * (semi_perimeter - b) * (semi_perimeter - c)) ** 0.5
print("The area of the triangle is: %.2f " %area)
Output
===============
Enter first side of Triangle : 4
Enter second side of Triangle : 5
Enter third side of Triangle : 6
The area of the triangle is: 9.92
0 Comments