Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
131 views

Sri Aurobindo: Python Lab ME506

This document contains 16 Python programs written by Ravi Dhakad, a student at Sri Aurobindo Institute of Technology. The programs cover topics such as adding numbers, string slicing, calculating circle area, if/else conditional logic, while and for loops, list, tuple, set and dictionary operations, and the use of functions and lambda functions. Each program is presented with the code, output, and Ravi's name and student details.

Uploaded by

Rajat Malviya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Sri Aurobindo: Python Lab ME506

This document contains 16 Python programs written by Ravi Dhakad, a student at Sri Aurobindo Institute of Technology. The programs cover topics such as adding numbers, string slicing, calculating circle area, if/else conditional logic, while and for loops, list, tuple, set and dictionary operations, and the use of functions and lambda functions. Each program is presented with the code, output, and Ravi's name and student details.

Uploaded by

Rajat Malviya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Python Lab ME506

Sri Aurobindo
Institute of Technology, Indore
Department of Mechanical Engineering

Name – Ravi Dhakad


Roll No. – 0873ME191012
Class – Btech-ME
Subject – Python Lab
Semester – 5th , 3rd Year

Ravi Dhakad 0873ME191012 ME-3 rd Year/5th Semester Page 1


Python Lab ME506

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 : ""))

add = num1 + num2


print(f"Sum of {num1} and {num2} is : {add}")

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 2
Python Lab ME506

Program -2.
2. Write a python program to perform slicing and indexing on string.

Ans :

string = "Hello python lovers......!"


print(string[5])
print(string[9])
print(string[15])
print(string[0:10])
print(string[5:15])
print(string[ : :-1])

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 3
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 4
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 5
Python Lab ME506

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 : "
"))

if a>b and a>c:


print(f"{a} is greater then {b} and {c}")
elif a<b and c<b:
print(f"{b} is greater then {c} and {a}")
else:
print(f"{c} is greater then {a} and {b}")

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 6
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 7
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 8
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 9
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 10
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 11
Python Lab ME506

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)

Tuple1 = (0, 1, 2, 3, 4,5 ,6


6 ,7, 8, 9, 10)
print(Tuple1[1:])
print(Tuple1[::-1])
print(Tuple1[4:9])

Tuple3.append("Ravi")
print(Tuple3)
Tuple3.remove("Ravi")
print(Tuple3)

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 12
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 13
Python Lab ME506

PROGRAM -13. 13. Write a python program to create dictionary and perform insert, replace,
keys,pop and popitem operation on it.

Ans :

dict1 = {"Nikhil": 7, "Akshat"


"Akshat": 1, "Akash": 2, "Ravi" : 10}

res = dict1.popitem()
print(str(res))
print(dict1.pop('Akash'))
dict1['Mohit'] = '9'
del dict1["Nikhil"]

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 14
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 15
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 16
Python Lab ME506

PROGRAM -16.
16. Write a python program to find the sum of 10 integer values using lambda
function.

Ans :

from functools import reduce


lst =[1,2,3,4,5,6,7,8,9,10]
fins = reduce(lambda x,y:x+y, lst)
print(fins)

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 17
Python Lab ME506

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)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y"
"%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year


d2 = today.strftime("%B
"%B %d, %Y"
%Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y"
"%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year


d4 = today.strftime("%b-%d-%Y"
%Y")
print("d4 =", d4)

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 18
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 19
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 20
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 21
Python Lab ME506

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.
'''

with open("text_file.txt", "w


"w" ) as f:
f.write(text)

with open("text_file.txt", "a" ) as f:


f.write(text)

with open("text_file.txt", "r" ) as f:


print(f.read())

os.rename("text_file.txt", "new_file.txt"
"new_file.txt")

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 22
Python Lab ME506

PROGRAM -22.
22. To write a Python program to find GCD of two numbers (by Euclid’s
Method).

def GCD(x, y):


if x > y:
small = y
else:
small = x
for i in range(1,
, small+
small+1):
if((x % i == 0) and (y % i == 0)):
gcd = i
return gcd

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 23
Python Lab ME506

PROGRAM -23.
23. To write a Python Program to find the square root of a number by
Newton’s Method.

def newton_method(number, number_iters = 100):


a = float(number)
for i in range(number_iters):
number = 0.5 * (number + a / number)
return number

a=int(input("Enter number:"))
))
print("Square
"Square root of number:"
number:",newton_method(a))

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 24
Python Lab ME506

PROGRAM -24.
24. To write a Python Program to find the maximum from a list of numbers.

Ans :

list1 = [10, 20, 15, 45, 99]


]
x = 0
for i in list1:
if i > x:x = i
else:pass
print("maximum is:", x)

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 25
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 26
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 27
Python Lab ME506

PROGRAM -27. To write a Python Program to perform Linear Search.

Ans :

def search(arr, x):


z = 0
for i in range(len(arr)):
if arr[i] == x:
z = i
break
return z

arr = [74, 89, 58, 76, 4, 99


99, 81]
x = 76
print(search(arr, x))

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 28
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 29
Python Lab ME506

PROGRAM -29. To write a Python Program to perform Insertion Sort.

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

# Driver code to test above


arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print(arr)

Output :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 30
Python Lab ME506

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 :

Ravi Dhakad 0873ME191012 ME


ME-3rd Year/5th Semester Page 31

You might also like