IITM COLLEGE OF ENGINEERING
PRACTICAL FILE
PYTHON PROGRAMMING LAB
LC-CSE-207G
SUBMITTED TO: SUBMITTED BY: AMAN KUMAR
MS. HIMANI REG. NO : 2413071619
COURSE : [Link] (CSE)
SEMESTER & SEC. : III A
DEPARTMENT OF COMPUTER SCIECE AND ENGINEERING
IITM COLLEGE OF ENGINEERIG,BAHADURGARH
AFFILATED TO MAHARSHI DAYANAND UNIVERSITY,ROHTAK
SEESION 2025-26
INDEX
[Link] Practical aim Date of Remarks
experiment
1. Compute gcd of two numbers
2. Find the square root of number
(newton’s method)
3. WAP to exponentiation (power of a
number)
4. Find maximum of a list of numbers
5. WAP to do linear and binary search
6. WAP to do selection sort,Insertion
sort
7. WAP to do merge sort
8. WAP to find first n prime numbers
9. WAP to multiply matrices
10. WAP that take command line
arguments(word count)
11. Find the most frequent words in a
text read from a file
12. Simulate elliptical orbits in Pygame
13. Simulate bouncing ball using Pygame
PRACTICAL 1
AIM :- Compute the GCD of two numbers.
SOURCE CODE
a = int(input("Enter First number:"))
b = int(input("Enter Second number:"))
while b != 0:
temp = b
b = a % b
a = temp
print("GCD is", a)
OUTPUT
PRACTICAL 2
AIM :- Find the square root of a number (Newton’s method).
SOURCE CODE
x = float(input("Enter a number: "))
guess = x / 2
tolerance = 1e-10
while True:
next_guess = 0.5 * (guess + x / guess)
if abs(next_guess - guess) < tolerance:
break
guess = next_guess
print("Square root of", x, "≈", next_guess)
OUTPUT
PRACTICAL 3
AIM :- WAP to find Exponentiation (power of a number).
SOURCE CODE
base = float(input("Enter the base number:
"))
exponent = int(input("Enter the exponent: "))
result = 1
for i in range(exponent):
result = result * base
print("Result =", result)
OUTPUT
PRACTICAL 4
AIM :- Find the maximum of a list of numbers.
SOURCE CODE
numbers = [int(x) for x in input("Enter numbers separated by space:
").split()]
maximum = numbers[0]
for num in numbers:
if num > maximum:
maximum = num
print("The maximum number is:", maximum)
OUTPUT
PRACTICAL 5
AIM :- WAP to do Linear search and Binary search.
SOURCE CODE
numbers = [int(x) for x in input("Enter numbers separated by space (sorted
for binary search): ").split()]
key = int(input("Enter the number to search: "))
print("\nChoose search method:")
print("1. Linear Search")
print("2. Binary Search")
choice = int(input("Enter your choice (1 or 2): "))
if choice == 1:
found = False
for i in range(len(numbers)):
if numbers[i] == key:
print("Number found at position", i + 1)
found = True
break
if not found:
print("Number not found in the list.")
elif choice == 2:
low = 0
high = len(numbers) - 1
found = False
while low <= high:
mid = (low + high) // 2
if numbers[mid] == key:
print("Number found at position", mid + 1)
found = True
break
elif numbers[mid] < key:
low = mid + 1
else:
high = mid - 1
if not found:
print("Number not found in the list.")
else:
print("Invalid choice! Please enter 1 or 2.")
OUTPUT
PRACTICAL 6
AIM :- WAP to do Selection sort, Insertion sort.
SOURCE CODE
numbers = [int(x) for x in input("Enter numbers separated by space:
").split()]
print("\nChoose sorting method:")
print("1. Selection Sort")
print("2. Insertion Sort")
choice = int(input("Enter your choice (1 or 2): "))
if choice == 1:
for i in range(len(numbers) - 1):
min_index = i
for j in range(i + 1, len(numbers)):
if numbers[j] < numbers[min_index]:
min_index = j
numbers[i], numbers[min_index] = numbers[min_index],
numbers[i]
print("Sorted list using Selection Sort:", numbers)
elif choice == 2:
for i in range(1, len(numbers)):
key = numbers[i]
j = i - 1
while j >= 0 and numbers[j] > key:
numbers[j + 1] = numbers[j]
j -= 1
numbers[j + 1] = key
print("Sorted list using Insertion Sort:", numbers)
else:
print("Invalid choice! Please enter 1 or 2.")
OUTPUT
PRACTICAL 7
AIM :- WAP to do Merge sort.
SOURCE CODE
numbers = [int(x) for x in input("Enter numbers separated by space:
").split()]
print("Unsorted list:", numbers)
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
merge_sort(numbers)
print("Sorted list using Merge Sort:", numbers)
OUTPUT
PRACTICAL 8
AIM :- WAP to find First ‘n’ prime numbers.
SOURCE CODE
n = int(input("Enter how many prime numbers you want:
"))
count = 0
num = 2
primes = []
while count < n:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
[Link](num)
count += 1
num += 1
print("First", n, "prime numbers are:")
print(primes)
OUTPUT
Sumit
PRACTICAL 9
AIM :- WAP to Multiply matrices.
SOURCE CODE
r1 = int(input("Enter number of rows in first matrix: "))
c1 = int(input("Enter number of columns in first matrix: "))
print("Enter elements of first matrix rowwise with spaces:")
A = []
for i in range(r1):
row = [int(x) for x in input().split()]
[Link](row)
r2 = int(input("Enter number of rows in second matrix: "))
c2 = int(input("Enter number of columns in second matrix: "))
print("Enter elements of second matrix rowwise with spaces:")
B = []
for i in range(r2):
row = [int(x) for x in input().split()]
[Link](row)
if c1 != r2:
print("Matrix multiplication not possible! (Columns of A must
equal rows of B)")
else:
result = [[0 for j in range(c2)] for i in range(r1)]
for i in range(r1):
for j in range(c2):
for k in range(c1):
result[i][j] += A[i][k] * B[k][j]
print("Resultant Matrix:")
for row in result:
print(row)
OUTPUT
PRACTICAL 10
AIM :- WAP that take command line arguments (word count).
SOURCE CODE
import sys
args = [Link][1:]
text = " ".join(args)
words = [Link]()
print("Total number of words:", len(words))
OUTPUT
PRACTICAL 11
AIM :- Find the most frequent words in a text read from a file.
SOURCE CODE
filename = input("Enter file name: ")
with open(filename, "r") as file:
text = [Link]()
words = [Link]()
freq = {}
for word in words:
word = [Link]()
freq[word] = [Link](word, 0) + 1
max_count = max([Link]())
print("Most frequent word(s):")
for word, count in [Link]():
if count == max_count:
print(f"'{word}' appears {count} times")
OUTPUT
Sumit
PRACTICAL 12
AIM :- Simulate elliptical orbits in Pygame.
SOURCE CODE
import pygame
import math
[Link]()
WIDTH, HEIGHT = 350, 200
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Simple Elliptical Orbit")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (100, 149, 237)
center = (WIDTH // 2, HEIGHT // 2)
a = 150
b = 70
angle = 0
speed = 0.02
running = True
clock = [Link]()
while running:
for event in [Link]():
if [Link] == [Link]:
running = False
[Link](BLACK)
[Link](screen, YELLOW, center, 10)
x = center[0] + a * [Link](angle)
y = center[1] + b * [Link](angle)
[Link](screen, WHITE, (center[0] - a, center[1] - b, 2 * a,
2 * b), 1)
[Link](screen, BLUE, (int(x), int(y)), 10)
angle += speed
if angle > 2 * [Link]:
angle = 0
[Link]()
[Link](60)
[Link]()
OUTPUT
PRACTICAL 13
AIM :- Simulate bouncing ball using Pygame.
SOURCE CODE
import pygame
[Link]()
WIDTH, HEIGHT = 350, 200
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Bouncing Ball")
BLACK = (0, 0, 0)
RED = (255, 0, 0)
x = WIDTH // 2
y = HEIGHT // 2
radius = 15
speed_x = 3
speed_y = 2
trail_surface = [Link]((WIDTH, HEIGHT), [Link])
clock = [Link]()
running = True
while running:
for event in [Link]():
if [Link] == [Link]:
running = False
x += speed_x
y += speed_y
if x - radius <= 0 or x + radius >= WIDTH:
speed_x = -speed_x
if y - radius <= 0 or y + radius >= HEIGHT:
speed_y = -speed_y
trail_surface.fill((0, 0, 0, 25))
[Link](trail_surface, (*RED, 255), (x, y), radius)
[Link](trail_surface, (0, 0))
[Link]()
[Link](60)
[Link]()
OUTPUT