Modules

  • Modules : ”packages” or libraries of code in a file or set of files

from module_name import object_name
# exampe - datetime is both a module name and an object name

from datetime import datetime
  • random is a useful module

import random

#create a random list of numbers, and then choose a random number from that list
rand_list = [random.randint(1, 10) for i in range(11)]
rand_choose = random.choice(rand_list)
print(rand_choose)
  • alias: changing the name of a namespace when importing

  • use from <module_name> import <object_name> as <alias>

  • another useful module is decimal

# Import Decimal below:
from decimal import Decimal

# Fix the floating point math below:
two_decimal_points = Decimal('0.2') + Decimal('0.69')
print(two_decimal_points)