0% found this document useful (0 votes)
86 views

Lab File: Department of Computer Science & Engineering

Tanishq Sharma submitted their Python Programming Lab file for the session 2020-21, semester 4. The file details 8 programs completed across 12 experiments: 1) Matrix multiplication 2) GCD of two numbers 3) Finding most frequent words in a text file 4) Square root using Newton's method 5) Exponentiation 6) Finding max of a list 7) Linear and binary search 8) Selection, insertion, and merge sort 9) First n prime numbers 10) Creating a pygame program. For each experiment, the code, input, and output is documented.

Uploaded by

prashant singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Lab File: Department of Computer Science & Engineering

Tanishq Sharma submitted their Python Programming Lab file for the session 2020-21, semester 4. The file details 8 programs completed across 12 experiments: 1) Matrix multiplication 2) GCD of two numbers 3) Finding most frequent words in a text file 4) Square root using Newton's method 5) Exponentiation 6) Finding max of a list 7) Linear and binary search 8) Selection, insertion, and merge sort 9) First n prime numbers 10) Creating a pygame program. For each experiment, the code, input, and output is documented.

Uploaded by

prashant singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Department of Computer Science & Engineering

Affiliated to Dr. A.P.J. AKTU, Lucknow

LAB FILE
Subject Name : Python Language
Programming Lab
Subject Code : KCS 453
Session : 2020-21 (EVEN
Semester SEMESTER) – 4TH

Submitted By:-
Name : TANISHQ SHARMA
Roll No. : 1902900100169
Section : 2CS-C
Lab Ref. Name of experiment Date Signature
No. Pt.

To write a python program to perform Matrix 07.04.21


1.1
Multiplication.
1 07.04.21
To write a python program to compute the
1.2
GCD of two numbers.
14.04.21
To write a python program to find the most
2 2
frequent words in a text file.
21.04.21
To write a python program find the square root
3.1
of a Number (Newton’s method).
3 3.2 21.04.21
To write a python program exponentiation
(power of a number).
28.04.21
To write a python program find the maximum
4 4
of a list of numbers.
05.05.21
5.1 To write a python program linear search.
5
05.05.21
5.2 To write a python program Binary search.
26.05.21
6.1 To write a python program selection sort.
6 26.05.21
To write a python program Insertion sort.
6.2

02.06.21
7 7 To write a python program merge sort..

02.06.21
To write a python program first n prime
8 8
numbers.
9 9 02.06.21
Write a python program To create a pygame
PROGRAM:-1(A)

AIM :- Write a python program to perform Matrix


Multiplication.

CODE :-

matrix_1 = [[ 1 , 2 , 4 ],
[ 7 , 5 , 8 ],
[ 3 , 6 , 9 ]]
matrix_2= [[ 1 , 12, 3 ],
[ 2 , 5 , 7 ],
[ 10 , 0 , 1 ]]
r e s u l t = [ [ 0 , 0 , 0 ],
[ 0 , 0 , 0 ],
[ 0 , 0 , 0 ]]
f o r i in range( len( matrix_ 1 )):

f o r j in range( len( matrix_ 2 [ 0 ] ) ) :

f o r k in range( len( matrix_ 2 )):

r e s u l t [ i ] [ j ] += m a t r i x _ 1 [ i ] [ k ] * m a t r i x _ 2 [ k ] [ j ]
p r i n t ( " M u l t i p l i c a t i o n o f two Matrix i s : " )

f o r j in r e s u l t :
p r i n t (j)
OUTPUT:-
PROGRAM:-1(B)

AIM :- Write a python program to compute the GCD of


Two numbers.

CODE :-

def compute_gcd(a, b):


while(b):
a, b = b, a % b
return a

num1 = int(input("Enter first number\n"))


num2 = int(input("Enter second
number\n"))
gcd = compute_gcd(num1,num2)
print("the GCD of two number is:",gcd)

OUTPUT:-
PROGRAM:-2

AIM :- Write a python program to find the most frequent


words in a text file.

CODE:-

count = 0
word = ""
maxCount = 0
words = []
file = open("programing.txt", "r")
for line in file:
string = line.lower().replace(',',
'').replace('.', '').split(" ")
for s in string:
words.append(s)
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 > maxCount):
maxCount = count
word = words[i]
print("Most repeated word: " + word)
file.close()

OUTPUT:-

Most repeated word: Tanishq


PROGRAM:-3(A)

AIM :- write a python program find the square root of a


Number (Newton’s method).

CODE :-

def newton_method(number,
number_iters=100):
x= float(number)
for i in range(number_iters):
number = 0.5 * (number + x /
number)
return number

num= int(input("Enter number:"))


square_root= newton_method(num)
print("Square root of the
number:",square_root)

OUTPUT:-
PROGRAM:-3(B)

AIM :- Write a python program exponentiation (power of


a number).

CODE
number = int(input(" Please Enter any
Positive Integer : "))
exponent = int(input(" Please Enter
Exponent Value : "))
power = 1
for i in range(1, exponent + 1):
power = power * number
print(f"The Result of {number} power
{exponent} is {power}")

OUTPUT:-
PROGRAM:-4

AIM :- Write a python program find the maximum of a list


of numbers.

CODE :-

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:-
PROGRAM:-5(A)

AIM :- Write a python program linearsearch.

CODE :-

def linearsearch(arr, x):


fori in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = []
num = int(input("Enter number of elements
in list: "))
for i in range(1, num + 1):
ele = input("Enter elements: ")
arr.append(ele)
x= input("element to be searched : ")
print("element found at index
",linearsearch(arr,x))

OUTPUT:-
PROGRAM:-5(B)

AIM :- Write a python program Binary search.

CODE :-

def binary_search(arr, low, high, x):


if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1,
x)
else:
return binary_search(arr, mid + 1, high,
x)
else:
return -1

arr = []
num = int(input("Enter number of elements in list:
"))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
arr.append(ele)
x = int(input("Enter the element to be searched"))

result = binary_search(arr, 0, num - 1, x)

OUTPUT:-
Enter number of elements in list: 4
Enter elements: 41
Enter elements: 60
Enter elements: 43
Enter elements: 13
Enter the element to be searched 43
Element is present at index 2
PROGRAM:-6(A)

AIM :- Write a python program selection sort.

CODE :-

A= []

num = int(input("Enter number of elements


in list: "))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
A.append(ele)

for i in range(len(A)):
min_idx = i
for j in range(i + 1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i]

print("Sorted array")

for i in range(len(A)):
print("%d" % A[i])
OUTPUT:-
PROGRAM:-6(B)

AIM :- Write a python program Insertion sort.

CODE:-

def insertionSort(a):
for i in range(1, len(a)):
value = a[i]
flag = i
while(flag > 0 and a[flag-1] >
value):
a[flag] = a[flag-1]
flag -= 1

a[flag] = value

arr = []
num = int(input("Enter number of elements
in list: "))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
arr.append(ele)

insertionSort(arr)

print("Sorted array is:")


for i in range(len(arr)):
print("%d" % arr[i])
OUTPUT:-
PROGRAM:-7

AIM :- Write a python program merge sort.

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)

arr = [12, 11, 13, 5, 6, 7]


n = len(arr)
print("Given array is")
for i in range(n):
print("%d" % arr[i]),

mergeSort(arr, 0, n - 1)
print("\n\nSorted array is")
for i in range(n):
print("%d" % arr[i])

OUTPUT:-
PROGRAM:-8

AIM :- Write a python program first n prime numbers.

CODE :-

x = int(input("Enter the number:"))


print("prime numbers are ")
i = 1
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:-8

AIM :- Write a python program to create a pygame.

CODE :-
OUTPUT:-

You might also like