Python Lab Manual Cse Format
Python Lab Manual Cse Format
ANDREWS INSTITUTE
OF TECHNOLOGY & MANAGEMENT
Bachelor of Technology
A Practical File
Subject Code-LC-CSE-215G
ROLL
NO.:
St. Andrews Institute of Technology &
Management, Gurugram
Department of……………………………
Practical Lab Evaluation Sheet
Average Marks
SOURCE CODE
import math
def compute_gcd(a, b):
gcd_value = math.gcd(a, b)
return gcd_value
# Example numbers
num1 = 60
num2 = 48
gcd_result = compute_gcd(num1, num2)
print(f"The GCD of {num1} and {num2} is: {gcd_result}")
OUTPUT
Program 2: Find the square root of a number Newton‘s method)
SOURCE CODE
x = N / 2 if N > 1 else N
for _ in range(max_iterations):
x_next = (x + N / x) / 2
return x_next
x = x_next
return x
N = 25
result = sqrt_newton(N)
OUTPUT
⮚ Program 3: Exponentiation (power of a number)
SOURCE CODE
base = 8
exponent = 3
OUTPUT
⮚ Program 4: Find the maximum of a list of numbers
SOURCE CODE
numbers = [15,78,89,58,45]
numbers.sort()
max_value = numbers[-1]
OUTPUT
⮚ Program 5: Linear search and Binary search
LINEAR SEARCH
SOURCE CODE
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
target = 78
if result != -1:
else:
OUTPUT
BINARY SEARCH
SOURCE CODE
def binary_search(arr, target):
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
arr = [14,69,56,15,69,69]
target = 69
OUTPUT
⮚ Program 6: Selection sort, Insertion sort
SELECTION SORT
SOURCE CODE
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
OUTPUT
INSERTION SORT
SOURCE CODE
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
arr = [65,78,98,91,12,8]
insertion_sort(arr)
print("Sorted array:", arr)
OUTPUT
⮚ Program 7: Merge sort
SOURCE CODE
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left_half = merge_sort(arr[:mid])
right_half = merge_sort(arr[mid:])
result.extend(left[i:])
result.extend(right[j:])
return result
# Example usage:
arr = [38, 27, 43, 3, 9, 82, 10]
sorted_arr = merge_sort(arr)
print("Sorted array:", sorted_arr)
OUTPUT
⮚ Program 8: First n prime numbers
SOURCE CODE
def is_prime(num):
if num <= 1:
return False
if num % i == 0:
return False
return True
def first_n_primes(n):
primes = []
num = 2
if is_prime(num):
primes.append(num)
num += 1
return primes
n = 10
print(first_n_primes(n))
OUTPUT
⮚ Program 9: Multiply matrices
SOURCE CODE
m = len(A)
n = len(A[0])
p = len(B[0])
for i in range(m):
for j in range(p):
for k in range(n):
return C
A = [[1, 2, 3],
[4, 5, 6]]
B = [[7, 8],
[9, 10],
[11, 12]]
C = multiply_matrices(A, B)
for row in C:
print(row)
OUTPUT
⮚ Program 10: Programs that take command line arguments (word
count)
SOURCE CODE
import sys
import string
def word_count_from_args():
if len(sys.argv) < 2:
return
# Combine all the command line arguments into a single string (excluding the script
name)
cleaned_text = input_text.translate(translator)
words = cleaned_text.split()
word_counts = Counter(words)
print("Word counts:")
for word, count in word_counts.items():
print(f"{word}: {count}")
if __name__ == "__main__":
word_count_from_args()
OUTPUT
⮚ Program 11: Find the most frequent words in a text read from a
file
SOURCE CODE
import string
# Function to read the text from the file and find the most frequent words
try:
except FileNotFoundError:
return
text_cleaned = text.translate(translator)
words = text_cleaned.split()
word_counts = Counter(words)
most_common = word_counts.most_common(top_n)
print(f"{word}: {count}")
# Example usage
find_most_frequent_words(filename, top_n=10)
Python is a great programming language. Python is easy to learn, and Python has a
rich ecosystem.
Python is used for web development, data analysis, artificial intelligence, and more.
OUTPUT
⮚ Program 12: Simulate elliptical orbits in Pygame
SOURCE CODE
import pygame
import math
import sys
# Initialize Pygame
pygame.init()
# Constants
# Ellipse parameters
# Planet parameters
# Simulation loop
clock = pygame.time.Clock()
while True:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Update the angle for the next frame (simulate constant angular velocity)
screen.fill(WHITE)
pygame.display.flip()
clock.tick(FPS)
OUTPUT
⮚ Program 13: Simulate bouncing ball using Pygame
SOURCE CODE
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
# Gravity constant
ball_y = HEIGHT // 2
# Simulation loop
clock = pygame.time.Clock()
while True:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
ball_velocity_y += GRAVITY
ball_x += ball_velocity_x
ball_y += ball_velocity_y
# Ball collision with the left and right walls (horizontal boundaries)
screen.fill(BACKGROUND_COLOR)
pygame.display.flip()
clock.tick(FPS)
OUTPUT