Page 1 of 19
PROGRAM:-01
OBJECT:- Python program THAT TAKES IN
COMMOND LINE ARGUMENTS AS INPUT AND
PRINT THE NUMBER OF ARGUMENTS
SOURCE CODE:-
# total arguments
n = len(sys.argv)
print("Total arguments passed:", n)
# Arguments passed
print("\nName of Python script:", sys.argv[0])
print("\nArguments passed:", end = " ")
for i in range(1, n):
print(sys.argv[i], end = " ")
# Addition of numbers
Sum = 0
# Using argparse module
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
OUTPUT:-
Page 2 of 19
PROGRAM:-02
OBJECT:- Program to multiply two
matrices using nested loops
Page 3 of 19
SOURCE CODE:-
# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
Page 4 of 19
print(r)
OUTPUT:-
PROGRAM:-03
OBJECT:- Python program to find G.C.D of
two numbers
Page 5 of 19
SOURCE CODE:-
# define a function
def compute_hcf(x, y):
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = 12
num2 = 6
print("The H.C.F. is", compute_hcf(num1, num2))
OUTPUT:-
PROGRAM:-04
OBJECT:- Python program to find the
most FREQUENT word in a text file
Page 6 of 19
SOURCE CODE:-
# A file named "gfg", will be opened with the
# reading mode.
file = open("gfg.txt","r")
frequent_word = ""
frequency = 0
words = []
# Traversing file line by line
for line in file:
# splits each line into
# words and removing spaces
# and punctuations from the input
line_word =
line.lower().replace(',','').replace('.','').split("
");
# Adding them to list words
for w in line_word:
words.append(w);
# Finding the max occurred word
for i in range(0, len(words)):
# Declaring count
count = 1;
# Count each word in the file
for j in range(i+1, len(words)):
if(words[i] == words[j]):
count = count + 1;
# If the count value is more
# than highest frequency then
if(count > frequency):
frequency = count;
frequent_word = words[i];
Page 7 of 19
print("Most repeated word: " + frequent_word)
print("Frequency: " + str(frequency))
file.close();
OUTPUT:-
PROGRAM:-05
Page 8 of 19
OBJECT:-Python Program to FIND THE
SQUARE ROOT OF A number(newton ’s
method)
Source code:-
# Note: change this value for a different result
num = 8
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
# Find square root of real or complex numbers
# Importing the complex math module
import cmath
num = 1+2j
# To take input from the user
#num = eval(input('Enter a number: '))
num_sqrt = cmath.sqrt(num)
Page 9 of 19
print('The square root of {0} is {1:0.3f}
+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
output:-
Program:-06
Page 10 of 19
Object:- Python program to find
maximum of a list of numbers
source code:-
# Creating an empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list:
"))
# Iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# Printing maximum element
print("Largest element is:", max(list1))
output:
Program:-07
Page 11 of 19
Object:- Linear Search program in Python
Source code:-
def linearSearch(array, n, x):
# Going through array sequencially
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:-
program:-08
object:-Binary Search program in python
Page 12 of 19
source code:-
def binarySearch(array, x, low, high):
# Repeat until the pointers low and high meet each other
while low <= high:
mid = low + (high - low)//2
if array[mid] == x:
return mid
elif array[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
array = [100,150,115,230,225]
x=4
result = binarySearch(array, x, 0, len(array)-1)
Page 13 of 19
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
output:-
Program:-09
Object:- Insertion sort program in Python
Source code:-
Page 14 of 19
def insertionSort(array):
for step in range(1, len(array)):
key = array[step]
j = step - 1
# Compare key with each element on the left of it until an element
smaller than it is found
# For descending order, change key<array[j] to key>array[j].
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j=j-1
# Place key at after the element just smaller than it.
array[j + 1] = key
data = [8, 5, 10, 2, 13]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)
Selection sort program in Python
def selectionSort(array, size):
for step in range(size):
min_idx = step
Page 15 of 19
for i in range(step + 1, size):
# to sort in descending order, change > to < in this line
# select the minimum element in each loop
if array[i] < array[min_idx]:
min_idx = i
# put min at the correct position
(array[step], array[min_idx]) = (array[min_idx], array[step])
data = [12,4,3,16,13,9,5]
size = len(data)
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data) output:-
Program:-10
Object:-merge sort program in python
SOURCE CODE:-
Page 16 of 19
def mergeSort(myList):
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
# Iterator for the main list
k=0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
# The value from the left half has been used
myList[k] = left[i]
# Move the iterator forward
i += 1
else:
myList[k] = right[j]
j += 1
# Move to the next slot
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
k += 1
myList = [4,10,9,2,6,13]
Page 17 of 19
mergeSort(myList)
print(myList)
OUTPUT:-
INDEX
S.NO PROGRAMS DATE PAGE NO. SIGNATURE
1. Python program that 1
take in command
Page 18 of 19
line argumants as
input and print the
number of
argumants.
2. Python program to 3
parform matrix
multiplication.
3. Python program to 4
compute the GCD of
two number.
4. Python program to 6
find the most
frequent words in
text file.
5 Python program find 8
to square root of a
number.
6 Python program find 9
the maximum of a
list of numbers.
7 Linear search python 10
program.
8 Binary search 12
python program.
9 Selection and 14
insertion sort
program.
10. Merge sort python 16
program.
Page 19 of 19