0% found this document useful (0 votes)
53 views7 pages

Practical No

good
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)
53 views7 pages

Practical No

good
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/ 7

Practical No: 18

Aim: Write a program that inputs a text file. The program should print all
of the unique words in the file in alphabetical order.
Syntax:
def print_unique_words():
# Prompt the user for the file name
file_name = input("Enter the name of the text file (including .txt): ")
try:
# Open the file for reading
with open(file_name, 'r') as file:
# Read the contents of the file
content = file.read()
# Split the content into words (separated by spaces, punctuation, and newlines)
words = content.split()
# Remove punctuation and convert words to lowercase for accurate uniqueness
words = [word.strip('.,!?";:()').lower() for word in words]
# Create a set of unique words
unique_words = set(words)
# Sort the unique words alphabetically
sorted_unique_words = sorted(unique_words)
# Print each unique word
print("\nUnique words in alphabetical order:")
for word in sorted_unique_words:
print(word)
except FileNotFoundError:
print(f"Error: The file {file_name} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
print_unique_words()

2200490
Output:

2200490
Practical No:19
Aim: Write a Python class to convert an integer to a roman numeral.
Syntax:
class IntegerToRoman:
def int_to_roman(self, num):
# List of Roman numerals and corresponding integer values
val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
syb = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
roman_numeral = ' '
# Loop through the values and symbols
for i in range(len(val)):
while num >= val[i]:
roman_numeral += syb[i]
num -= val[i]
return roman_numeral
# Example usage
if __name__ == "__main__":
converter = IntegerToRoman()
# Test the conversion with user input
num = int(input("Enter an integer to convert to Roman numeral: "))
print(f"Roman numeral of {num} is {converter.int_to_roman(num)}")
Output:

2200490
Practical No:20
Aim: Write a Python class to implement pow(x, n).
Syntax:
class Power:
def pow(self, x, n):
result = 1
# Handle negative exponents
if n < 0:
x=1/x
n = -n
# Multiply x by itself n times
for _ in range(n):
result *= x
return result
# Example usage
if __name__ == "__main__":
power_calculator = Power()
# Get input from the user
x = float(input("Enter the base (x): "))
n = int(input("Enter the exponent (n): "))
# Calculate x^n
print(f"{x} raised to the power of {n} is: {power_calculator.pow(x, n)}")
Output:

2200490
Practical No:21
Aim: Write a Python class to reverse a string word by word.
Syntax:
def reverse_words(sentence):
# Split the sentence into words
words = sentence.split()
# Reverse the list of words
reversed_words = words[::-1]
# Join the reversed list back into a string
return ' '.join(reversed_words)
# Example usage
sentence = input("Enter a sentence: ")
# Reverse the sentence word by word
print("Reversed sentence:", reverse_words(sentence))

Output:

2200490
Practical No:17
Aim: Write a script named copyfile.py. This script should prompt the user
for the names of two text files. The contents of the first file should be input
and written to the second file.
Syntax:
# copyfile.py
def main():
try:
# Prompt the user for the names of the two text files
input_file_name = input("Enter the name of the source text file: ")
output_file_name = input("Enter the name of the destination text file: ")
# Read the contents from the input file
with open(input_file_name, 'r') as input_file:
file_contents = input_file.read()
# Write the contents to the output file
with open(output_file_name, 'w') as output_file:
output_file.write(file_contents)
print(f"Contents from '{input_file_name}' have been copied to '{output_file_name}'.")
except FileNotFoundError:
print("Error: One or both files not found. Please check the file names.")
if __name__ == "__main__":
main()
Output:

2200490
2200490

You might also like