Lesson 26: Standard modules: math, sys and time
Three standard modules cover most small needs: math for precise arithmetic, sys for information about the interpreter, and time for clocks and delays. All three ship with Python.
In short
- `math` gives square roots, powers, rounding down and up, and the constant pi.
- `sys` describes the interpreter: version, platform, arguments from the command line, and `exit()`.
- `time` measures and pauses: `time()`, `sleep()`, `strftime()` for formatted dates.
- Standard modules are installed with Python, so importing them needs no pip and no internet.
3
modules covered: math, sys, time
Python docs
0
packages to install for any of them
Python installer
Why standard modules exist
A standard module is a library that ships with Python. You import it and it is there — no installation, no account, no network. These three are the ones you will use most in a first course.
The program
# stdlib.py — maths, interpreter facts, and time
import math
import sys
import time
print(math.sqrt(144)) # 12.0
print(math.pi) # 3.141592653589793
print(math.floor(3.9), math.ceil(3.1)) # 3 4
print(math.pow(2, 5)) # 32.0
print(sys.version.split()[0]) # your Python version
print(sys.platform) # win32, linux or darwin
print(len(sys.argv)) # 1 when no arguments are passed
start = time.time()
time.sleep(0.2)
print("slept for", round(time.time() - start, 1), "seconds")
print(time.strftime("%d-%m-%Y", time.gmtime(0)))
How it works, step by step
math.sqrt()returns a float even when the answer is whole, so 144 gives 12.0.math.floor()goes down to the nearest whole number,math.ceil()goes up. They are not the same asround().sys.versionis a long string;.split()[0]keeps only the version number.sys.platformreadswin32on Windows andlinuxon Linux. Useful when a script must behave slightly differently on the lab machines.sys.argvholds the script name first, then any arguments typed after it. With none, its length is 1.time.time()gives the current time in seconds as a float. Subtracting two readings measures elapsed time.time.sleep(0.2)pauses for two-tenths of a second.strftimeformats a time;gmtime(0)is the start of 1970, used here so the output is predictable.
What it prints
12.0 3.141592653589793 3 4 32.0 3.10.12 win32 1 slept for 0.2 seconds 01-01-1970
What each module offers
| Module | Useful members | Typical use |
|---|---|---|
math | sqrt, pow, floor, ceil, pi, fabs | Geometry, statistics, precise rounding |
sys | version, platform, argv, path, exit() | Command-line arguments, environment checks |
time | time(), sleep(), strftime(), localtime() | Timing code, pausing, formatting dates |
Common mistakes
- Writing
import mathand then callingsqrt(9). You needmath.sqrt(9). - Comparing times as strings. Use
time.time()numbers for measuring. - Calling
print(sys.argv[1])without checking the length first, which raisesIndexErrorwhen no argument was given.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Use `math.sqrt` to print the square root of 625 and the hypotenuse of a triangle with sides 3 and 4.
- Print `math.floor` and `math.ceil` of 7.2 and of -7.2, and explain the difference.
- Print the Python version and platform of your machine using `sys`.
- Time a loop that runs 100000 times with `time.time()` and print how long it took.
Sources
Frequently asked questions
What is the math module used for?
Precise mathematical functions: square roots, powers, floor and ceiling, trigonometry, logarithms and the constant pi. It is part of the standard library, so it needs no installation.
What can I get from sys?
Information about the interpreter and the environment: version, platform, the folders it searches, command-line arguments in sys.argv, and sys.exit() to stop the program.
How do I measure how long code takes in Python?
Read time.time() before and after the block and subtract the two floats. For serious profiling use time.perf_counter(), which has higher resolution.
Do standard modules need pip?
No. math, sys, time, os and the rest of the standard library are installed with Python itself. pip is only for third-party packages.