Built in Modules
2 min read ·
Python provides many built in modules that are ready to use. These modules help perform common tasks efficiently without writing code from scratch.
math Module
The math module provides functions for mathematical calculations.
Common Functions
Important Constants
Note
math module works with float values and provides precise mathematical operations.
random Module
The random module is used to generate random values.
Common Functions
Shuffle Example
Pro Tip
random module is widely used in games, simulations, and testing.
datetime Module
The datetime module is used to work with date and time.
Current Date and Time
Access Individual Components
Formatting Date
Note
datetime module is very useful for logging, scheduling, and time-based operations.
sys Module
The sys module provides access to system-level operations.
Common Usage
Command Line Arguments
Exit Program
Caution
Be careful while using sys.exit() as it stops the program immediately.
Understanding When to Use Which
- math → calculations
- random → generating random values
- datetime → handling date and time
- sys → interacting with Python runtime and system
Exercise
- Use math to calculate square root of a number
- Use random to generate a random number
- Print current date using datetime
- Print Python version using sys