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

Latest Python Programs Solution Class XII

Uploaded by

niti2007jain
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)
28 views

Latest Python Programs Solution Class XII

Uploaded by

niti2007jain
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
You are on page 1/ 6

Q1.

Read a text file line by line and display


each word separated by a #.

def
display_words_separated_by_hash(file_path):
try:
with open(file_path, 'r') as file:
lines = file.readlines()
for line in lines:
words = line.split()
formatted_line = '#'.join(words)
print(formatted_line)
except Exception as error:
print("An error occurred:”
+str(error))
file_path = “your_file_path.txt”
display_words_separated_by_hash(file_path)
Q 2.Read a text file and display the number of vowels
/consonants/uppercase/lowercase characters in the file.

def count_characters(text):
vowels = 'aeiouAEIOU'
consonants =
'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
upper_count = 0
lower_count = 0
vowel_count = 0
consonant_count = 0
for char in text:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
if char in vowels:
vowel_count += 1
elif char in consonants:
consonant_count += 1
return upper_count, lower_count, vowel_count,
consonant_count
file_name = "your_file_path.txt"
with open(file_name, 'r') as file:
text = file.read()
upper_count, lower_count, vowel_count, consonant_count =
count_characters(text)
print("Number of uppercase characters:” +str(upper_count))
print("Number of lowercase characters:”+str(lower_count))
print("Number of vowels: +str(vowel_count))
print("Number of consonants: +str(consonant_count))
Q 3. Remove all the lines that contain the character
‘a’ in a file and write it to another file.

# Specify the input and output file names


input_file_name = 'your_file_path.txt' # Replace
with the name of your input file
output_file_name = 'output.txt' # Replace with the
name of your output file
# Open the input file for reading and the output file
for writing
with open(input_file_name, 'r') as input_file,
open(output_file_name, 'w') as output_file:
# Read each line from the input file
for line in input_file:
# Check if the line contains the character 'a'
(case-insensitive)
if 'a' not in line.lower():
# If 'a' is not found in the line, write it to the
output file
output_file.write(line)
# Print a message to indicate the task is complete
print("Lines containing 'a' removed and saved to",
output_file_name)
Q 6.Write a random number generator that
generates random numbers between 1 to 6
(simulates a dice)

import random
# Simulate a dice roll
def roll_dice():
return random.randint(1, 6)
# Roll the dice and print the result
dice_result = roll_dice()
print("You rolled a", dice_result)
Q 7.Write a python program to implement a stack
using list.

class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
print("Stack is empty. Cannot pop.")
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
print("Stack is empty. Nothing to peek.")
def size(self):
return len(self.items)
# Example usage of the stack
stack = Stack()
# Push some items onto the stack
stack.push(1)
stack.push(2)
stack.push(3)
# Print the stack size
print("Stack size:", stack.size())
# Peek at the top item
print("Top item:", stack.peek())
# Pop items from the stack
print("Popped item:", stack.pop())
print("Popped item:", stack.pop())
print("Popped item:", stack.pop())
# Check if the stack is empty
print("Is the stack empty?", stack.is_empty())

You might also like