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 turtlecan 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!
๐ณ 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 =
importCommand - Turtle ingredients =
turtlelibrary
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
# Add blocks and the Python code will show up here...
Example Code
- Import the turtle library - You have to write it first!
- Create a turtle object and
tStores it in a variable - 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
Show Answer
Answer: A) It imports the turtle tools
importmeans "import". import turtle tells Python,
"I'm going to use 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= import (the keyword for loading a library)turtle= the name of the turtle graphics library
๐ง Debug Clinic
Problem: a NameError occurs!
โ Wrong Code
Error message:NameError: name 'turtle' is not defined
Cause: import turtleYou didn't write it
โ Correct Code
Fix: at the very top of the programimport 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
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? - [ ]
NameErrorDo you know how to fix it when an error occurs? - [ ] Did you confirm that the "start turtle" block in Blockly automatically adds the import?