This repository contains Python programs demonstrating functions and the use of modules in Python. These examples show how to create reusable code blocks and leverage Python's built-in math module.
File: factorial_calculator.py
Problem Statement:
- Defines a function named
factorialthat takes a number as an argument - Calculates the factorial using a loop
- Returns the calculated factorial
- Calls the function with a sample number and prints the output
File: math_calculations.py
Problem Statement:
- Asks the user for a number as input
- Uses the
mathmodule to calculate:- Square root of the number
- Natural logarithm (log base e) of the number
- Sine of the number (in radians)
- Displays the calculated results
After completing these exercises, you will understand:
- ✅ How to define and call functions in Python
- ✅ How to use parameters and return values
- ✅ How to import and use Python's built-in modules
- ✅ How to use the
mathmodule for mathematical operations
python-functions-modules-assignment3/
│
├── factorial_calculator.py # Task 1: Factorial calculation using function
├── math_calculations.py # Task 2: Math module calculations
└── README.md # This documentation file
- Python 3.x installed on your computer
- A terminal or command prompt
- A text editor or IDE (VS Code recommended)
python factorial_calculator.pySample Output:
Enter a number: 5
Factorial of 5 is: 120
python math_calculations.pySample Output:
Enter a number: 25
Square root: 5.0
Logarithm: 3.2188758248682006
Sine: -0.13235175009777303
Functions are reusable blocks of code that perform a specific task.
def function_name(parameter):
# Code to execute
return resultExample from Task 1:
def factorial(number):
result = 1
for i in range(1, number + 1):
result = result * i
return resultModules are Python files containing functions and variables that can be reused.
import math # Import the entire module| Function | Description | Example |
|---|---|---|
math.sqrt(x) |
Square root of x | math.sqrt(25) → 5.0 |
math.log(x) |
Natural logarithm of x | math.log(25) → 3.218... |
math.sin(x) |
Sine of x (in radians) | math.sin(25) → -0.132... |
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n:
5! = 5 × 4 × 3 × 2 × 1 = 120
4! = 4 × 3 × 2 × 1 = 24
0! = 1 (by definition)
- Clean, beginner-friendly code with meaningful comments
- Error handling for invalid user input
- Clear output formatting matching expected results
- Well-documented functions with docstrings
Python Course - Module 4: Functions & Modules in Python
Student Assignment
Module 4: Functions & Modules in Python
Happy Coding! 🐍