PYTHON Assignment
PYTHON Assignment
elif x=="Square":
y=int(input("Enter side of square: "))
area=y*y
print("Area of square is",area)
elif x=="Rectangle":
y = int(input("Enter length of rectangle: "))
z = int(input("Enter breadth of rectangle: "))
area=y*z
print("Area of rectangle is", area)
Input :-
Name of polygon: Square
Enter side of square: 34
Output :-
Area of square is 1156
Discussion :-This program will find the area of a circle, rectangle or square based on the
input given by user under if and elif statement and then will find the area respectively under
the conditional statements.
Conclusion :- We have successfully calculated the area of square of side 34 unit as 1156
unit2.
PROGRAM :- 2
Program :- Write a python program to print Fibonacci series up to a certain limit (use
'while').
Platform :- IDLE (Python 3.11 64-bit)
Code :-
# Program to display the Fibonacci series up to n-th term
terms = int(input("How many terms:- "))
# first two terms
x1 = 0
x2 = 1
y=0
# check if the number of terms is valid
if terms <= 0:
print("Please enter a positive integer")
# if there is only one term, return x1
elif terms == 1:
print("Fibonacci sequence upto",terms,":",x1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while y < terms:
print(x1)
nth = x1 + x2
# update values
x1 = x2
x2 = nth
y += 1
Input :-
How many terms:- 10
Output :-
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34
Discussion :-
The Fibonacci sequence is a set of integers that starts with a zero, followed by a one, then
by another one, and then by a series of steadily increasing numbers. The sequence follows
the rule that each number is equal to the sum of the preceding two numbers.
Conclusion :- By giving the input of 10 terms we get the respective Fibonacci series as
per the code.
PROGRAM :- 3
Program:- Write a python program to print the Pascal triangle.
Platform :-IDLE (Python 3.11 64-bit)
Code :-
n = int(input("Enter row number : "))
list1=[]
for i in range(n):
temp_list=[]
for j in range(i+1):
if j==0 or j==i:
temp_list.append(1)
else:
temp_list.append(list1[i-1][j-1] + list1[i-1][j])
list1.append(temp_list)
for i in range(n):
for j in range(n-i-1):
print(format(" ","<3"), end="")
for j in range(i+1):
print(format(list1[i][j],"<6") , end="")
print()
Input :-
Enter row number : 7
Output :-
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Discussion :- Pascal's triangle is the triangular array of numbers that begins with 1 on
the top and with 1's running down the two sides of a triangle. Each new number lies
between two numbers and below them, and its value is the sum of the two numbers above
it.
Conclusion :-The above code gave the pascal triangle as per the user input of seven
rows.
PROGRAM :- 4
Program:- Write a python program to find HCF (GCD) of two numbers.
Platform :-IDLE (Python 3.11 64-bit)
Code :-
x = int(input("Enter first number : "))
y = int(input("Enter second number : "))
if x>y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if (y%i == 0) and (x%i==0):
hcf = i
print("HCF of",x,"and",y,"is",hcf)
Input :-
Enter first number : 15
Enter second number : 25
Output :-
HCF of 15 and 25 is 5
Discussion :-The greatest common divisor of two or more numbers, which are not all
zero, is the largest commonfactor that divides each of the number, exactly. It is also known
as highest common factor. For e.g. the HCF of 6 and 8 is 2 , since both numbers are
completely divided by 2.
Conclusion :-Using the above program we successfully calculated the HCF of 15 and 25.
PROGRAM :- 5
Program :- Write a python program to find LCM of two numbers.
Platform :-IDLE (Python 3.11 64-bit)
Code :-
x = int(input("Enter first number : "))
y = int(input("Enter second number : "))
if x>y:
greater = x
else:
greater = y
value=greater
while(True):
if(greater % x == 0) and (greater % y == 0):
print("LCM of",x,"and",y,"is",greater)
break
else:
greater=greater+value
Input :-
Enter first number : 23
Enter second number : 12
Output :-
LCM of 23 and 12 is 276
Discussion :-LCM stands for least common multiple. The least common multiple of two
or more numbers is the smallest number that is a common multiple of all of them. For e.g.
the lcm of 3, 4, 5 is 60.
Conclusion :-As per the user input of numbers 23 and 12 the program gave the LCM
276.
PROGRAM :- 6
Program :- Write Python programs to illustrate the various functions of the "Math"
module, "Statistics" module in Python.
Platform :-IDLE (Python 3.11 64-bit)
Code :-
a = min(5, 10, 25)
b = max(5, 10, 25)
print(a)
print(b)
c = abs(-7.25)
print(c)
d = pow(4, 3)
print(d)
import math
e = math.sqrt(64)
print(e)
f = math.ceil(1.4)
g = math.floor(1.4)
print(f)
print(g)
h = math.pi
print(h)
Output :-
5
25
7.25
64
8.0
2
1
3.141592653589793
Code :-
import statistics
print(statistics.mean([1, 3, 5, 7, 9, 11, 13]))
print(statistics.median([1, 3, 5, 7, 9, 11]))
print(statistics.mode([1, 1, -3, 3, 7, -9]))
print(statistics.stdev([1, 3, 5, 7, 9, 11]))
Output :-
7
6.0
1
3.7416573867739413
Discussion :-math is a built-in module in the Python 3 standard library that provides
standard mathematical constants and functions. We can use the math module to perform
various mathematical calculations, such as numeric, trigonometric, logarithmic, and
exponential calculations.
statistics module provides functions for calculating mathematical statistics of numeric (Real -
valued) data. There are various functions in statistics module like mean, median, mode, etc.
Conclusion :-The above codes gave the output of some functions of math and statistics
modules.
PROGRAM :- 7
Program :- Write a Python program to count number of vowels using sets in given string.
Platform :-IDLE (Python 3.11 64-bit)
Code :-
text = str(input("Enter text you want to count vowels in: "))
thisset = {"a","e","i","o","u","A","E","I","O","U"}
count = 0
for x in text:
if x in thisset:
count +=1
print("Number of vowels:",count)
Input :-
Enter text you want to count vowels in: This is my seventh python program.
Output :-
Number of vowels: 7
Discussion :-
1. The code will take input as a string from user in text variable.
2. A set, thisset is defined which contains the vowels in lower form and capitalised form.
3. Another variable named count is also defined which will store the number of vowels
present in a given string.
4. The for loop will traverse through the given string under the conditional statement to
check for vowels.
5. The output will be stored in count variable and print the number of vowels.
Conclusion :- As per the user input of a string, the code gave the number of vowels
present in the string.
PROGRAM :- 8
Program:- Write a Python program to Remove all duplicates from a given string in Python
Platform :-IDLE (Python 3.11 64-bit)
Code :-
x = str(input("Enter a string : "))
i=0
x1 = ""
for n in x:
if x.index(n) == i:
x1 += n
i +=1
print("String after removing duplicate value is ",x1)
Input :-
Enter a string : aaabbb cccddd eeefff ggghhh
Output :-
String after removing duplicate value is ab cdefgh
Discussion :-
1. The variable x will take input from user in string form.
2. The i variable will act as a reference for duplicate strings and x1 will store the strings
after duplicacy is removed.
3. The new sting without duplicacy is printed.
Conclusion :- The code has removed duplicate values from the user input.
PROGRAM :- 9
Program :- Write a Python program to count positive and negative numbers in a list
Platform :-IDLE (Python 3.11 64-bit)
Code :-
a=0
b=0
thislist = []
mylist = []
Discussion :-
1. The code will take input from user and store in form of a list in thislist variable.
2. The fsum function of the math module calculates the sum of the elements present in
the list.
3. The sum is printed.
Conclusion :-The code gave the required sum of the list given as input.
PROGRAM :- 11
Program :- Write a python program to read a list of 'n' integers (positive and negative) and
create two new lists one having all positive numbers and the other having all negative
numbers from the given list. Print all three lists.
Discussion :-The above code will take admission number as input from user and then
display the information of that student.
Conclusion :- The code gave the information of a student of admission number 101.
PROGRAM :- 14
Program :-Write a python program which contains user defined functions as a 'module' to
calculate area, perimeter or surface area , volume for various shapes like square, cube,
circle, cylinder. The user defined functions should accept the values for calculation as
parameters and calculated values should be returned. Import the module and use
appropriate functions.
Platform :-IDLE (Python 3.11 64-bit)
Code :-
print("Square: 1")
print("Cube : 2")
print("Circle : 3")
print("Cylinder : 4")
x = int(input("Enter your choice : "))
if x == 1:
def square(a):
print("Area of square is :",a*a)
print("Perimeter of square is :",4*a)
return
a = int(input("Enter side of square : "))
square(a)
elif x == 2:
def cube(a):
print("Surface area of cube is :",6*a*a)
print("Volume of cube is :",a*a*a)
return
a = int(input("Enter side of cube : "))
cube(a)
elif x == 3:
def circle(r):
print("Perimeter of circle is :",2*3.14*r)
print("Area of circle is :",3.14*r*r)
return
r = int(input("Enter radius of circle : "))
circle(r)
elif x == 4:
def cylinder(r,h):
print("Surface area of cylinder is :",2*3.14*r*r + 2*3.14*r*h)
print("Volume of cylinder is :",3.14*r*r*h)
return
r = int(input("Enter radius of cyliner : "))
h = int(input("Enter height of cyliner : "))
cylinder(r,h)
Input :-
Square: 1
Cube : 2
Circle : 3
Cylinder : 4
Enter your choice : 4
Enter radius of cyliner : 2
Enter height of cyliner : 5
Output :-
Surface area of cylinder is : 87.92
Volume of cylinder is : 62.800000000000004
Discussion :- The program will take input from user as per the requirement and will give
the required calculation based on input.
Conclusion :-The program gave the surface area and volume of a cylinder of radius 2 unit
and height 5 unit.
PROGRAM :- 15
Program :-Create a menu driven Python program using user defined functions to implement a
calculator to perform
(a) Basic arithmetic operations
(b) log10(x), sin(x) , cos(x)