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

Python Lab Programs

Uploaded by

chessaadil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python Lab Programs

Uploaded by

chessaadil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programs for Lab Test Questions

A.1 Program 1 - Display Words Separated by #

Program:
# Python program to read a text file line by line and display each word separated by #
def display_words_with_hash(filename):
try:
with open(filename, 'r') as file:
for line in file:
words = line.split()
print('#'.join(words))
except FileNotFoundError:
print("File not found. Please check the filename and path.")

# Example usage:
# Assuming 'sample.txt' contains:
# Hello world
# Python programming
# display_words_with_hash("sample.txt")

Expected Output:
Hello#world
Python#programming

A.1 Program 2 - Print Pattern

Program:
# Function to print pattern based on line count
def print_pattern(line):
for i in range(1, line + 1):
print("#" * i)

# Example usage:
print_pattern(3)

Expected Output:
#
##
###

A.2 Program - Stack of Students' Marks

Program:
# Class to create a stack for student marks
class StudentMarksStack:
def __init__(self):
self.stack = []

# Function to add marks to stack


def push(self, marks):
self.stack.append(marks)
print(f"Added marks: {marks}")

# Function to remove marks from stack


def pop(self):
if self.stack:
removed_marks = self.stack.pop()
print(f"Removed marks: {removed_marks}")
else:
print("Stack is empty.")

# Function to display stack


def display(self):
print("Current Stack:", self.stack)

# Example usage:
marks_stack = StudentMarksStack()
marks_stack.push(85)
marks_stack.push(90)
marks_stack.display()
marks_stack.pop()
marks_stack.display()

Expected Output:
Added marks: 85
Added marks: 90
Current Stack: [85, 90]
Removed marks: 90
Current Stack: [85]

Project - Bubble Sorting

Program:
# Function to perform bubble sort on a list
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr

# Example usage:
arr = [64, 34, 25, 12, 22, 11, 90]
print("Sorted Array:", bubble_sort(arr))

Expected Output:
Sorted Array: [11, 12, 22, 25, 34, 64, 90]

Project - Insertion Sorting

Program:
# Function to perform insertion sort on a list
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr

# Example usage:
arr = [64, 34, 25, 12, 22, 11, 90]
print("Sorted Array:", insertion_sort(arr))
Expected Output:
Sorted Array: [11, 12, 22, 25, 34, 64, 90]

Project - Palindrome Check

Program:
# Function to check if a string is a palindrome
def is_palindrome(s):
return s == s[::-1]

# Example usage:
print(is_palindrome("madam")) # True
print(is_palindrome("hello")) # False

Expected Output:
True
False

Project - Armstrong Number

Program:
# Function to check if a number is an Armstrong number
def is_armstrong(number):
order = len(str(number))
sum_of_powers = sum(int(digit) ** order for digit in str(number))
return sum_of_powers == number

# Example usage:
print(is_armstrong(153)) # True
print(is_armstrong(123)) # False

Expected Output:
True
False

You might also like