FDS Week 2 Practical
FDS Week 2 Practical
1. Write a Python script that prints prime numbers less than 20.
Program:
def is_prime(num):
if num <= 1:
return False
if num % i == 0:
return False
return True
def print_primes_less_than_n(n):
if is_prime(num):
print(num)
print_primes_less_than_n(20)
output:
2. Write a python program to find factorial of a number using Recursion.
Program:
def factorial(n):
if n < 0:
elif n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
result = factorial(number)
Output:
3. Write a program that accepts the lengths of three sides of a triangle as inputs. The program
output should indicate whether or not the triangle is a right triangle (Recall from the
Pythagorean Theorem that in a right triangle, the square of one side equals the sum of the
squares of the other two sides).
Program:
try:
print("The triangle with sides {:.2f}, {:.2f}, and {:.2f} is a right triangle.".format(side1,
side2, side3))
else:
print("The triangle with sides {:.2f}, {:.2f}, and {:.2f} is not a right triangle.".format(side1,
side2, side3))
except ValueError:
print("Please enter valid numerical values for the lengths of the sides.")
Output:
4. Write a python program to define a module to find Fibonacci Numbers and import the module
to another program.
Program:
# fibonacci.py
def fibonacci(n):
if n < 0:
elif n == 0:
return 0
elif n == 1:
return 1
else:
a, b = 0, 1
a, b = b, a + b
return b
def fibonacci_sequence(n):
if n < 0:
sequence = []
for i in range(n):
sequence.append(fibonacci(i))
return sequence
Output:
5. Write a python program to define a module and import a specific function in that module to
another program.
Program:
def square(num):
return num ** 2
# main.py
def main():
try:
result = square(number)
except ValueError:
if __name__ == "__main__":
main()
Output:
6. 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.
Program:
def copy_file_contents():
"""Copy contents from one file to another."""
# Prompt the user for the names of the files
source_file = input("Enter the name of the source file (including .txt): ")
destination_file = input("Enter the name of the destination file (including .txt): ")
try:
# Open the source file for reading
with open(source_file, 'r') as src:
contents = src.read() # Read the contents of the source file
except FileNotFoundError:
print(f"Error: The file '{source_file}' does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
copy_file_contents()
Output:
7. Write a program that inputs a text file. The program should print all of the unique words in
Program:
def extract_unique_words(file_name):
"""Extract unique words from a text file and return them in alphabetical order."""
try:
contents = file.read()
# Split the contents into words, removing punctuation and converting to lowercase
words = contents.split()
sorted_unique_words = sorted(unique_words)
return sorted_unique_words
except FileNotFoundError:
return []
except Exception as e:
return []
def main():
file_name = input("Enter the name of the text file (including .txt): ")
if unique_words:
print(word)
if __name__ == "__main__":
main()
Output:
again
hello
is
test
this
world