Skip to content

B. import: Loading a Library


๐ŸŽฏ Learning Goals

By the end of this lesson, you'll be able to:

  • โ—‹ importunderstands what it is
  • โ—‹ Understands why you need to import a library
  • โ—‹ import turtle can use the syntax

Core Concepts

What is import?

import means "bring in a tool." Turtle features aren't built into Python by default. You have to import them before you can use them!

import turtle  # Import the turtle tools!

๐Ÿณ Analogy: Cooking and Coding

To cook, you first have to take the ingredients out of the fridge, right? Coding is the same way: first you bring in the tools you need!

  • Refrigerator = Python library repository
  • Take out the ingredients = import Command
  • Turtle ingredients = turtle library

The flow of import

flowchart LR
    A["import turtle"] --> B["turtle.Turtle()"]
    B --> C["Turtle commands"]

    style A fill:#e3f2fd,stroke:#1976d2
    style C fill:#e8f5e9,stroke:#388e3c

Code execution order

Order Code Description
1๏ธโƒฃ import turtle Importing a tool
2๏ธโƒฃ turtle.Turtle() Create the turtle
3๏ธโƒฃ Commands forward, right, etc.

Try Visual Coding

๐ŸŽฏ Mission
โ—‹ Check the Python code that the "start turtle" block generates. Do you see import turtle?
# Add blocks and the Python code will show up here...
Add blocks and the flowchart will show up here...


Example Code

import turtle          # (1)
t = turtle.Turtle()    # (2)
t.shape("turtle")      # (3)
  1. Import the turtle library - You have to write it first!
  2. Create a turtle object and t Stores it in a variable
  3. Set the turtle shape

What Happens

A turtle appears in the center of the screen! ๐Ÿข

๐Ÿ“ import always goes at the very top!

import The statement is always the code's The very first line is where you have to write it. After all, you can't use a tool without taking it out first!


๐ŸŽฏ Quiz

Quiz 1: what import does

Quiz: what import does
What does `import turtle` do?
Show Answer

Answer: A) It imports the turtle tools

importmeans "import". import turtle tells Python, "I'm going to use the turtle tools!"

import turtle  # Import the turtle tools!

Without this one line turtle.Turtle() You can't use the same command.

import syntax practice

To import the turtle tools:

import turtle
  • import = import (the keyword for loading a library)
  • turtle = the name of the turtle graphics library

๐Ÿ”ง Debug Clinic

Problem: a NameError occurs!

โŒ Wrong Code

t = turtle.Turtle()  # What is turtle?
Error message: NameError: name 'turtle' is not defined

Cause: import turtleYou didn't write it

โœ… Correct Code

import turtle        # Import it first!
t = turtle.Turtle()
Fix: at the very top of the program import turtle Add!


Try Coding It Yourself

Take what you learned with the blocks and write it yourself in Python code!

Drawing the ใ„ฑ shape with the turtle

import turtleAfter importing the turtle tool with it, draw the ใ„ฑ shape. If it succeeds: The turtle goes forward, then turns right to draw the ใ„ฑ shape!

Example Answer
import turtle

t = turtle.Turtle()
t.shape('turtle')
t.forward(100)
t.right(90)
t.forward(100)

Result: The turtle draws the ใ„ฑ shape!


๐Ÿงฉ Key Takeaways

What you learned in this lesson:

Concept Description
import the command that imports a library (tool)
turtle Turtle graphics library
Position Always at the code's The very first lineWrite in
Error If you forget the import NameError Occurs

Review Checklist

  • [ ] importCan you explain what it is?
  • [ ] import turtleDo you know why you have to write it at the top of the code?
  • [ ] NameError Do you know how to fix it when an error occurs?
  • [ ] Did you confirm that the "start turtle" block in Blockly automatically adds the import?