Python Programming_Lab
Python Programming_Lab
[KCS-453]
LABORATORY MANUAL
B.TECH II YEAR – IV SEM
[A.Y:2022-2023]
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
➢ To strengthen the knowledge base of the faculty and students with motivation towards possession
➢ To promote the necessary moral and ethical values among the engineers, for the betterment
of the society
1. Lab Objectives:
2. Lab Outcomes:
Upon completion of the course, students will be able to
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
Aim:
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:
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:
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:
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:
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:
Program:
# Driver code
n=4
TowerOfHanoi(n,'A','B','C')
# A, C, B are the name of rods
PROGRAM-7
Aim:
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:
Program:
Aim:
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:
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")
Aim:
WAP to read a file and count total numbers of digits, upper case letters and lower case letters and spaces.
Program:
Aim:
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:
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))