A module is just a code library. Discover what's available with help('modules'), then take a hands-on tour of math, random, time, and os.
This topic covers Python modules โ what they are, how to discover which ones are available, and how to use some of the most common built-in modules (math, random, time, os) through hands-on examples in Jupyter Notebook.
help('modules').ModuleNotFoundError."Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application."
Examples of Python modules covered in this lesson:
mathrandomostimeYou can see every module installed/available in your Python environment with:
help('modules')
What happens:
Please wait a moment while I gather a list of all available modules...ShimWarning: The `IPython.kernel` package has been deprecated since IPython 4.0.
You should import from ipykernel or jupyter_client instead.
โ ๏ธ This is just a warning from the IPython/Jupyter internals โ it does not stop the module list from being generated, and can generally be ignored.
numpy, pandas, os, time, math, random, sklearn, matplotlib, sqlite3, threading, json-related modules, win32* modules on Windows, etc.). This list includes:
os, time, math, random, json, csv, threading, sqlite3)numpy, pandas, matplotlib, sklearn, sqlalchemy)_, e.g., _ast, _bisect, _codecs)๐ก Tip: help('modules') is a great way to explore what's available in your environment, but the output can be long โ scroll through it or search for a keyword.
Before using any function from a module, you must import it:
import math
import random
import os
import time
Important Point: If you misspell a module name or import something that isn't a real module, Python raises a ModuleNotFoundError.
Example of an error:
import mathrhe
ModuleNotFoundError: No module named 'mathrhe'
C vs Python โ importing a library:
| C | Python | |
|---|---|---|
| Bring in a library | #include <stdio.h> | import math |
| Syntax | #include <filename.h> | import module_name |
| When it happens | Preprocessor step (before compilation) | Runtime (when the line executes) |
Same purpose in both โ reuse existing code โ just a different keyword and mechanism.
Things to Remember:
ModuleNotFoundError means Python cannot find any module with that exact name โ check spelling first.Used for mathematical operations.
| Function | Example | Result | Description |
|---|---|---|---|
math.pi | math.pi | 3.141592653589793 | Value of ฯ |
math.e | math.e | 2.718281828459045 | Euler's number |
math.factorial(n) | math.factorial(5) | 120 | Factorial of n (5! = 120) |
math.ceil(x) | math.ceil(6.3) | 7 | Rounds up to the nearest integer |
math.floor(x) | math.floor(6.9) | 6 | Rounds down to the nearest integer |
math.sqrt(x) | math.sqrt(100) | 10.0 | Square root (always returns a float) |
Key takeaway: ceil() always rounds up, floor() always rounds down โ regardless of the decimal value (e.g., 6.1 would still floor to 6 and ceil to 7).
Used for generating random numbers and shuffling data.
import random
random.randint(1, 100) # Returns a random integer between 1 and 100 (inclusive)
Example output: 54
a = [1, 2, 3, 4, 5]
random.shuffle(a) # Shuffles the list IN PLACE (no return value needed)
a
Example output: [1, 4, 5, 2, 3] (order will vary each run)
Important Points:
random.randint(a, b) โ returns a random integer N such that a <= N <= b (both bounds inclusive).random.shuffle(list) โ shuffles the given list in place; it modifies the original list rather than returning a new one.Used for working with time-related values.
import time
time.time() # Returns current time as a Unix timestamp (seconds since epoch)
Example output: 1625459281.405317
time.ctime() # Converts current time into a human-readable string
Example output: 'Mon Jul 5 09:58:08 2021'
time.sleep(1) # Pauses program execution for the given number of seconds
Example demonstrating time.sleep():
print("Hello")
time.sleep(1)
print("World")
Output:
Hello
World
("Hello" prints, then execution pauses for 1 second, then "World" prints.)
Important Points:
time.time() โ raw numeric timestamp (useful for measuring elapsed time / performance).time.ctime() โ human-friendly formatted date/time string.time.sleep(seconds) โ delays/pauses code execution โ useful for simulating wait times or throttling loops.Used to interact with the operating system (files, directories, paths).
import os
os.getcwd() # Returns the current working directory
Example output:
'C:\\Users\\91842\\100-days-of-python\\day9-builtins'
os.listdir() # Lists all files and folders in the current directory
Example output:
['.ipynb_checkpoints', 'built-in-functions.ipynb', 'built-in-modules.ipynb']
Important Points:
os.getcwd() โ "get current working directory" โ shows where your script/notebook is running from.os.listdir() โ lists contents (files + folders) of a directory (defaults to current directory if no path is given).os module also has many other functions (seen while autocompleting in the notebook): os.getenv(), os.getlogin(), os.getpid(), os.getppid(), os.get_exec_path(), os.get_terminal_size(), etc. โ these deal with environment variables, process IDs, and terminal info.# Discover all available modules
help('modules')
# Import modules
import math
import random
import os
import time
# math module
math.pi
math.e
math.factorial(5)
math.ceil(6.3)
math.floor(6.9)
math.sqrt(100)
# random module
random.randint(1, 100)
random.shuffle(my_list)
# time module
time.time()
time.ctime()
time.sleep(seconds)
# os module
os.getcwd()
os.listdir()
import a module before using its functions โ using the exact, correct name.help('modules') lists every module available in your current environment (built-in + installed packages).math โ mathematical constants & operations (pi, e, factorial, ceil, floor, sqrt).random โ randomness (randint for random integers, shuffle for shuffling a list in place).time โ time-related operations (time() for timestamp, ctime() for readable time, sleep() to pause execution).os โ operating system interaction (getcwd() for current directory, listdir() to list directory contents).ModuleNotFoundError: No module named '...'.Python modules are reusable code libraries. Use help('modules') to see all available ones, and import <module_name> to bring one into your program. Four commonly used built-in modules:
math โ constants (pi, e) and operations (factorial, ceil, floor, sqrt).random โ randint(a, b) for random integers, shuffle(list) to randomize a list in place.time โ time() for a raw timestamp, ctime() for a readable date/time, sleep(n) to pause execution for n seconds.os โ getcwd() to get the current directory, listdir() to list files/folders in it.If you import something that doesn't exist, Python throws ModuleNotFoundError: No module named '<name>' โ always double-check spelling.