Python namespaces#

This notebook covers the basics of Python namespaces.

# Use the default math module
import math

# Need to use the prefix 'math.' to call functions in the math module
print(math.factorial(6))
720
# This imports sin and cos functions only
from math import sin, cos

# No need to use 'math.' prefix for individually imported functions
print(cos(math.pi / 3))
0.5000000000000001
# Can use custom namespace name different from the module name
# This imports random but uses 'rn' namespace
import random as rn

for i in range(5):
    # print a random number in [0, 1)
    print(rn.random())
0.6740011379453074
0.3621035635243627
0.7922788912057892
0.9976199482877249
0.8738133501376826
# Import all the 'math' functions into the namespace
# Importing everything without a namespace is generally discouraged
from math import *

print(sin(pi))
1.2246467991473532e-16