Sri Aurobindo: Python Lab ME506
Sri Aurobindo: Python Lab ME506
Sri Aurobindo
Institute of Technology, Indore
Department of Mechanical Engineering
Program -1. Write a python program to add two floating numbers (Values are taken from
users).
Ans :
num1 = float(input("Enter
"Enter first number : "
"))
num2 = float(input("Enter
"Enter second number : ""))
Output :
Program -2.
2. Write a python program to perform slicing and indexing on string.
Ans :
Output :
Program -3.
3. Write a python program to calculate area of circle (Values are taken from
users).
Ans :
r = float(input("Enter
"Enter the radius of circle : "
"))
pi = 3.14
a = pi*(r**2)
print(a)
Output :
Program -4. Write a python program to read number and print corresponding day using if-
if
elif-else.
Ans :
n = int(input("Enter
"Enter a number frome 1
1-7 : "))
if n == 1:
print("Sunday")
elif n == 2:
print("Monday")
elif n == 3:
print("Tuesday")
elif n == 4:
print("Wednesday")
elif n == 5:
print("Thursday")
elif n == 6:
print("Friday")
elif n == 7:
print("Saturday")
else:
print("Enter
"Enter a right number."
number.")
Output :
Program -5. Write a python program to find out largest number among three numbers
(Values are taken from users).
Ans :
a = float(input("Enter
"Enter 1st number : "
"))
b = float(input("Enter
"Enter 2nd number : "
"))
c = float(input("Enter
"Enter 3rd number : "
"))
Output :
Program -6. Write a python program to check whether the given number is even or odd
(Values are taken from users).
Ans :
n = int(input("Enter
"Enter a number : "
"))
if n%2 == 0:
print(f"{n} is even number"
number")
else:
print(f"{n} is odd number"
number")
Output :
Program -7.
7. Write a pythons program to print first 10 natural number numbers using while
loop
Ans :
i=1
while i<=10:
print(i)
i+=1
Output :
Program -8.
8. Write a python program to print table of any number using for loop(Values are
taken from users) example:- 2*1=2
n = int(input("Enter
"Enter the Number : "
"))
for i in range(1,11):
print(n*i)
Output :
Program -9.
9. Write a python program to perform various condition i.e. if else, continue,
break , break else & pass statement using for loop(Values are taken from users).
Ans :
for i in [1,2,3,4,5]:
if i==3:
break
else:
pass
print (i)
else:
print ("for
"for loop is done"
done")
print ("Outside
"Outside the for loop"
loop")
for i in [1,2,3,4,5]:
if i==3:
continue
print (i)
else:
print ("for
"for loop is done"
done")
print ("Outside
"Outside the for loop"
loop")
Output :
PROGRAM -10.
10. Write a python program to illustrate a list & perform
append,remove,pop,extend and reverse operation on list.
lis = [2, 1, 3, 5, 4, 3, 8,
, 6, 9]
lis.append("Ravi")
print(lis)
lis.remove("Ravi")
print(lis)
lis.reverse()
print(lis)
del lis[2 : 4]
print(lis)
lis.pop(2)
lis2 = [16, 14, 13]
lis.extend(lis2)
print(lis)
Output :
PROGRAM -11. 11. Write a python program to create tuple and perform
slicing,concatenation,append and remove operation on it.
Ans :
# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('1st', '2nd', '3rd'
'3rd')
Tuple3 = Tuple1 + Tuple2
print(Tuple3)
Tuple3.append("Ravi")
print(Tuple3)
Tuple3.remove("Ravi")
print(Tuple3)
Output :
PROGRAM -12. 12. Write a python program to create set and perform union,intersection and
symmetric difference operation on it.
Ans :
A = {0, 2, 4, 6, 8};
B = {1, 2, 3, 4, 5};
# union
print("Union :", A | B)
# intersection
print("Intersection :",
, A & B)
# symmetric difference
print("Symmetric
"Symmetric difference :"
:", A ^ B)
Output :
PROGRAM -13. 13. Write a python program to create dictionary and perform insert, replace,
keys,pop and popitem operation on it.
Ans :
res = dict1.popitem()
print(str(res))
print(dict1.pop('Akash'))
dict1['Mohit'] = '9'
del dict1["Nikhil"]
Output :
PROGRAM -14. 14. Write a python program to add two integer values using functions.(
Values are taken from users).
Ans :
n1 = int(input("Enter
"Enter 1st Number : "
"))
n2 = int(input("Enter 2nd Number : "
"))
def add(a,b):
return a+b
print(add(a=n1, b=n2))
Output :
PROGRAM -15.
15. Write a python program to add two integer values using lambda function.
Ans :
a = lambda x, y: x + y
x = int(input("Enter
"Enter first number : "
"))
y = int(input("Enter
"Enter second number : ""))
result = a(x, y)
print("Sum is:", result)
Output :
PROGRAM -16.
16. Write a python program to find the sum of 10 integer values using lambda
function.
Ans :
Output :
PROGRAM -17.
17. Write a python program to perform date and math function on it.
import math
n = 5
math.sqrt(n)
math.exp(n)
math.log10(n)
print(math.pi)
today = date.today()
# dd/mm/YY
d1 = today.strftime("%d/%m/%Y"
"%d/%m/%Y")
print("d1 =", d1)
# mm/dd/y
d3 = today.strftime("%m/%d/%y"
"%m/%d/%y")
print("d3 =", d3)
Output :
PROGRAM -18. 18. Write a python program to create a user define calculator module and
create addition subtraction multiplication and division function in it and perform import and
for..import
.import and rename statement on it.
# Calculator Module
def addition(a,b):
return a+b
def subtraction(a,b):
return a-b
def multiplication(a,b):
return a*b
def division(a,b):
return a/b
def sqr(a):
return a**2
# import calculator
from calculator import *
a = 10
b = 5
print(addition(a,b))
print(subtraction(a,b))
print(multiplication(a,b))
print(division(a,b))
print(sqr(a))
Output :
PROGRAM -19. 19. Write a python program to demonstrate try,try &except statement,except
c louse,try finally statement , raising exception, and try except and else statement for
exception handing.
Ans :
try:
input("Enter Something..:"
Something..:")
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!"
number!")
else:
reciprocal = 1/num
print(reciprocal)
try:
f = open("test.txt",encoding
,encoding = 'utf-8')
finally:
f.close()
try:
int(input("Enter a number : "))
except Exception as e :
print(e)
Output :
PROGRAM -20. Write a python program to demonstrate exception handling for divide by
zero and naming error and syntax error.
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
print(c)
a = int(input("Enter a:"))
b = int(input("Enter b:"))
try:
c = a/b
print(c)
except ZeroDivisionError as e:
print(e)
Output :
PROGRAM -21.21. Write a python program to create a new file and perform read , write and
rename operation on it.
Ans :
import os
text = '''
Python is an interpreted high
high-level general-purpose
purpose programming language. Its
design philosophy emphasizes
code readability with its use of significant indentation. Its language constructs
as well as its object-oriented
oriented
approach aim to help programmers write clear, logical code for small and large-
large
scale projects.
'''
os.rename("text_file.txt", "new_file.txt"
"new_file.txt")
Output :
PROGRAM -22.
22. To write a Python program to find GCD of two numbers (by Euclid’s
Method).
a = int(input("Enter
"Enter first Number :
:- "))
b = int(input("Enter
"Enter Second Number ::- "))
x = GCD(a,b)
print (f"The gcd of {a} and {b} is : {x}")
Output :
PROGRAM -23.
23. To write a Python Program to find the square root of a number by
Newton’s Method.
a=int(input("Enter number:"))
))
print("Square
"Square root of number:"
number:",newton_method(a))
Output :
PROGRAM -24.
24. To write a Python Program to find the maximum from a list of numbers.
Ans :
Output :
PROGRAM -25.
25. To write a Python Program to find first n prime number.
Ans :
def PrimeChecker(a):
if a > 1:
for j in range(2,, int(a/
int(a/2) + 1):
if (a % j) == 0::
print(a, "is not a prime number"
number")
break
else:
print(a, "is a prime number"
number")
else:
print(a, "is not a prime number"
number")
while True:
a = int(input("Enter
"Enter an input number:"
number:"))
PrimeChecker(a)
Output :
PROGRAM -26.
26. To write a Python Program to multiply matrices.
Ans :
all_matrices = [[[2,4],[3,6]],[[
]],[[5,3],[6,5]]]
def multiplication():
ans = []
for rows in range(len(all_matrices[
range(len(all_matrices[0])):
row = []
for zero in range(len(all_matrices[
range(len(all_matrices[1][1])):
row.append(0)
ans.append(row)
row = []
for i in range(len(all_matrices[
range(len(all_matrices[0])):
for j in range(len(all_matrices[
range(len(all_matrices[1][1])):
for k in range(len(all_matrices[
nge(len(all_matrices[1])):
ans[i][j] += all_matrices[0][i][k] * all_matrices[1
1][k][j]
print("Your ans is :-\n"
n")
for i in ans:
print(i)
multiplication()
Output :
Ans :
Output :
PROGRAM -28.
28. To write a Python Program to perform Selection Sort.
Ans :
def selectionSort(array):
n = len(array)
for i in range(n):
minimum = i
for j in range(i+1, n):
if (array[j] < array[minimum]):
minimum = j
temp = array[i]
array[i] = array[minimum]
array[minimum] = temp
return array
# Driver code
array = [13, 4, 9, 5, 3, 16,, 12]
print(selectionSort(array))
Output :
def insertionSort(arr):
for i in range(1,
, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1]
] = arr[j]
j -= 1
arr[j+1] = key
Output :
PROGRAM -30.
30. To write a Python Program to find exponentiation of a number.
Ans :
num=int(input("Enter
"Enter number: "
"))
exp=int(input("Enter
"Enter exponential value: "
"))
result=1
for i in range(1,exp+1):
result=result*num
print("Result is:",result)
Output :