You are on page 1of 27

Python Programming

Practical File

Session [2022-23]

Submitted To - Mrs. Kajal

Submitted By - Shubham Kumar Jha


B. Tech (CSE)
3rd Sem
INDEX
S.no Topic Date Remarks
1. Write a program to Compute the GCD of 5/12/2022
two numbers.
2. Write a program to find the square root 5/12/2022
of a number (Newton ‘s method).
3. Write a program to find the power of a 5/12/2022
number.
4. Write a program to find the maximum of 5/12/2022
a list of numbers.
5. Write a program to implement Linear 5/12/2022
search and Binary search.
6. Write a program to implement Selection 5/12/2022
sort and Insertion sort.
7. Write a program to implement Merge 5/12/2022
sort.
8. Write a program to find first n prime 5/12/2022
numbers.
9. Write a program for Multiplication of 5/12/2022
matrices.
10. Write a program to Programs that take 5/12/2022
command line arguments (word count).
Program - 1
#To compute the GCD of two numbers

a = int(input('Enter The First Number:'))


b = int(input('Enter The Second Number:'))
i=1
while (i<=a and i<=b):
if (a%i==0 and b%i==0):
gcd=i
i=i+1
print ('GCDis', gcd)
OUTPUT :
Program - 2
#To find the square root of a number using Newton’s Method

a = int(input('Enter the number:'))


b=0.0000001
x=a
count=0
while(b):
count += 1
root = 0.5*(x+(a/x))
if abs(root-x) < b:
break
x=root
print(x)
OUTPUT :
Program - 3
#To find the power of a number

x=int(input('Enter the Number whose power to be calculated:'))


y=int(input('Enter the Value raised to compute Power:'))
p=x**y
print('The value of',x,'**',y,'is',p)
OUTPUT :
Program-4
#To find the maximum of a list of Numbers

List1 = []
Num = int (input ('Enter Number of elements in the List:'))
for i in range (1, Num+1):
e=int (input ('Enter the elements:'))
List1.append(e)
print ('The largest element is:', max(List1))
OUTPUT :
Program-5
#Linear Search

def linear_Search(L,x):

for i in range(len(L)):
if (L[i] == x):
return i
return -1

L = []

for k in range(0,10):
n=int(input('Enter the elements to put in list :'))
L.append(n)
x=int(input('Enter the element to search:'))
result=linear_Search(L,x)
if(result==-1):
print('Element is not present in the array')
else:
print('Element is present at index :',result)
OUTPUT :
Program - 6

#Binary search

def binary_search(list1, n):


low = 0
high = len(list1) - 1
mid = 0

while low <= high:


mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid - 1
else:
return mid
return -1

list1 = [1, 2, 3, 4, 5, 6, 7]
n=5

result = binary_search(list1, n)

if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list")
OUTPUT :
Program - 7

#Selection Sort)
def selectionSort(array, size):
for ind in range(size):
min_index = ind
for j in range(ind + 1, size):
# select the minimum element in every iteration
if array[j] < array[min_index]:
min_index = j
# swapping the elements to sort the array
(array[ind], array[min_index]) = (array[min_index],
array[ind])
arr = [-58, 4, 0, 14, -4, 69, -66, -6969, 3495, 584, 34, -234]
size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by selection
sort is:')
print(arr
OUTPUT :
Program - 8

#Insertion Sort
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

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)
print('The insertion sorted list:',L)
OUTPUT :
Program - 9

#Merge sort

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-l)//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)

arr = [45, 1, 56, 3, 34, 10]


n = len(arr)
print("Given array is")
for i in range(n):
print("%d" % arr[i],end=" ")
mergeSort(arr, 0, n-1)
print("\n\nSorted array is")
for i in range(n):
print("%d" % arr[i],end=" ")
OUTPUT :
Program - 10

#To find first n prime numbers


i=1
x = int(input('Enter the number:'))
for k in range(1, x+1):
c=0
for j in range(1, i+1):
a=i%j
if a == 0:
c=c+1

if c == 2:
print(i)
else:
k=k-1

i=i+1
OUTPUT :
PROGRAM-11:

#Multiplication of matrices
#3x3 matrix

X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[2,3,4,1],
[34,1,45,1],
[4,5,2,89]]
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)
OUTPUT :
Program - 12

#To take command line arguments

import sys
print(type(sys.argv))
print('The command line arguments are:')
for i in sys.argv:
print (i)
OUTPUT:

You might also like