0% found this document useful (0 votes)
18 views2 pages

Python File Handling & Stack Examples

The document contains Python programs for file handling and stack operations. It includes a program to count lines starting with vowels or consonants in a text file and another to count occurrences of 'me' and 'my' in a different text file. Additionally, it features a stack implementation to manage student records based on their marks, allowing records to be pushed or popped accordingly.

Uploaded by

rohanbudhani48
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)
18 views2 pages

Python File Handling & Stack Examples

The document contains Python programs for file handling and stack operations. It includes a program to count lines starting with vowels or consonants in a text file and another to count occurrences of 'me' and 'my' in a different text file. Additionally, it features a stack implementation to manage student records based on their marks, allowing records to be pushed or popped accordingly.

Uploaded by

rohanbudhani48
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

Python Programs - File Handling and Stack (Set 2)

Q1 (Option 1): Count lines starting with a Vowel or Consonant


# Program to count number of lines starting with vowel or consonant in [Link]

def count_lines():
vowels = "AEIOUaeiou"
vowel_count = 0
consonant_count = 0

f = open("[Link]", "r")
lines = [Link]()
[Link]()

for line in lines:


line = [Link]()
if len(line) == 0:
continue # skip empty lines
if line[0] in vowels:
vowel_count += 1
elif line[0].isalpha():
consonant_count += 1

print("Number of lines starting with a vowel:", vowel_count)


print("Number of lines starting with a consonant:", consonant_count)

# --- Main Program ---


count_lines()

Q1 (Option 2): Count 'me' and 'my' in Text File


# Program to count the number of times 'me' or 'my' appears in a text file [Link]

def count_words():
f = open("[Link]", "r")
text = [Link]().lower()
[Link]()

count_me = [Link]().count("me")
count_my = [Link]().count("my")

print("Count of 'me':", count_me)


print("Count of 'my':", count_my)

# --- Main Program ---


count_words()

Q2: Stack of Student Records [Stud_Name, Marks]


MyStack = [] # stack to store student records

# Function to push records if marks > 70


def Push(lst):
if lst[1] > 70:
[Link](lst)
print("Record pushed:", lst)
else:
print("Record not pushed (marks <= 70):", lst)

# Function to pop a record and display details


def Pop():
if len(MyStack) == 0:
print("Stack is empty!")
else:
rec = [Link]()
print("Popped Record:")
print("Student Name:", rec[0])
print("Marks:", rec[1])

# --- Main Program ---


n = int(input("Enter number of student records: "))
for i in range(n):
name = input("Enter Student Name: ")
marks = int(input("Enter Marks: "))
Push([name, marks])

Pop()

You might also like