PYTHON FOR BEGINNERS
Understanding import
A from-scratch guide to using other people's code in your own programs
The Problem import Solves
When you write Python code, you don't want to write everything from scratch every time. Someone has already
written code to do common things — math operations, working with dates, generating random numbers, and much
more. That code lives in files called modules.
import is how you tell Python: “I want to use code that someone already wrote, which lives in another file.”
A Real-World Analogy
🍳 Think of your kitchen
You don't own a farm, a flour mill, and an oven factory. Instead, you go to the store and bring in (import)
flour, sugar, and eggs — things made elsewhere — and use them in your recipe. import in Python does the
same thing: it brings in a ready-made package of code so you can use it in your program.
Basic Syntax
Here's the simplest possible example:
import math
print([Link](16)) # 4.0
What happened here:
● import math — Python loads a built-in module called math, which has lots of math-related functions.
● [Link](16) — you use dot notation (module_name.function_name) to reach a function inside that module.
⚠ Remember
You must write math. before sqrt — Python needs to know which module the function came from.
Understanding import in Python | Page 1
Importing Only What You Need
If you only want one specific thing from a module (not the whole thing):
from math import sqrt
print(sqrt(16)) # 4.0
Notice: now you don't need math. anymore, because you imported sqrt directly.
Giving It a Nickname (Alias)
Module names can be long, so people give them shorter nicknames using as:
import math as m
print([Link](16)) # 4.0
This is extremely common with popular libraries, for example:
import pandas as pd
import numpy as np
Where Do Modules Come From?
There are three sources you'll encounter:
Type Example Description
Built-in math, random, datetime Comes free with Python — no installation needed
Written by others; install first with pip install
Third-party pandas, requests
<name>
Your own files import my_script Code you wrote yourself, saved in another .py file
Quick Example with random
import random
Understanding import in Python | Page 2
number = [Link](1, 10) # random whole number between 1 and 10
print(number)
✅ Recap
import brings in pre-written code. Use “import module” for the whole module, “from module import name”
for one piece, and “as” to give it a short nickname. Modules come from Python itself, from third parties (via
pip), or from your own files.
Understanding import in Python | Page 3