Lab Manual
Lab Manual
LAB MANUAL
Prepared By:
Mr. Vishnuvardhan. Y
Assistant Professor
Dept. of CSE, MSEC
Syllabus
1) Write a Python program to find GCD of two numbers.
2) Write a Python Program to find the square root of a number by Newton’s Method
3) Write a Python program to find the exponentiation of a number.
4) Write a Python Program to find the maximum from a list of numbers.
5) write a Python Program to perform Linear Search
6) write a Python Program to perform Binary Search
7) Write a Python Program to perform selection sort.
8) Write a Python Program to perform insertion sort.
9) Write a Python Program to perform Merge sort.
10) Write a Python program to find first n prime numbers.
11) Write a Python program to multiply matrices.
Course Objective:
Learn Syntax and Semantics and create Functions in Python.
Handle Strings and Files in Python.
Understand Lists, Dictionaries and Regular expressions in Python.
Implement Object Oriented Programming concepts in Python
Build Web Services and introduction to Network and Database Programming in Python.
Course Outcomes:
Examine Python syntax and semantics and be fluent in the use of Python flow control and
functions.
Demonstrate proficiency in handling Strings and File Systems.
Create, run and manipulate Python Programs using core data structures like Lists,
Dictionaries and use Regular Expressions.
Interpret the concepts of Object-Oriented Programming as used in Python.
Implement exemplary applications related to Network Programming, Web Services and
Databases in Python.
Program 1: Write a Python program to find GCD of two numbers.
Aim:
To write a Python program to find GCD of two numbers.
Algorithm:
1. Define a function named compute GCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:
def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i
return gcd
num1 = 54
num2 = 24
# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))
print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))
Sample Output:
$python main.py
('The GCD. of', 54, 'and', 24, 'is', 6)
Program 2: Write a Python Program to find the square root of a number by
Newton’s Method
Aim:
To write a Python Program to find the square root of a number by Newton’s Method.
Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx..
Program:
def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx
print('The square root is' ,newtonSqrt(100))
Sample Output:
The square root is 10
Program 3: Write a Python program to find the exponentiation of a number.
Aim:
To write a Python program to find the exponentiation of a number.
Algorithm:
1. Define a function named power()
2. Read the values of base and exp
3. Use ‘if’ to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result.
Program:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))
Sample Output:
Enter base: 7
Enter exponential value: 2
Result:49
Program 4: Write a Python Program to find the maximum from a list of
numbers.
Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i<n
8. Print the value of maximum number
Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)
Sample Output:
$python main.py
(list of items is: [5, 7, 10, 12, 15] )
enter item to search: 7
(item found at position:, 2)
Program 6: Write a Python Program to perform Binary Search
Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates
Program:
# Python code to implement iterative Binary Search.
# It returns location of x in given array arr
# if present, else returns -1
Sample Output:
$python main.py
Element is present at index 2
Program 7: Write a Python Program to perform selection sort.
Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selection sort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
Sample Output:
$python main.py
[17, 20, 26, 31, 44, 54, 55, 77, 93]
Program 8: Write a Python Program to perform insertion sort.
Aim:
To write a Python Program to perform insertion sort.
Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list
Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
Sample Output:
$python main.py
[20, 54, 54, 54, 54, 54, 93, 93, 93]
Program 9: Write a Python Program to perform Merge sort.
Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list
Program:
# Python program for implementation of MergeSort
# Merges two subarrays of arr[].
# First subarray is arr[l..m]
# Second subarray is arr[m+1..r]
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
mergeSort(arr,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
print ("%d" %arr[i]),
Sample Output:
$python main.py
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
Program 10: Write a Python program to find first n prime numbers.
Aim:
To write a Python program to find first n prime numbers.
Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num
4. Repeat step 3 for i in range(2,num)
Program:
n = int(input("Enter the upper limit: "))
print("Prime numbers are")
for num in range(0,n + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Sample Output:
$python main.py
Enter the upper limit: 20
Prime numbers are
2
3
5
7
11
13
17
19
Program 11: Write a Python program to multiply matrices.
Aim:
To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ‘result’
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r
Program:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Sample Output:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]