Functions and modules in python
Task 1: Calculate Factorial Using a Function
Problem Statement: Write a Python program that:
- Defines a function named factorial that takes a number as an argument and calculates its factorial using a loop or recursion.
- Returns the calculated factorial.
- Calls the function with a sample number and prints the output.
This Python program calculates the factorial of a user-provided number using a recursive function.
# Define a function named factorial that takes one argument n
def factorial(n):
if n < 2:
return 1
else:
return n * factorial(n - 1)
-
This defines a recursive function called
factorial
. -
The function works as follows:
- If
n
is less than 2 (i.e., 0 or 1), it returns1
. This is the base case because the factorial of 0 or 1 is always 1. - Otherwise, it multiplies
n
by the factorial ofn - 1
, calling itself. This is the recursive case, which continues until the base case is reached.
- If
n = input("Please enter a number to get factorial : ")
n = int(n)
-
These two lines:
- Ask the user to enter a number.
- Convert the input from a string to an integer using
int()
.
result = factorial(n)
- This line calls the
factorial
function with the user’s input value and stores the result in the variableresult
.
print(f"Factorial of {n} is : ", result)
- This prints the final output to the screen, showing the factorial of the entered number.
The factorial of a number n
(written as n!
) is the product of all positive integers from 1 to n
.
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
3! = 3 × 2 × 1 = 6
0! = 1
(by definition)
Please enter a number to get factorial : 6
Factorial of 6 is : 720
Task 2: Using the Math Module for Calculations
Problem Statement: Write a Python program that:
- Asks the user for a number as input.
- Uses the math module to calculate the: o Square root of the number o Natural logarithm (log base e) of the number o Sine of the number (in radians)
- Displays the calculated results.
This Python program:
-
Takes a number as input from the user.
-
Calculates:
- The square root
- The natural logarithm (log base
e
) - The sine (in radians)
-
Displays the results.
import math
- Imports the built-in
math
module, which provides access to mathematical functions likesqrt()
,log()
, andsin()
.
num = int(input("Please enter a number : "))
- Prompts the user to enter a number.
- The input is read as a string and then converted to an integer using
int()
.
if num > 0:
square_root = math.sqrt(num)
natural_log = math.log(num)
else:
square_root = "Undefined for non-positive numbers"
natural_log = "Undefined for non-positive numbers"
-
Conditional check to ensure
num
is positive:-
If
num > 0
:math.sqrt(num)
calculates the square root.math.log(num)
calculates the natural logarithm (ln
), which is only valid for positive numbers.
-
If
num <= 0
:- Both square root and log are set to text strings explaining they are undefined (since these operations aren't valid for zero or negative numbers in the real number system).
-
sine_value = math.sin(num)
- Calculates the sine of the number, assuming the input is in radians.
- The
math.sin()
function works for any real number (positive, negative, or zero).
print("\nCalculated Results:")
print(f"Square Root of {num} : {square_root}")
print(f"Natural Logarithm of {num} : {natural_log}")
print(f"Sine of {num} radians : {sine_value}")
-
Displays the results in a nicely formatted output using f-strings.
-
The results include:
- Square root
- Natural logarithm (ln)
- Sine value (in radians)
If the user enters 25
, the output will be:
Please enter a number : 25
Calculated Results:
Square Root of 25 : 5.0
Natural Logarithm of 25 : 3.2188758248682006
Sine of 25 radians : -0.13235175009777303