Python modules are like toolboxes.
Instead of writing every single function from scratch, you can
"import" these toolboxes to use pre-written code.
Think of a module as a separate .py file containing functions and variables that you can use in your
main program.
Built-in Modules
Python comes with a "Standard Library," which is a collection of modules available out of the box.
You don't need to install them; you just import them.
Built-ins: Learn print(), input(), and len().
Standard Library: Learn math, random, and os.
Custom Modules: Practice writing your own .py file and importing it into another.
Third-Party: Learn how to use pip to install requests or pandas.
1. The Math Module
The math module provides access to mathematical functions defined by the C standard. It is essential
for anything involving trigonometry, logarithms, or advanced constants.
Key Functions:
[Link](x): Rounds a number up to the nearest integer.
[Link](x): Rounds a number down to the nearest integer.
[Link](x): Returns the square root of $x$.
[Link](x, y): Returns $x$ raised to the power $y$.
[Link]: The constant $\pi$ (approx. 3.14159).
Example Code:
Python
import math
print([Link](16)) # Output: 4.0
print([Link](4.2)) # Output: 5
print([Link]) # Output: 3.141592653589793
2. The Random Module
This module is used to generate pseudo-random numbers. It’s perfect for games, shuffling lists, or
picking random winners.
Key Functions:
[Link](): Returns a random float between 0.0 and 1.0.
[Link](a, b): Returns a random integer between $a$ and $b$ (inclusive).
[Link](sequence): Returns a random element from a list or string.
[Link](list): Reorders the elements of a list in place.
Example Code:
Python
import random
# Roll a die
print([Link](1, 6))
# Pick a random color
colors = ['Red', 'Blue', 'Green']
print([Link](colors))
3. The Statistics Module
This module is perfect for quick data analysis without needing heavy tools like Excel or specialized
libraries like Pandas.
[Link](data): Calculates the average (sum divided by count).
[Link](data): Finds the middle value. This is better than the mean if your data has
"outliers" (values that are way higher or lower than the rest).
[Link](data): Returns the most frequent value in the dataset.