Unit 5 • Lesson 2

Importing Built-in Modules

Overview

Python provides hundreds of modules that handle tasks like random number generation, date manipulation, and math operations. You'll practice importing modules using import, from...import, and aliases to streamline your code and improve efficiency, accessing powerful tools without rewriting code.

Beginner 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • Basic import: How to import entire modules using import.
  • Selective import: How to import specific functions using from...import.
  • Module aliases: How to rename modules for convenience using as.
  • Common modules: How to use popular built-in modules like math, random, and datetime.

Why This Matters

Python comes with many built-in modules that provide ready-to-use functions for common tasks. Instead of writing code to generate random numbers, calculate square roots, or work with dates from scratch, you can import these modules and use their functions. Learning different import methods helps you write cleaner, more efficient code!

Step 1: Basic Import Statement

The simplest way to import a module is using the import statement. This loads the entire module and makes it available with its name:

Importing a Module
import math

# Use functions from the module
result = math.sqrt(16)
print(result)  # Output: 4.0

# Access constants
print(math.pi)  # Output: 3.141592653589793
1

Import Statement

import math loads the entire math module. Python finds the module and makes it available.

2

Access Functions

Use math.sqrt() to call functions. The math. prefix tells Python you're using something from the math module.

3

Access Variables

Access constants like math.pi using the same dot notation. This keeps everything organized!

Key Concept: When you use import math, you must prefix everything with math. This prevents naming conflicts - if you have your own function called sqrt(), it won't conflict with math.sqrt()!

Mini Practice #1: Basic Import

Try It Yourself

Try importing and using the math module:

Press Run to see output

What happened? The import math statement loaded the math module. You can then use math.sqrt() to calculate square roots and math.pi to access the value of pi. The dot notation (math.) tells Python which module the function belongs to!

Step 2: From...Import Statement

You can import specific functions or variables from a module using from...import. This lets you use them without the module prefix:

Selective Import
from math import sqrt, pi

# Use functions directly without module prefix
result = sqrt(16)
print(result)  # Output: 4.0

# Use constants directly
print(pi)  # Output: 3.141592653589793
1

Selective Import

from math import sqrt, pi imports only sqrt and pi from the math module.

2

Direct Use

You can use sqrt() and pi directly without the math. prefix. This makes code shorter!

3

Trade-off

While convenient, this can cause naming conflicts if you have variables with the same names. Use carefully!

When to Use Each Method

  • Use import math: When you need many functions from a module or want to avoid naming conflicts.
  • Use from math import sqrt: When you only need one or two specific functions and want shorter code.

Mini Practice #2: Selective Import

Try It Yourself

Try importing specific functions from the random module:

Press Run to see output

What happened? By using from random import randint, choice, you imported only those two functions. You can use them directly without writing random.randint() or random.choice(). This makes your code shorter and easier to read!

Step 3: Import with Alias

You can give a module a shorter name using as. This is especially useful for modules with long names:

Using Aliases
import math as m

# Use the shorter alias
result = m.sqrt(16)
print(result)  # Output: 4.0

# Still works the same way
print(m.pi)  # Output: 3.141592653589793

Common Aliases

Some modules are commonly imported with aliases:

  • import numpy as np - Very common in data science
  • import pandas as pd - Standard for data analysis
  • import matplotlib.pyplot as plt - Common for plotting

Step 4: Common Built-in Modules

Here are some useful built-in modules you'll use frequently:

math

Mathematical functions and constants.

import math
math.sqrt(16)
math.pi

random

Generate random numbers and choices.

import random
random.randint(1, 10)
random.choice([1, 2, 3])

datetime

Work with dates and times.

from datetime import datetime
datetime.now()

os

Interact with the operating system.

import os
os.getcwd()

End-of-Lesson Exercises

Exercise 1: Import and Use Math Module

Import the math module and calculate the area of a circle with radius 7. Use math.pi for pi. Print the result rounded to 2 decimal places.

Use import math, then calculate area = math.pi * radius ** 2.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Use From...Import

Use from random import randint to generate a random number between 50 and 100. Print the result.

Use from random import randint, then randint(50, 100).

Write your code above and click "Check Answer" to verify it's correct.