Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

S.No Name of Lab Experiment Date of Date of Remarks


Performance Submission

1. To write a python program


that find the maximum of
numbers
2. To write a python program
linear search.
3. To write a python program to
compute the GCD of two
numbers.
4. To write a python program
exponentiation (power of a
number).
5. To write a python program to
perform Matrix
Multiplication.
6. To write a python
program First N prime nos
7. To write a python
program Selection sort
8. To write a python
program Insertion sort
9. To write a python program
find the square root of a
number (Newton‟s method).
10. To write a python program
Binary search.
11. To write a python program
merge sort
12. To write a python program to
find the most frequent words
in a text file.
13. To write a python program
that takes in command line
arguments as input and print
the number of arguments.
14. To write a python program
simulate bouncing ball in
Pygame.

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab – 1

AIM :- To write a python program that 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:-

Enter number of elements in list: 6


Enter elements: 12
Enter elements: 23
Enter elements: 98
Enter elements: 54
Enter elements: 10
Enter elements: 2
Largest element is: 98

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab – 2

AIM :- To write a python program that perform Linear Search.

CODE :-

def search (arr, x):


for j in range (len (arr)):
if arr[j] == x:
return j
return -1

arr = []
num = int(input("Enter number of elements in array: "))
for i in range(0, num):
ele = int(input("Enter element: "))
arr.append(ele)

x = int(input("Enter the element to be searched: "))

res = search(arr, x)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)

OUTPUT :-

Enter number of elements in array: 5


Enter element: 21
Enter element: 43
Enter element: 67
Enter element: 56
Enter element: 90
Enter the element to be searched: 56
Element found at index: 3

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab – 3

AIM :- To write a python program to compute the GCD of two numbers.

CODE :-

def gcd(a, b):


if(b == 0):
return a
else:
return gcd(b, a % b)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("The gcd of a and b is : ", end="")
print(gcd(a, b))

OUTPUT :-

Enter first number: 24


Enter second number: 4
The gcd of a and b is : 4

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab – 4

AIM :- To write a python program to compute the Exponent of number.

CODE :-

def calc_power(N, p):


if p == 0:
return 1
return N * calc_power(N, p-1)
a = int(input("Enter base: "))
b = int(input("Enter power: "))

print(calc_power(a, b))

OUTPUT :-

Enter base: 4
Enter power: 2
16

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab – 5

AIM :- To write a python program to perform Matrix Multiplication.

CODE :-

A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]

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

result = [[sum(a * b for a, b in zip(A_row, B_col))


for B_col in zip(*B)]
for A_row in A]

for r in result:
print(r)

OUTPUT:-

[114, 160, 60, 27]


[74, 97, 73, 14]
[119, 157, 112, 23]

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-6

AIM : To write a python program to Print first n prime numbers.

CODE:

n=int(input("Enter the value of n: "))


for i in range(1,n+1):
if i>1:
for j in range(2,int(i/2 + 1)):
if(i%j==0):
break
else:
print(i)
OUTPUT:

Enter the value of n: 30


2
3
5
7
11
13
17
19
23
2

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-7

AIM : To write a python program to implement selection sort.

CODE:

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, 45, 88, 747]

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-8

AIM : To write a python program to implement insertion sort.

CODE:

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, 78, 1, 32, 23]


insertionSort(arr)
print(arr)

OUTPUT:
[1, 5, 6, 11, 12, 13, 23, 32, 78]

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-9

AIM : To write a python program find the square root of a number (Newton‟s method).

CODE:

def newton_method(n, iters = 100):


a = float(n)
for i in range(iters):
n= 0.5 * (n + a / n)
return n

a=int(input("\nEnter the number: "))


print("\nSquare root of" , a , "is ",newton_method(a))

OUTPUT:

Enter the number: 16

Square root of 16 is 4.0

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-10

AIM : To 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 = [ 2, 3, 4, 5, 10, 23, 40, 43, 56, 70, 78, 91, 100 ]
x = 56

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

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

OUTPUT:

Element is present at index 8

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-11

AIM : To write a python programme 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):


B2(IT-2) SHIVANSH GUPTA 2100910130102
JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)
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

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-12

AIM : To write a python program to find the most frequent words in a text file.

CODE:

file = open("text.txt", "r")


frequent_word = ""
frequency = 0
words = []
print("\nThe given text is:\n")
for line in file:
print(line,end="")
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("\n")
print("Most repeated word: " + frequent_word)
print("Frequency: " + str(frequency))

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

file.close()

OUTPUT:

Most frequent words in a text file:

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-13

AIM: To write a python program that takes in command line arguments as input and
print the number of arguments.

CODE:

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:

Printing the number of arguments :

B2(IT-2) SHIVANSH GUPTA 2100910130102


JSS Academy of Technical Education, Noida Python Language Programing Lab(KCS-453)

Lab-14

AIM: To write a python program simulating bouncing ball in Pygame.

CODE:

import pygame
pygame.init()
width = 1000
height = 600
screen_res = (width, height)

pygame.display.set_caption("GFG Bouncing game")


screen = pygame.display.set_mode(screen_res)

red = (255, 0, 0)
black = (0, 0, 0)

ball_obj = pygame.draw.circle(
surface=screen, color=red, center=[100, 100], radius=40)
speed = [1, 1]

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill(black)
ball_obj = ball_obj.move(speed)
if ball_obj.left <= 0 or ball_obj.right >= width:
speed[0] = -speed[0]
if ball_obj.top <= 0 or ball_obj.bottom >= height:
speed[1] = -speed[1]
pygame.draw.circle(surface=screen, color=red,
center=ball_obj.center, radius=40)
pygame.display.flip()

OUTPUT:

B2(IT-2) SHIVANSH GUPTA 2100910130102

You might also like