Practical No.
1
TITLE : Program to calculate area of triangle, rectangle and circle.
Problem Description : Program calculates area of triangle, rectangle and circle and displays
area of triangle, rectangle and circle.
THEORY/ ANALYSIS :
Python Operators
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Python Comparison Operators
Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Description Example
and Returns True if both statements x < 5 and x < 10
are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns False not(x < 5 and x < 10)
if the result is true
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually
the same object, with the same memory location:
Operator Description Example
is Returns True if both variables x is y
are the same object
is not Returns True if both variables x is not y
are not the same object
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
in Returns True if a sequence with the specified value is present in the x in y
object
not Returns True if a sequence with the specified value is not present in x not in
in the object y
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill Shift left by pushing zeros in from the right and let the leftmost bits fall
left shift off
>> Signed Shift right by pushing copies of the leftmost bit in from the left, and let
right shift the rightmost bits fall off
Python print() Function
The print() function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before
written to the screen.
Python print() function prints the message to the screen or any other standard output device.
Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
Parameters:
value(s) : Any value, and as many as you like. Will be converted to string before printed
sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than
one.Default :’ ‘
end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
file : (Optional) An object with a write method. Default :sys.stdout
flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False).
Default: False
Returns: It returns output to the screen.
Example
Print a message onto the screen:
print("Hello World")
Print a tuple:
x = ("apple", "banana", "cherry")
print(x)
Python input() Function
Python input() function is used to take user input. By default, it returns the user input in form of
a string.
Syntax
input(prompt)
Parameter:
Prompt: A String, representing a default message (usually screen) before the input. However, it
is optional to have a prompt message.
Return: input() returns a string object. Even if the inputted value is an integer it converts it into
a string.
Example
# Taking name of the user as input
# and storing it name variable
name = input("Please Enter Your Name “)
Python program to calculate area of circle
Method
1. Read base and height of a triangle using input()orraw_input()
2. Multiply both base and height and divide it with 2
3. Area a = ( base * height ) / 2
4. Print the result.
Program
b = float(input('Enter base of a triangle: '))
h = float(input('Enter height of a triangle: '))
area = (b * h) / 2
print('The area of the triangle is %0.2f' % area)
Program to determine area of rectangle
1. Firstly, we will take input from the user for length and breadth using the input() function.
2. Now, we will calculate the area of a rectangle by using the formula Area = l * b.
3. At last, print the area of a rectangle to see the output.
Program
l = float(input('Enter the length of a Rectangle: '))
b = float(input('Enter the breadth of a Rectangle: '))
Area = l * b
print("Area of a Rectangle is: %.2f" %Area)
Python Program to calculate area of circle
1. Firstly, we will take input from the user using the input() function for radius and store it
in a variable.
2. We can use constant to store the value of „pi‟.
3. Now, we will calculate the area of a circle by using the formula area = PI * r * r.
4. And at last, print the area of a circle to get the output
Program
PI = 3.14
radius = float(input(' Please Enter the radius of a circle: '))
area = PI * radius * radius
circumference = 2 * PI * radius
print(" Area Of a Circle = %.2f" %area)
print(" Circumference Of a Circle = %.2f" %circumference”)
Runtime Test Cases