Vinay Garg Assistant Professor

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.

A dark code window showing math.sqrt and sys.platform output

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

  1. math.sqrt() returns a float even when the answer is whole, so 144 gives 12.0.
  2. math.floor() goes down to the nearest whole number, math.ceil() goes up. They are not the same as round().
  3. sys.version is a long string; .split()[0] keeps only the version number.
  4. sys.platform reads win32 on Windows and linux on Linux. Useful when a script must behave slightly differently on the lab machines.
  5. sys.argv holds the script name first, then any arguments typed after it. With none, its length is 1.
  6. time.time() gives the current time in seconds as a float. Subtracting two readings measures elapsed time.
  7. time.sleep(0.2) pauses for two-tenths of a second. strftime formats 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

ModuleUseful membersTypical use
mathsqrt, pow, floor, ceil, pi, fabsGeometry, statistics, precise rounding
sysversion, platform, argv, path, exit()Command-line arguments, environment checks
timetime(), sleep(), strftime(), localtime()Timing code, pausing, formatting dates

Common mistakes

  • Writing import math and then calling sqrt(9). You need math.sqrt(9).
  • Comparing times as strings. Use time.time() numbers for measuring.
  • Calling print(sys.argv[1]) without checking the length first, which raises IndexError when no argument was given.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Use `math.sqrt` to print the square root of 625 and the hypotenuse of a triangle with sides 3 and 4.
  2. Print `math.floor` and `math.ceil` of 7.2 and of -7.2, and explain the difference.
  3. Print the Python version and platform of your machine using `sys`.
  4. 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.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.