Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 1: Compute the sum ,
subtraction, multiplication, division
and exponent by given variable.
x=int(input('Enter the value of a'))
y=int(input('Enter the value of b'))
print("The addition of a and b is",x+y)
print("The subtraction of a and b is",x-y)
print("The quotient a and b is",x/y)
print("a multiplied by b is",x*y)
print("a to the power b is",x**y)
Output:
1 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 2: Find the areas of triangle
,circle, trapezoid, rectangle, square,
parallelogram
#area of square
s=int(input('Enter the side of square'))
print("Area of Square is",s*s)
print()
#area of triangle
a=int(input('Enter the value of base of the triangle'))
b=int(input('Enter the value of height of the triangle'))
print("Area of triangle is",1/2*a*b)
print()
#area of rectangle
l=int(input('Enter the value of length of the rectangle'))
b=int(input('Enter the value of breadth of the rectangle'))
print("Area of Rectangle ",l*b)
print()
#area of trapezoid [formula (a+b/2)*h , where, a and b sum
of two parallel sides, h = height]
a=int(input('Enter the value of base of one side of Trapezoid'))
b=int(input('Enter the value of base of second side of
Trapezoid'))
h=int(input('Enter the value of height of Trapezoid'))
print("Area of Trapezoid is",a+b/2* h)
print()
#area of parallelogram
2 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
b=int(input('Enter the value of base of the parallelogram'))
h=int(input('Enter the value of height of the parallelogram'))
print("Area of Parallelogram is",b*h)
print()
#area of circle
Pi=3.14
r=float(input('Enter the value of radius of circle'))
area= 2*Pi*r*r
print("Area of Circle is",area)
Output
3 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 3: Compute the volume of the
following 3D shapes cube, cylinder, cone
and sphere
#Volume of cube
s=int(input('Enter the side of cube'))
print("The volume of cube is",s*s*s)
#Volume of cylinder
a=int(input('Enter the area of Cyclinder'))
h=int(input('Enter the height of Cyclinder'))
print("The volume of cylinder is",a*h)
#Volume of cone
a=int(input('Enter the area of Cone'))
h=int(input('Enter the height of Cone'))
print("The volume of cone is",1/3*a*h)
#Volume of sphere
pi=3.14
r=int(input('Enter the value of radius of Sphere'))
print("The volume of sphere is",4/3*pi*r*r*r)
4 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
Output
5 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 4: Compute and print roots of
quadratic equation ax2+bx+c=0, where
the values of a, b and c are the input by
the user.
#import complex math module
import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))
#calculate the discriminant
d = (b**2) - (4*a*c)
#find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solutions are {0} and {1}'.format(sol1,sol2))
Output
6 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 5: Print numbers up to N
which are not divisible by 3,6,9 e.g.
1,2,4,5,7.
N = int (input("Enter value of N : "))
n=1
print("Numbers not divisible by 3")
while n <= N:
if n % 3 != 0:
print(n)
n = n+1
Output
7 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 6: Write a program to
determine whether a triangle is isosceles
or not
x = int(input("enter first side of triangle"))
y = int(input("enter second side of triangle"))
z = int(input("enter third side of triangle"))
print()
ans = bool(False)
if x == y or x == z or y == z :
print("The triangle is isosceles")
else :
print("The triangle is not isosceles")
Output
8 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 7: Print multiplication table
of no. input by the user
num = int(input("Enter the number to print its table"))
i=1
res = 0
while(i<=10):
res = i * num
print(num,"*",i,"=",res)
i = i+1
Output
9 | Page
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 8: Compute sum of natural
from one to n numbers.
num = int(input("Enter the number"))
i=1
res = 0
while(i<=num):
res += i
i = i+1
print("The sum till",num,"is",res)
Output
10 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 9: Print Fibonacci series up to
n numbers e.g.0 11 2 3 5 8 13….n.
n= int(input("How many terms u want to display "))
n1, n2 = 0, 1
count = 0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
Output
11 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 10: Compute factorial of a
given number.
num = int(input("Enter the number of which you want
factorial"))
i = num - 1
res = 0
res2 = 1
while(i >= 1) :
num *= i
i = i-1
print("The factorial is",num)
Output:
12 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 11: Count occurrences of digit
5 in a given no. input by the user in
python
num = int(input("Enter a number"))
str = str(num)
i=0
count = 0
len = len(str)
while(i < len) :
if str[i] == "5" :
count = count + 1
i = i+1
print(count)
Output:
13 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 12: Print Geometric and
harmonic means of a series input by the
user
a=int(input('Enter 1st no.'))
b=int(input('Enter 2nd no.'))
GM=(a*b)**0.5
HM=(2*a*b)/(a+b)
print('Geometric mean of' ,a, 'and' ,b, '=',GM)
print('Harmonic mean of' ,a, 'and' ,b, '=',HM)
Output
14 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 13: Evaluate the following
expressions:
a) x-x 2 /2!+x3 /3!- x 4 /4!+… xn /n!
b) x-x 3 /3!+x5 /5!- x 7 /7!+… xn /n!
x=int(input("Enter value of x: "))
n=int(input("Enter the nth element: "))
sum1=0
sum2=0
sum3=0
sum4=0
for i in range(1,n+1,2):
a=i
k=1
while a>0:
k=a*k
a=a-1
sum1=x**i
k=sum1
for I in range (2,n+1,2):
a=i
k=1
while a>0:
k=a*k
a=a-1
sum2=x**i/k+sum2
print("Expression 1:sum=",sum1-sum2)
for j in range(1,n+1,4):
a=j
k=1
15 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
while a>0:
k=a*k
a=a-1
sum3=x**j/k+sum3
for j in range (3,n+1,4):
a=j
k=1
while a>0:
k=a*k
a=a-1
sum4=x**j/k+sum4
print("Expression 2: sum=", sum3-sum4)
Output
16 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 14: Print all possible
combinations of 4, 5, and 6
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
if(i !=j and j !=k and i !=k):
print(L[i],L[j],L[k])
comb([4,5,6])
Output
17 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 15: Determine prime number
within a specific range
min = int(input("Enter the lower range : "))
max = int(input("Enter the upper range : "))
for n in range(min,max + 1):
if n > 1:
for i in range(2,n):
if (n % i) == 0:
break
else:
print(n)
Output
18 | P a g e