IMS ENGINEERING COLLEGE
GHAZIABAD
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
LAB MANUAL
COURSE : B.Tech. (CSE)
SEMESTER : IV
SUBJECT : Python Language Programming Lab
SUBJECT CODE : KCS-453
SUBMITTED BY: SUBMITTED TO:
1
IMS ENGINEERING COLLEGE, GHAZIABAD
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
LIST OF EXPERIMENTS
COURSE: B. Tech SEM: IV SESSION: 2020-21(EVEN SEM)
BRANCH: CSE
SUBJECT CODE & NAME: KCS-453 & Python Language Programming Lab
S. No. NAME OF EXPERIMENT Page
No.
WAP in python to create a list and use function append( ), count( ), index( ), insert( ), pop( ),
1. 3
remove( ), reverse( ), sort( ) and extend( ) on the created list.
WAP in python to create tuple and use function len(), max(), min(), count(), index() and zip() on
2. 5
the created tuple.
3. To write a python program to print first n prime numbers. 7
4. To write a python program find the square root of a number.(Newton’s Method). 8
5. To write a python program to compute the GCD of two numbers. 10
6. To write a python program exponentiation (power of a number). 11
7. To write a python program linear search. 13
8. To write a python program Binary search. 14
9. To write a python program for matrix multiplication. 16
10. To write a python program Selection sort. 18
11. To write a python program Bubble sort. 19
12. To write a python program Insertion sort. 21
13. To write a python program Quick sort. 22
14. To write a python program Merge sort. 24
15. To write a python program Heap sort. 26
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:
• len(): The len() function returns the number of items in an object.
• max(): The max() function returns the item with the highest value, or the
item with the highest value in an iterable.
• min(): The min() function returns the item with the lowest value, or the
item with the lowest value in an iterable.
• 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.
• zip(): The zip() function returns a zip object, which is an iterator of tuples
where the first item in each passed iterator is paired together, and then
the second item in each passed iterator are paired together etc.
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
AIM: To write a python program to print first n prime numbers.
CODE:
n = int(input("Enter the no: "))
print("First",n, "prime no is: ", end="")
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
AIM: To write a python program find the square root of a number.(Newton’s
Method).
CODE:
• # using exponent operator
number=int(input("Enter the number: "))
sqrt=number**0.5
print("Square root is: ", sqrt)
• #using sqrt() method
import math
number=int(input("Enter the number: "))
sqrt=math.sqrt(number)
print("Square root is: ", sqrt)
• # using pow() method
import math
number=int(input("Enter the number: "))
sqrt=math.pow(number,0.5)
print("Square root is: ", sqrt)
OUTPUT:
8
PRACTICAL -5
AIM: To write a python program to compute the GCD of two numbers.
CODE:
• # using gcd() method
import math
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
print("GCD is: ",math.gcd(a,b))
• # using Euclid algo
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
print("GCD is:",gcd(a,b))
OUTPUT:
9
PRACTICAL -6
AIM: To write a python program exponentiation (power of a number).
CODE:
# using pow method
import math
a=int(input("Enter the number: "))
exp=int(input("Enter the exponential value: "))
result=math.pow(a,exp)
print("result is: ",result)
#using exponential operator
a=int(input(Enter the number: "))
exp=int(input(Enter the exponential value: "))
result = a**exp
print("result is: ",result)
# using looping
a=int(input("Enter the number: "))
exp=int(input("Enter the exponential value: "))
result=1
for i in range(1,exp+1):
result=result*a
10
OUTPUT:
PRACTICAL -7
AIM: To write a python program linear search.
CODE:
def linear_search(list,n,x):
for i in range(0,n):
if list[i]==x:
return i
return -1
n=int(input("Enter the number of elements in list: "))
print("Enter the elemnts: ")
list=[]
for i in range(0,n):
list.append(int(input()))
print("List is: ",list)
11
x = int(input("Enter the element which do u want to search: "))
result = linear_search(list,n,x)
if result!=-1:
print("Element found at position: ",result)
else:
print("Element not found")
OUTPUT:
PRACTICAL -8
AIM: To write a python program Binary search.
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
n=int(input("Enter the number of elements in list: "))
print("Enter the elemnts: ")
list=[]
for i in range(0,n):
list.append(int(input()))
print("List is: ",list)
x=int(input("Enter the element which do u want to search: "))
result=binary_search(list,0,n-1,x)
if result!=-1:
print("Element found at position: ",result)
OUTPUT:
13
PRACTICAL -9
AIM: To write a python program for matrix multiplication.
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:
print("Enter the size of row and column of array1: ")
r1 = int(input())
c1 = int(input())
print("Enter the size of row and column of array2: ")
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):
print("Enter the elements of",i+1,"row of array1")
temp =[]
for j in range(c1):
temp.append(int(input()))
arr1.append(temp)
arr2 = []
for i in range(r2):
print("Enter the elements of",i+1,"row of array2")
temp =[]
for j in range(c2):
temp.append(int(input()))
arr2.append(temp)
res = array_mul(arr1 ,arr2)
print("Multiplication array is: ")
for _ in range(len(res)):
print(res[_])
OUTPUT:
PRACTICAL -10
15
AIM: To write a python program Selection sort.
CODE:
def selection_sort(A, n):
for i in range(n):
min_ = i
for j in range(i+1, n):
if A[min_] > A[j]:
min_ = j
#swap
A[i], A[min_] = A[min_], A[i]
n=int(input("Enter the total elements: "))
print("Enter the elements: ")
list=[]
for i in range (0,n):
list.append(int(input()))
selection_sort(list,n)
print("Sorted array: ")
for i in range(n):
print(list[i] , end=" ")
OUTPUT:
16
PRACTICAL -11
AIM: To write a python program Bubble sort.
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]
n = int(input("Enter the total elements: "))
print("Enter the elements: ")
list=[]
for i in range (0,n):
list.append(int(input()))
bubble_sort(list,n)
print("Sorted array: ")
for i in range(n):
print(list[i] , end = " ")
17
OUTPUT:
PRACTICAL -12
AIM: To write a python program Insertion sort.
CODE:
def insertion_sort(list):
for i in range(1,len(list)):
key=list[i]
j=i-1
while j>=0 and key < list[j]:
list[j+1]=list[j]
j -= 1
list[j+1]=key
18
n=int(input("Enter the total elements: "))
print("Enter the elements: ")
list=[]
for i in range (0,n):
list.append(int(input()))
insertion_sort(list)
print("Sorted array is:")
for i in range(len(list)):
print(list[i], end=" ")
OUTPUT:
PRACTICAL -13
AIM: To write a python program Quick sort.
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)
n=int(input("Enter the total elements: "))
print("Enter the elements: ")
list=[]
for i in range (0,n):
list.append(int(input()))
quick_sort(list,0,n-1)
print("Sorted array is:")
for i in range(n):
print(list[i] , end = " ")
OUTPUT:
PRACTICAL -14
AIM: To write a python program Merge sort.
CODE:
def mergeSort(myList):
20
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]
# Recursive call on each half
mergeSort(left)
mergeSort(right)
# Two iterators for traversing the two halves
i=0
j=0
k=0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
myList[k] = left[i]
i += 1
else:
myList[k] = right[j]
j += 1
k += 1
# For all the remaining values
while i < len(left):
myList[k] = left[i]
i += 1
k += 1
while j < len(right):
myList[k]=right[j]
j += 1
21
k += 1
n = int(input("Enter the total elements: "))
print("Enter the elements: ")
list = []
for i in range (0,n):
list.append(int(input()))
mergeSort(list)
print("Sorted list is: ", list)
OUTPUT:
PRACTICAL -15
AIM: To write a python program Heap sort.
CODE:
Def heapify(a,n,i):
largest = i
l=2*i+1
r=2*i+2
if l<n and a[i]<a[l]:
largest=l
if r<n and a[largest]<a[r]:
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)
n = int(input("Enter the total elements: "))
print("Enter the elements: ")
list = []
for i in range (0,n):
list.append(int(input()))
heapsort(list)
print("Sorted Array:")
for i in range(n):
print(list[i], end=" ")
OUTPUT:
23