Learning LibraryCore Coding LibraryKids

import: borrow code someone already wrote

You don't have to build everything yourself. import lets you borrow ready-made tools — like a star stamp and a flower stamp — and use them right away.

The big idea

import brings code from another place into your program, so you can use its tools as if you wrote them.

See it in code

1Start simple

Python ships with ready-made tools. The math box already knows square roots — we don't rewrite that, we borrow it. from math import sqrt grabs just that one tool, and we use it right away:

python
from math import sqrt

print("The square root of 49 is", sqrt(49))
Run it — borrowed code does the work:
The square root of 49 is 7.0

We never wrote the square-root math — math did. The import line is what let us reach in and grab sqrt.

2A step further

You can grab more than one tool on the same line. Here we borrow sqrt and floor (which rounds a number down to a whole number):

python
from math import sqrt, floor

print(sqrt(49))
print(floor(4.8))
Run it — two borrowed tools, one import line:
7.0
4

Two names after import, separated by a comma, so we borrowed two tools at once. Every tool you use has to be on that line.

3In our world

Now our own toolbox. The art package already knows how to draw a perfect star and a perfect flower. We don't rewrite them — from art import Canvas, star, flower grabs three tools at once, just like before. Then we use them:

python
from art import Canvas, star, flower

screen = Canvas.create()
Canvas.fill(screen)

star(screen, 160, 250, size=90)
flower(screen, 340, 250, size=90)
Run it — a star and a flower, drawn by borrowed tools:
A yellow star on the left and a pink flower on the right, on a dark navy canvas.

We never wrote the math for a star's five points — art did. Importing let us stand on top of someone else's work.

The same idea, everywhere

Every real program imports. Games import their game pieces, artists import color palettes, and Python itself ships with hundreds of ready-made tools. Learning to borrow code is how small programs do big things.

Try it yourself

Add another star near the top with star(screen, 250, 120). Then try to draw a heart(...) — Python will complain, because you only imported star and flower. Every borrowed tool needs to be on the import line.

The common mistake

Using a tool you forgot to import. If you call star(...) but never wrote from art import star, Python says it does not know what star is. Each borrowed tool needs its import line first.

What it unlocks

Once you can import, you can pull in the random module to scatter stars, and reach for for loops to stamp a whole row of them.

Older, or want more depth? Read the Teens version →