Python Lab Manual Final
Python Lab Manual Final
Software Lab: 7
Based on 21MCA24C1
Name:
Roll no:
INDEX
Sr.
Title Date Signature
No.
EXPERIMENT NO. 1
The greatest common divisor (GCD) of two or more numbers is the greatest common factor
number that divides them, exactly. It is also called the highest common factor (HCF). For
example, the greatest common factor of 15 and 10 is 5, since both the numbers can be divided by
5.
15/5 = 3
10/5 = 2
If a and b are two numbers then the greatest common divisor of both the numbers is denoted by
gcd(a, b). To find the gcd of numbers, we need to list all the factors of the numbers and find the
largest common factor.
PYTHON CODE
PYTHON CODE
Output: -
EXPERIMENT NO. 3
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input number.
Create another variable to store the exponent/power value.
Use the pow() function to print the resultant power of a number i.e, inputNumber
^ inputPower by passing the number, and exponent values as arguments to it and
print the resultant power.
PYTHON CODE
PYTHON CODE
list1 = []
num = int(input("Enter Number of Elements in List: "))
for i in range(1,num+1):
e = int(input("Enter Elements: "))
list1.append(e)
print("Largest Element is: ",max(list1))
Output: -
EXPERIMENT NO. 5
PYTHON CODE
def lsearch(L,x):
for i in range(len(L)):
if L[i] ==x:
return i
return -1
# Function call
L=[]
for i in range(0,10):
n = int(input("Enter item Of List: "))
L.append(n)
x = int(input("Enter the Element you want to search: "))
result = lsearch(L,x)
if(result == -1):
print("Element is not present in Array")
else:
print("Element is present at index: ",result)
Output:
EXPERIMENT NO. 6
Approach:
1. Compare x with the middle element.
2. If x matches with the middle element, we return the mid index.
3. Else if x is greater than the mid element, then x can only lie in the right (greater) half
subarray after the mid element. Then we apply the algorithm again for the right half.
4. Else if x is smaller, the target x must lie in the left (lower) half. So we apply the
algorithm for the left half
PYTHON CODE
def bsearch(L,x):
low = 0
high = len(L)-1
mid = 0
while low<=high:
mid = (high+low)//2
if L[mid]<x:
low = mid+1
elif L[mid]>x:
high = mid-1
else:
return mid
return -1
# Function call
L=[]
for i in range(0,5):
n = int(input("Enter item Of List: "))
L.append(n)
x = int(input("Enter the Element you want to search: "))
result = bsearch(L,x)
if(result != -1):
print("Element is present at index: ",str(result))
else:
print("Element is not present in the List")
Output:
EXPERIMENT NO. 7
THEORY:
PYTHON CODE
def Ssort(L):
for i in range(0,len(L)):
s=L[i]
p=i
for j in range(i+1,len(L)):
if L[j]<s:
s=L[j]
p=j
L[i],L[p]=L[p],L[i]
print(L)
# Function call
L=[]
m=int(input("Enter Size of List: "))
for k in range(0,m):
n=int(input("Enter item of List: "))
L.append(n)
Ssort(L)
Output:
EXPERIMENT NO.8
THEORY:
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing
cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from
the unsorted part are picked and placed at the correct position in the sorted part.
PYTHON CODE
def Isort(L):
for i in range(0,len(L)):
temp=L[i]
j=i-1
while (j>=0 and temp<L[j]):
L[j+1] = L[j]
j -= 1
L[j+1] = temp
print(L)
# Function call
L=[]
m=int(input("Enter Size of List: "))
for k in range(0,m):
n=int(input("Enter item of List: "))
L.append(n)
Isort(L)
Output: -
EXPERIMENT NO.9
THEORY:
Think of it as a recursive algorithm continuously splits the array in half until it cannot be further
divided. This means that if the array becomes empty or has only one element left, the dividing
will stop, i.e. it is the base case to stop the recursion. If the array has multiple elements, split the
array into halves and recursively invoke the merge sort on each of the halves. Finally, when both
halves are sorted, the merge operation is applied. Merge operation is the process of taking two
smaller sorted arrays and combining them to eventually make a larger one.
PYTHON CODE
def merge(arr,l,m,r):
n1 = m-l+1
n2 = r-m
L = [0]*(n1)
R = [0]*(n2)
for i in range(0,n1):
L[i] = arr[l+i]
for j in range(0,n2):
R[j] = arr[m+1+j]
i=0
j=0
k=l
while i<n1 and j<n2 :
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i<n1:
arr[k] = L[i]
i += 1
k += 1
while j<n2:
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr,l,r):
if l<r:
m = (l+(r-1))//2
mergeSort(arr,l,m)
mergeSort(arr,m+1,r)
merge(arr,l,m,r)
# Function call
arr = []
q = int(input("Enter size of Array: "))
for z in range(0,q):
t = int(input("Enter items in Array: "))
arr.append(t)
n = len(arr)
print("Given array is")
for i in range(n):
print("%d" %arr[i]),
mergeSort(arr,0,n-1)
print("\n Sorted array is")
for i in range(n):
print("%d" %arr[i]),
Output: -
EXPERIMENT NO.10
THEORY:
Prime Numbers are the numbers that have only 2 factors 1 and the number itself. It is only
defined for the number which is greater than ‘1’.
Approach:
Our program will take integer input from the user. This integer is the number limit till
where we need to find all prime numbers.
We need to iterate through every number below that input integer and check if it is a
prime number or not simultaneously.
If the iterated number is found prime number then print that number as an output.
PYTHON CODE
i=1
x = int(input("Enter the Number: "))
counter = 0
while True:
c=0
for j in range(1,i+1,1):
a = i%j
if a==0:
c = c+1
if c==2:
print(i)
counter = counter+1
if counter>=x:
break
i = i+1
Output: -