Challenge 1 +25 XP
Import the `math` module and print the value of `math.pi` rounded to 4 decimal places using the `round()` function.
- Test 1 — expects "3.1416"
Show solution (0 XP)
import math
print(round(math.pi, 4)) A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
When you have written a handy function, you do not want to copy its definition into every program. Instead, you can save it as a .py file and import it.
Step through the visualizer below to see how the math module is brought into our local namespace.
import math
print(math.sqrt(16.0))There is a variant of the import statement that imports names from a module directly into the importing module’s namespace.
Instead of typing the module name every time, you can pull specific items straight into your code. Notice how this time, only pi appears in the namespace, not the entire math module!
from math import pi
print(round(pi, 2))If the module name is followed by as, then the name following as is bound directly to the imported module.
This is extremely common in data science libraries (like import pandas as pd).
import math as m
print(m.sqrt(25.0))Python comes with a library of standard modules, described in a separate document, the Python Library Reference
Python includes batteries out of the box! You can access random number generators, math operations, system paths, and more.
import random
# mock random for test stability
random.seed(42)
print(random.randint(1, 10))2
The variable sys.path is a list of strings that determines the interpreter’s search path for modules.
import sys
print(type(sys.path))<class 'list'>
the code in the module will be executed, just as if you imported it, but with the name set to “main”.
If you import a module, its __name__ is the module’s name. If you run the file directly, it runs as __main__.
if __name__ == '__main__':
print('Running as main script')Running as main script
math.py) will cause import errors because the local directory is first in sys.path.Importing a module multiple times runs its code multiple times.
For efficiency reasons, each module is only imported once per interpreter session. — For efficiency reasons, each module is only imported once per interpreter session.
Using `from module import *` is the standard and best way to import.
Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. — Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code.
The __name__ variable is always the file name.
When a module is executed directly as a script (e.g. `python my_script.py`), its `__name__` is set to the string `'__main__'` instead of the file name. — When a module is executed directly as a script (e.g. `python my_script.py`), its `__name__` is set to the string `'__main__'` instead of the file name.
Variables from an imported module overwrite your own variables automatically.
Each module has its own private namespace. Unless you use `from module import *`, the module's contents are kept inside its own namespace (e.g. `module.variable`). — Each module has its own private namespace. Unless you use `from module import *`, the module's contents are kept inside its own namespace (e.g. `module.variable`).
Import the `math` module and print the value of `math.pi` rounded to 4 decimal places using the `round()` function.
import math
print(round(math.pi, 4)) Write an `if` statement that checks if the script is being run directly. If it is, print the string 'Running directly!'.
if __name__ == '__main__':
print('Running directly!')