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

Python Lab

Uploaded by

rrrroptv
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)
10 views

Python Lab

Uploaded by

rrrroptv
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/ 3

53. Create a module to check whether a number is a prime or not.

Write a program to find


the prime number between two limits using this module.
Ans: The module: primechecker.py
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
The program:
from PrimeCheckerModule import is_prime

def find_primes_in_range(start, end):


prime_numbers = []
for num in range(start, end + 1):
if is_prime(num):
prime_numbers.append(num)
return prime_numbers

if __name__ == "__main__":
start_limit = int(input("Enter the start limit: "))
end_limit = int(input("Enter the end limit: "))
primes_in_range = find_primes_in_range(start_limit, end_limit)
if primes_in_range:
print(f"Prime numbers between {start_limit} and {end_limit}:")
print(primes_in_range)
else:
print(f"No prime numbers found between {start_limit} and {end_limit}.")

54. Create a module to find the factorial of a number and import the module from the main
program to find the factorial of a given number.
Ans: The module: FactorialModule.py
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
The program:
from FactorialModule import factorial
if __name__ == "__main__":
num = int(input("Enter a number to find its factorial: "))
result = factorial(num)
print(f"The factorial of {num} is: {result}")
55. Write a program to find the mean, median, and standard deviation of a list of random
numbers between 1 and 10.
Ans: Required program:
import random
import statistics

def generate_random_numbers(n):
return [random.randint(1, 10) for _ in range(n)]

if __name__ == "__main__":
random_numbers = generate_random_numbers(10)
mean_value = statistics.mean(random_numbers)
median_value = statistics.median(random_numbers)
std_deviation = statistics.stdev(random_numbers)
print("Generated random numbers:", random_numbers)
print("Mean:", mean_value)
print("Median:", median_value)
print("Standard Deviation:", std_deviation)

56. Write a program to shuffle elements of a list of random numbers between given
ranges.
Ans:
import random
def generate_random_numbers(n, lower_limit, upper_limit):
return [random.randint(lower_limit, upper_limit) for _ in range(n)]
if __name__ == "__main__":
n = int(input("Enter the number of elements: "))
lower_limit = int(input("Enter the lower limit: "))
upper_limit = int(input("Enter the upper limit: "))
random_numbers = generate_random_numbers(n, lower_limit, upper_limit)
print("Original list:", random_numbers)
random.shuffle(random_numbers)
print("Shuffled list:", random_numbers)

57. Write a program to create a list of random numbers using list comprehension.
Ans:
import random
def generate_random_numbers(n, lower_limit, upper_limit):
return [random.randint(lower_limit, upper_limit) for _ in range(n)]
if __name__ == "__main__":
n = int(input("Enter the number of elements: "))
lower_limit = int(input("Enter the lower limit: "))
upper_limit = int(input("Enter the upper limit: "))
random_numbers = generate_random_numbers(n, lower_limit, upper_limit)
print("Generated list of random numbers:", random_numbers)

__________________________________________________________________________________
Exception Handling
58. Write a program to read a number from the user. If the number is positive or zero,
print it, otherwise raise an exception.
Ans:
try:
number = float(input("Enter a number: "))
if number >= 0:
print("Entered number:", number)
else:
raise ValueError("Entered number is negative.")

except ValueError as e:
print(f"Error: {e}")

59. Write a program to read two numbers from the user and perform basic mathematical
operations (addition, multiplication, subtraction, division) by handling all possible
exceptions.
Ans:
def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y == 0:
raise ValueError("Cannot divide by zero.")
return x / y
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result_addition = add(num1, num2)
result_subtraction = subtract(num1, num2)
result_multiplication = multiply(num1, num2)
result_division = divide(num1, num2)
print(f"Addition: {result_addition}")
print(f"Subtraction: {result_subtraction}")
print(f"Multiplication: {result_multiplication}")
print(f"Division: {result_division}")
except ValueError as ve:
print(f"Error: {ve}")
except Exception as e:
print(f"An unexpected error occurred: {e}")

You might also like