PYTHON PROGRAMMING
[KCS-453]
LABORATORY MANUAL
B.TECH II YEAR – IV SEM
[A.Y:2022-2023]
SR INSTITUTE OF MANAGEMENT AND
TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
NAME:
ROLL NO.
VISION
➢ To achieve high quality in technical education that provides the skills and attitude to adapt to
the global needs of the Computer Science & Engineering sector, through academic and research
excellence..
MISSION
➢ To equip the students with the cognizance for problem solving and to improve the
teaching learning pedagogy by using innovative techniques.
➢ To strengthen the knowledge base of the faculty and students with motivation towards possession
of effective academic skills and relevant research experience.
➢ To promote the necessary moral and ethical values among the engineers, for the betterment
of the society
1. Lab Objectives:
To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, and dictionaries.
Read and write data from/to files in Python.
2. Lab Outcomes:
Upon completion of the course, students will be able to
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.
Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python
3. Introduction about lab
Minimum System
requirements:
Processors: Intel Atom® processor or Intel® Core™ i3 processor.
Disk space: 1 GB.
Operating systems: Windows* 7 or later, macOS, and Linux.
Python* versions: 2.7.X, 3.6.X.,3.8.X
About lab:
Python is a general purpose, high-level programming language; other high- level languages you might
have heard of C++, PHP, and Java. Virtually all modern programming languages make us ofan
Integrated Development Environment (IDE), which allows the creation, editing, testing, and saving of
programs and modules. In Python, the IDE is called IDLE (like many items in the language, this is a
reference to the British comedy group Monty Python, and in this case, one of its members, Eric Idle).
Many modern languages use both processes. They are first compiled into a lower level language,
called byte code, and then interpreted by a program called a virtual machine. Python uses both
processes, but because of the way programmers interact with it, it is usually considered an
interpretedlanguage.
Practical aspects are the key to understanding and conceptual visualization Of theoretical aspects
covered in the books. Also, this course is designed to review the concepts of Data Structure using C,
studied in previous semester and implement the various algorithms related to different data structures.
INDEX
S.No List of Programs Date Sign
1
2
3
4
5
6
7
8
9
10
11
12
PROGRAM-1
Aim:
Create a list and perform the following methods
1) insert() 2) remove() 3) append() 4) len() 5) pop() 6) clear()
Program:
a=[1,3,5,6,7,[3,4,5],"hello"]
print(a)
a.insert(3,20)
print(a)
a.remove(7)
print(a)
a.append("hi")
print(a)
len(a)
print(a)
a.pop()
print(a)
a.pop(6)
print(a)
a.clear()
print(a)
PROGRAM-2
Aim:
Create a set and implements all methods
Program:
s1={1,2,3}
s2={3,4,5}
print(len(s1))
print(s1.difference(s2))
print(s2.difference(s1))
print(s1.intersection(s2))
print(s1.union(s2))
s1.add(6)
print(s1)
print(s1.discard(1))
print(s1)
s1.update([1,7,8,9])
s1.remove(8)
print(s1)
s2.clear()
print(s2)
print(s1.issubset(s2))
print(s1.issuperset(s2))
print(s1.isdisjoint(s2))
PROGRAM-3
Aim:
Create a Dictionary and implements all methods
Program:
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
founder = first_dict.get("founder")
print(founder)
items = first_dict.items()
print(items)
dict_keys = first_dict.keys()
print(dict_keys)
dict_values = first_dict.values()
print(dict_values)
first_dict.pop("work-style")
print(first_dict)
first_dict.popitem()
print(first_dict)
first_dict.update({"Editor": "Abbey Rennemeyer"})
print(first_dict)
second_dict = first_dict.copy()
print(second_dict)
first_dict.clear()
print(first_dict)
PROGRAM-4
Aim:
Write a program to implement Binary Search
Program:
a=[2,3,4,1,6,17,19,23]
a.sort()
#[1,2,3,4,6,17,19,23]
low=0
high=len(a)-1
print(a)
x=int(input("enter item"))
def binarysearch(a,low,high,x):
if high>low:
mid=int((low+high)/2)
if a[mid]==x:
return mid
elif a[mid]>x:
return binarysearch(a,low,mid,x)
else:
return binarysearch(a,mid,high,x)
else:
return -1
r=binarysearch(a,low,high,x)
if r==-1:
print("item not found")
else:
print(x,"found at index",r)
PROGRAM-5
Aim:
Write a program to implement Merge Sort.
Program:
#Merge Sort
arr=[13,5,4,18,1,10,11]
def merge (arr):
if len(arr)>1:
mid=len(arr)//2
L=arr[:mid]
R=arr[mid:]
merge (L)
merge (R)
i=j=k=0
while i<len(L)and j<len(R):
if L[i]<=R[j]:
arr[k]=L[i]
i=i+1
else:
arr[k]=R[j]
j=j+1
k=k+1
while i<len(L):
arr[k]=L[i]
i=i+1
k=k+1
while j<len(R):
arr[k]=R[j]
j=j+1
k=k+1
print ("array before sorting",arr)
merge (arr)
print (arr)
PROGRAM-6
Aim:
Write a program to implement Tower of Hanoi.
Program:
# Recursive Python function to solve the tower of hanoi
def TowerOfHanoi(n , source, destination, auxiliary):
if n==1:
print ("Move disk 1 from source",source,"to destination",destination)
return
TowerOfHanoi(n-1, source, auxiliary, destination)
print ("Move disk",n,"from source",source,"to destination",destination)
TowerOfHanoi(n-1, auxiliary, destination, source)
# Driver code
n=4
TowerOfHanoi(n,'A','B','C')
# A, C, B are the name of rods
PROGRAM-7
Aim:
Write a program to implement Sieve Erastothenes Algo to find prime numbers.
Program:
prime=[]
n=int(input("enter limit"))
for i in range(n+1):
prime.append(i)
prime[0]=0
prime[1]=0
p=2
while(p*p<=n):
if prime[p]!=0:
for i in range(p*p,n+1,p):
prime[i]=0
p+=1
for i in range(2,n+1):
if prime[i]!=0:
print(prime[i])
PROGRAM-8
Aim:
Write a program to check the given number is palindrome.
Program:
n=int(input("enter any number"))
num=n
r=0
while n>0:
dig=n%10
r=r*10+dig
n=n//10
print(r)
print(num)
if num==r:
print("pallndrome")
else:
print("non pallindrme")
PROGRAM-9
Aim:
Write a program to implement Selection Sort.
Program:
a=[3,1,6,5,7,11,100,50,202,22,21,10,18]
def selectionsort(a):
n=len(a)
for i in range(n):
min=i
for j in range(i+1,n):
if a[j]<a[min]:
min=j
temp=a[i]
a[i]=a[min]
a[min]=temp
return a
print(selectionsort(a))
PROGRAM-10
Aim:
Write a menu driven program to implement simple calculator using modules.
Program:
Calc.py
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a//b
TestCalc.py
import calc
print("press 1 for addition")
print("press 2 for subtrCTION")
print("press 3 for mutiply")
print("press 4 for division")
c=int(input("enter your choice"))
if c==1:
print(calc.add(100,50))
elif c==2:
print(calc.sub(100,50))
elif c==3:
print(calc.mul(100,50))
elif c==4:
print(calc.div(100,50))
else:
print("invalid choice")
PROGRAM-11
Aim:
WAP to read a file and count total numbers of digits, upper case letters and lower case letters and spaces.
Program:
#WAp to read a file and count
#total num of uppercase letters
#total num of lowercase letters
#total num of digits
#total num of spaces
c1=0#count uppercase letters
c2=0#count lower letters
c3=0#count digits
c4=0#count spaces
try:
f=open("test.txt","r")
data=f.read()
for i in data:
if i.isupper():
c1=c1+1#c1+=1
elif i.islower():
c2=c2+1
elif i.isdigit():
c3=c3+1
elif i.isspace():
c4=c4+1
else:
print("file is corrupted")
except FileNotFoundError:
print("please enter a valid file name")
#raise Exception("file does not exist")
else:
print("total no of upper letters",c1)
print("total no of lower letters",c2)
print("total no of digits",c3)
print("total no of spaces",c4)
finally:
f.close()
PROGRAM-12
Aim:
WAP to write some numbers in a input.txt file.
1- find even numbers and write them into even.txt file
2- find odd numbers and write them into odd.txt file
Program:
f=open("input.txt","w")
for i in range(1,11):
f.write(str(i))
#f.write("\n")
f.close()
f1=open("input.txt","r")
ch=f1.read()
even=open("even.txt","w")
odd=open("odd.txt","w")
for i in ch:
j=int(i)
if j%2==0:
even.write(str(j))
else:
odd.write(str(j))
print("Done")
odd.close()
even.close()
f1.close()
PROGRAM-13
Aim:
WAP to find the Fibonacci series upto n numbers.
Program:
def fib(n):
if n<=1:
return n
else:
return fib(n-1)+fib(n-2)
n=int(input(“enter limit”))
for i in range(n):
print(fib(i))