Python File
Python File
GHAZIABAD
LAB MANUAL
1
IMS ENGINEERING COLLEGE, GHAZIABAD
LIST OF EXPERIMENTS
COURSE: B. Tech SEM: IV SESSION: 2020-21(EVEN SEM)
BRANCH: CSE
SUBJECT CODE & NAME: KCS-453 & Python Language Programming Lab
WAP in python to create tuple and use function len(), max(), min(), count(), index() and zip() on
2. 5
the created tuple.
2
PRACTICAL -1
AIM: WAP in python to create a list and use function append(), count( ),
index( ), insert( ), pop( ), remove( ), reverse( ), sort( ) and extend( ) on the
created list.
Procedure:
• append(): The append() method appends an element to the end of the
list.
• count(): The count() method returns the number of times a specified
value appears in the string.
• index(): The index() method returns the position at the first occurrence
of the specified value.
• insert(): The insert() method inserts the specified value at the specified
position.
• pop(): The pop() method removes the element at the specified position.
• remove(): The remove() method removes the first occurrence of the
element with the specified value.
• reverse(): reverse() is an inbuilt method in Python programming
language that reverses objects of list in place.
• sort(): The sort() method sorts the list ascending by default.
• extend(): The extend() method adds all the elements of an iterable (list,
tuple, string etc.) to the end of the list.
Code:
fruits = ['apple', 'banana', 'cherry']
fruits_1 = ['grapes', 'lichi', 'mango']
#append()
fruits.append("apple")
print(fruits)
#count()
print("Count: ", fruits.count('apple'))
#index()
print("Index: ", fruits.index('apple'))
3
#insert()
fruits.insert(1, "orange")
print("Insert: ", fruits)
#pop()
fruits.pop(0) # 1 value poped
print("Pop: ", fruits)
#remove()
fruits.remove('banana')
print("Remove: ", fruits)
#reverse()
fruits.reverse()
print("Reverse: ", fruits)
#sort()
fruits.sort()
print("Sort: ", fruits)
#extend()
fruits.extend(fruits_1)
print("Extend: ", fruits)
OUTPUT:
4
PRACTICAL -2
AIM: WAP in python to create tuple and use function len(), max(), min(),
count(), index() and zip() on the created tuple.
Procedure:
CODE:
fruits=("apple","banana","mango","grapes","papaya")
vegetables=("tomato","potato","ladyfinger","peas")
#len
print(len(fruits))
#max
print(max(fruits))
#min
print(min(fruits))
5
#index
print(fruits.index("mango"))
#count
print(fruits.count("papaya"))
#zip
print(list(zip(fruits,vegetables)))
OUTPUT:
6
PRACTICAL -3
CODE:
count =0
num=2
while count<=n:
flag =0
for j in range(2,num):
if num%j==0:
flag = 1
break
if flag == 0:
count += 1
print(num,end=" ")
num += 1
OUTPUT:
7
PRACTICAL -4
CODE:
sqrt=number**0.5
import math
sqrt=math.sqrt(number)
import math
sqrt=math.pow(number,0.5)
OUTPUT:
8
PRACTICAL -5
CODE:
import math
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
print("GCD is:",gcd(a,b))
OUTPUT:
9
PRACTICAL -6
CODE:
import math
result=math.pow(a,exp)
result = a**exp
# using looping
result=1
for i in range(1,exp+1):
result=result*a
10
OUTPUT:
PRACTICAL -7
CODE:
def linear_search(list,n,x):
for i in range(0,n):
if list[i]==x:
return i
return -1
list=[]
for i in range(0,n):
list.append(int(input()))
11
x = int(input("Enter the element which do u want to search: "))
result = linear_search(list,n,x)
if result!=-1:
else:
OUTPUT:
PRACTICAL -8
CODE:
def binary_search(list,l,r,x):
while l<=r:
12
mid=(l+(r-1))//2
if list[mid]==x:
return mid
elif list[mid]<x:
l=mid+1
else:
r=mid-1
return -1
list=[]
for i in range(0,n):
list.append(int(input()))
result=binary_search(list,0,n-1,x)
if result!=-1:
OUTPUT:
13
PRACTICAL -9
CODE:
def array_mul(array1,array2):
res = []
for _ in range(len(array1)):
temp = []
for i in range(len(array2[0])):
sum = 0
j=0
while j != len(array2):
sum += array1[_][j]*array2[j][i]
j += 1
temp.append(sum)
res.append(temp)
return res
while True:
r1 = int(input())
c1 = int(input())
r2 = int(input())
c2 = int(input())
if c1 == r2:
break
else:
14
print("Enter the sizes of the array with correct data")
arr1 = []
for i in range(r1):
temp =[]
for j in range(c1):
temp.append(int(input()))
arr1.append(temp)
arr2 = []
for i in range(r2):
temp =[]
for j in range(c2):
temp.append(int(input()))
arr2.append(temp)
for _ in range(len(res)):
print(res[_])
OUTPUT:
PRACTICAL -10
15
AIM: To write a python program Selection sort.
CODE:
for i in range(n):
min_ = i
min_ = j
#swap
list=[]
list.append(int(input()))
selection_sort(list,n)
for i in range(n):
OUTPUT:
16
PRACTICAL -11
CODE:
def bubble_sort(list,n):
for i in range(n-1):
for j in range(0,n-i-1):
if list[j]> list[j+1]:
list[j],list[j+1]=list[j+1],list[j]
list=[]
list.append(int(input()))
bubble_sort(list,n)
for i in range(n):
17
OUTPUT:
PRACTICAL -12
CODE:
def insertion_sort(list):
for i in range(1,len(list)):
key=list[i]
j=i-1
list[j+1]=list[j]
j -= 1
list[j+1]=key
18
n=int(input("Enter the total elements: "))
list=[]
list.append(int(input()))
insertion_sort(list)
for i in range(len(list)):
OUTPUT:
PRACTICAL -13
CODE:
def partition(a,p,r):
i=p-1
pivot=a[r]
19
for j in range(p,r):
if a[j]<=pivot:
i=i+1
a[i],a[j]=a[j],a[i]
a[i+1],a[r]=a[r],a[i+1]
return(i+1)
def quick_sort(a,p,r):
if p<r:
q=partition(a,p,r)
quick_sort(a,p,q-1)
quick_sort(a,q+1,r)
list=[]
list.append(int(input()))
quick_sort(list,0,n-1)
for i in range(n):
OUTPUT:
PRACTICAL -14
def mergeSort(myList):
20
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]
mergeSort(left)
mergeSort(right)
i=0
j=0
k=0
myList[k] = left[i]
i += 1
else:
myList[k] = right[j]
j += 1
k += 1
myList[k] = left[i]
i += 1
k += 1
myList[k]=right[j]
j += 1
21
k += 1
list = []
list.append(int(input()))
mergeSort(list)
OUTPUT:
PRACTICAL -15
CODE:
Def heapify(a,n,i):
largest = i
l=2*i+1
r=2*i+2
largest=l
largest=r
if largest !=i:
22
a[i],a[largest]=a[largest],a[i]
heapify(a, n, largest)
def heapsort(a):
n=len(a)
for i in range(n//2-1,-1,-1):
heapify(a, n, i)
for i in range(n-1,0,-1):
a[i],a[0]=a[0],a[i]
heapify(a,i,0)
list = []
list.append(int(input()))
heapsort(list)
print("Sorted Array:")
for i in range(n):
OUTPUT:
23