ASSIGNMENT NO.
1. Create a module calc.py with 2 functions addition and
subtraction that two numeric values as parameters. Write a
python program to import the value and call appropriate
functions.
PROGRAM :
def addition(a, b):
"""Function to add two numbers."""
return a + b
def subtraction(a, b):
"""Function to subtract two numbers."""
return a - b
print("Enter two numeric values:")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result_addition = addition(num1, num2)
print(f"The addition of {num1} and {num2} is:
{result_addition}")
result_subtraction = subtraction(num1, num2)
print(f"The subtraction of {num1} from {num2} is:
{result_subtraction}")
OUTPUT:
Enter two numeric values:
Enter first number: 55
Enter second number: 56
The addition of 55.0 and 56.0 is: 111
The subtraction of 55.0 from 56.0 is: -1.0
2. Create a python module that has functions to calculate sin
value, cos value, square root of value, power of value of a
particular value entered by the user. Use in-built math
module. Write a python code to import the value and call
appropriate functions as per choice of the user.
PROGRAM :
import math
def sin_value(x):
return math.sin(x)
def cos_value(x):
return math.cos(x)
def square_root(x):
if x >= 0:
return math.sqrt(x)
else:
return "Square root not defined for negative numbers"
def power(base, exponent):
return math.pow(base, exponent)
print("Choose an operation:")
print("1. Sine value")
print("2. Cosine value")
print("3. Square root")
print("4. Power of a number")
choice = int(input("Enter your choice (1/2/3/4): "))
if choice == 1:
x = float(input("Enter the value (in radians) to find the sine:
"))
print(f"Sin({x}) = {sin_value(x)}")
elif choice == 2:
x = float(input("Enter the value (in radians) to find the
cosine: "))
print(f"Cos({x}) = {cos_value(x)}")
elif choice == 3:
x = float(input("Enter the value to find the square root: "))
print(f"Square root of {x} = {square_root(x)}")
elif choice == 4:
base = float(input("Enter the base: "))
exponent = float(input("Enter the exponent: "))
print(f"{base} raised to the power {exponent} =
{power(base, exponent)}")
else:
print("Invalid choice. Please choose a valid option.")
OUPUT:
Choose an operation:
1. Sine value
2. Cosine value
3. Square root
4. Power of a number
Enter your choice (1/2/3/4): 1
Enter the value (in radians) to find the sine: 55
Sin(55.0) = -0.999755173
Enter your choice (1/2/3/4): 2
Enter the value (in radians) to find the cosine: 55
Cos(55.0) = 0.0221267563
Enter your choice (1/2/3/4): 3
Enter the value to find the square root: 55
Square root of 55.0 = 7.41619849
Enter your choice (1/2/3/4): 4
Enter the base: 55
Enter the exponent: 55
55.0 raised to the power 55.0 = 5.247445324687519e+95
3. Write a python program to print current year month and
today’s date using built-in timedate module.
PROGRAM :
import datetime
today = datetime.date.today()
print("Current year:", today.year)
print("Current month:", today.month)
print("Today's date:", today.day)
OUTPUT:
Current year: 2024
Current month: 11
Today's date: 11
4. Develop a module named calculator.py in python, which
contains 2 functions:
Factorial (n): To find factorial of a number
Fibonacci (n): To find Fibonacci series up to given number n
Perform the following tasks:-
Import developed module
Call the functions available in the module
PROGRAM :
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
def fibonacci(n):
fib_series = []
a, b = 0, 1
while a <= n:
fib_series.append(a)
a, b = b, a + b
return fib_series
print("Choose an operation:")
print("1. Factorial")
print("2. Fibonacci Series")
choice = int(input("Enter your choice (1/2): "))
if choice == 1:
num = int(input("Enter a number to find its factorial: "))
print(f"Factorial of {num} is {factorial(num)}")
elif choice == 2:
num = int(input("Enter a number to generate Fibonacci
series up to: "))
print(f"Fibonacci series up to {num}: {fibonacci(num)}")
else:
print("Invalid choice. Please select 1 or 2.")
OUTPUT:
Choose an operation:
1. Factorial
2. Fibonacci Series
Enter your choice (1/2): 1
Enter a number to find its factorial: 55
Factorial of 55 is :
1551118753287382280224243016469303211063259720016
986112000000000000
Choose an operation:
1. Factorial
2. Fibonacci Series
Enter your choice (1/2): 2
Enter a number to generate Fibonacci series up to: 55
Fibonacci series up to 55: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.
5. Create a package in python named interest and within this
package create 2 modules simple.py and compound.py. The
simple.py modules contain function simple-interest() and
compound.py contains function compound-interest().
PROGRAM :
def simple_interest(principal, rate, time):
"""Function to calculate Simple Interest"""
si = (principal * rate * time) / 100
return si
def compound_interest(principal, rate, time, n):
"""Function to calculate Compound Interest"""
# A = P (1 + r/n)^(nt)
amount = principal * (1 + rate/(n*100))**(n*time)
ci = amount - principal
return ci
print("Choose the type of interest calculation:")
print("1. Simple Interest")
print("2. Compound Interest")
choice = int(input("Enter your choice (1/2): "))
if choice == 1:
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time (in years): "))
si = simple_interest(principal, rate, time)
print(f"Simple Interest: {si}")
elif choice == 2:
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time (in years): "))
n = int(input("Enter the number of times interest is
compounded per year: "))
ci = compound_interest(principal, rate, time, n)
print(f"Compound Interest: {ci}")
else:
print("Invalid choice. Please select 1 or 2.")
OUTPUT:
Choose the type of interest calculation:
1. Simple Interest
Choose the type of interest calculation:
1. Simple Interest
2. Compound Interest
Enter your choice (1/2): 1
Enter the principal amount: 55000
Enter the rate of interest: 5500
Enter the time (in years): 55
Simple Interest: 166,375,000
Choose the type of interest calculation:
1. Simple Interest
2. Compound Interest
Enter your choice (1/2): 2
Enter the principal amount: 55000
Enter the rate of interest: 5500
Enter the time (in years): 55
Enter the number of times interest is compounded per year:
2
Compound Interest: 7.562220173307365e+149