About Lesson
Write a Function to Calculate the Factorial of a Number
- Objective: Write a Python function that calculates the factorial of a given number.
- Steps:
- Define the function with one parameter.
- Use a loop or recursion to calculate the factorial.
- Return the result.
python
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print(factorial(5)) # Output: 120
Create a Module with Utility Functions
- Objective: Create a custom module with utility functions and use it in a program.
- Steps:
-
Create a module (
utilities.py
):python
def add(a, b): return a + b def subtract(a, b): return a - b
-
Import and use the module:
python
import utilities print(utilities.add(5, 3)) # Output: 8 print(utilities.subtract(5, 3)) # Output: 2
-