INDEX
S.NO. EXPERIMINT PAGE NO. SIGNATURE
Python program that takes in command
1 line arguments as input and print the 1
number of arguments.
Python program to perform Matrix
2
Multiplication. 2
Python program to compute the
3 GCD of two numbers. 3
Python program to find the most frequent
4 words in a text file. 4
Python program find the square root of
a number (Newton’s method)
5 5
Python program exponentiation (power of
6 a number). 6
Python program find the largest
7 element of a list. 7
8 Python program for linear search. 8
9 Python program Binary search. 9
10 Python program selection sort. 10
11 Python program insertion sort. 11
12 Python program merge sort. 12-13
Python program first n prime
13 numbers. 14
Program 1
Python program that takes in command line arguments as input
and print the number of arguments.
import sys
n = len(sys.argv)
print("Total arguments passed:", n)
print("\nName of Python script:", sys.argv[0])
print("\nArguments passed:", end=" ") for i in
range(1, n):
print(sys.argv[i], end=" ")
Sum = 0
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
Output :
Arguments passed :
5,7,3,2
Result : 17
Program 2
Python program to perform Matrix Multiplication.
# take a 3x3 matrix
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
# take a 3x4 matrix
B = [[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]]
# iterating by row of A
for i in range(len(A)):
# iterating by column by B
for j in range(len(B[0])):
# iterating by rows of B
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
for r in result:
print(r)
Output :
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Program 3
Python program to compute the GCD of two numbers.
def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
a = int(input("Enter first number."))
b = int(input("Enter second number."))
# prints gcd
print("The gcd of is : ",
end="") print(hcf(a, b))
Output :
Enter first number. 60
Enter second number. 48
The gcd of is : 12
Program 4
Python program to find the most frequent words in a text file.
file = open("gfg.txt", "r")
frequent_word = ""
frequency = 0
words = []
for line in file:
line_word = line.lower().replace(',',
'').replace('.', '').split(" ")
for w in line_word:
words.append(w)
for i in range(0, len(words)):
count = 1
for j in range(i+1, len(words)):
if (words[i] == words[j]):
count = count + 1
if (count > frequency):
frequency = count
frequent_word = words[i]
print("Most repeated word: " +
frequent_word) print("Frequency: " +
str(frequency)) file.close()
Program 5
Python program find the square root of a
number (Newton’s method).
def squareRoot(n, l):
x = n
count = 0
while (1):
count += 1
root = 0.5 * (x + (n / x))
if (abs(root - x) < l):
break
x = root
return root
if __name__ == "__main__":
n = 327
l = 0.00001
print(squareRoot(n, l))
Output :
18.083141320025124
Program 6
Python program exponentiation (power of a number).
def calc_power(N, p):
if p == 0:
return 1
return N * calc_power(N, p-1)
a = int(input("Enter the number.")) b =
int(input("Enter the exponential power."))
print(calc_power(a, b))
Output :
Enter the number.3
Enter the exponential power.4
81
Program 7
Python program find the largest element of a list.
list1 = []
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
print("Largest element is:", max(list1))
Output :
Enter number of elements in list: 5
Enter elements: 4
Enter elements: 7
Enter elements: 15
Enter elements: 21
Enter elements: 12
Largest element is: 21
Program 8
Python program for linear search.
def linearSearch(array, n, x):
for i in range(0, n):
if (array[i] == x):
return i
return -1
array = [2, 4, 0, 1, 9]
x = 1
n = len(array)
result = linearSearch(array, n,
x) if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
Output :
Element found at index 3.
Program 9
Python program Binary search.
def binary_search(arr, x):
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1
arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search(arr, x)
if result != -1:
print("Element is present at index",
str(result)) else:
print("Element is not present in array")
Output :
Element found at index 3.
Program 10
Python program selection sort.
def selectionSort(array, size):
for ind in range(size):
min_index = ind
for j in range(ind + 1, size):
if array[j] < array[min_index]:
min_index = j
(array[ind], array[min_index]) =
(array[min_index], array[ind])
arr = [-2, 45, 0, 11, -9, 88, -97, -202,
747] size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by
selection sort is:')
print(arr)
Output :
The array after sorting in Ascending Order by Selection
Sort is :
[-202,-97,-9,-2,0,11,88,747]
Program 11
Python program insertion sort.
def insertionSort(arr):
if (n := len(arr)) <= 1:
return
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print(arr)
Output :
Sorted Array is :
[5,6,11,12,13]
Program 12
Python program 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 = [12, 11, 13, 5, 6,
7] 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 :
Given Array is :
12 11 13 5 6 7
Sorted Array is :
5 6 7 11 12 13
Program 13
Python program first n prime numbers.
def isPrime(n):
if (n == 1 or n == 0):
return False
for i in range(2, int(n**(1/2))+1):
if (n % i == 0):
return False
return True
N = 100
for i in range(1, N+1):
if (isPrime(i)):
print(i, end=" ")
Output :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83
89 97
The array after sorting in Ascending Order by selection sort
Element is present at index 3
Element is present at index 3