Skip to main content

Lesson 1: Intro to Python Turtle

info
Prerequisites: Before You Dive In!

Python Libraries

Before you start coding in Python, let's talk about what a "library" is.

Think of a library like a toolbox where you can borrow tools you need for a project. In programming, a library is similar, but instead of physical tools, it gives us special functions that you can use to make coding easier and faster.

Getting to Know the Python turtle Library

The Python turtle library is a built-in tool that lets you create pictures and shapes by providing a virtual canvas. The tool you use to draw on this canvas is called the "turtle," and that's how the library gets its name.

You can use it to draw all sorts of shapes and images. Here's an example of what you can do with the turtle:

nerdEmoji

Pretty neat, isn't it? This is just one example of the many things you can create using the turtle library.

Objective πŸ§πŸ—Ώβ€‹

Dive into the world of Python programming by learning about libraries, variables, and basic turtle commands to start moving and positioning your turtle creatively!

Getting Started with Turtle πŸš©πŸ’β€‹

Step 1. Import Turtle​

info
Coach Note!

Here, your coach will speak a bit about the Python import system.

Standard

You'll learn how to use Python's import system, including:

  • Basic usage of the import statement.
  • How to import specific functions, classes, and variables from modules.
  • Understanding the role of Python's built-in modules in everyday programming tasks.
Advanced

If you've gone over the content in the Standard section above and still need a bit more of a challenge, have no fear for your coach is here!

You'll learn how a library is used in Python from start to finish, covering:

  • An overview of PyPi (Python Package Index), the repository for Python packages.
  • The process of downloading and installing packages using pip.
  • Understanding what "creating an instance" means in terms of import

Open up our "My Turtle" file we made in our Setup section.

To use the Python Turtle library, we first need to import it into our environment. Type this command in:

# main.py
import turtle

Now that we have Turtle in our Python environment, we can begin programming with it.

Step 2. Making Our Own Turtle​

Let's talk about our Turtle and the screen!

Our Turtle and Screen

The box highlighted in red is called the screen. It’s where you can view the output of your code. The circle highlighted in green is called our turtle.

To do make some cool artwork, we'll need a variable to access our turtle.

info
Coach Note!

Here your coach will speak a bit on variables.

Standard

    Understanding Variables:

    • What a variable is and how it's used to store data.
    • How to create and initialize variables in Python, with examples using Turtle graphics.

    Variable Naming:

    • Importance of choosing clear and descriptive names for better readability and maintainability of code.
    • Examples of good vs. bad variable names in the context of Turtle graphics.
Advanced

    Advanced Variable Management:

    • Discussing variable scope (local vs global variables) and its implications in larger Python scripts.
    • Using variables effectively to manage the state and properties of Turtle objects and the Turtle screen.

    Dynamic Typing in Python:

    • Explanation of Python's dynamic typing system and how it affects variable assignment and function arguments.
    • Practical example of creating a Turtle instance with custom parameters
    • Case studies: Modifying the properties of Turtle objects (like color, shape, and speed) dynamically based on user input or other conditions.

First, let’s bring in our turtle. This turtle is going to be your artist, drawing cool stuff on the screen!

t = turtle.Turtle()

The turtle has changeable characteristics, like size, color, and speed. It always points in a specific direction and will move in that direction unless you tell it otherwise:

  • When it’s up, it means that no line will be drawn when it moves.
  • When it’s down, it means that a line will be drawn when it moves.

Now, we need to set up the screen where your turtle will draw. Think of it like setting up a canvas for painting. This screen also works off a coordinate system.

On Coordinates

Imagine the screen as a giant graph paper, divided into four quadrants. The center, where your turtle starts, is called Home and is at coordinates (0,0).

nerdEmoji

Understanding the coordinates helps move our turtle to any point on the screen using its x and y coordinates.

We'll have to make a variable for our screen, just like we did for our turtle. Here's one way to do it:

s = turtle.getscreen()

We use a short and sweet name s for the screen. Easy to type and easy to remember, right?

We now have a screen and our turtle. The screen acts as a canvas, while the turtle acts like a pen. We can program the turtle to move around the screen.

Programming with Turtle βŒ¨οΈπŸ’šβ€‹

info
Coach Note!

In these next two sections, your coach will be going over Turtle Movement and Positioning. Here is what you can expect to learn!

Standard

    Basic Turtle Movements:

    • Introduction to moving the turtle using basic commands like t.forward(100) and t.right(90).
    • Understanding how these commands affect the turtle’s position and orientation on the screen.
    • Practical exercises: Commands to move the turtle in a square or triangle pattern.
Advanced

    Complex Movements and Patterns:

    • Using loops and conditions to create complex geometric patterns and designs.
    • Examples include creating spirals, fractals, and other intricate designs with the Turtle module.

    Precise Positioning Techniques:

    • Detailed explanation of the coordinate system used in Turtle graphics.
    • How to use t.goto(x, y) for absolute positioning, and the implications of using absolute vs. relative movements.
    • Case study: Using mathematical functions to calculate exact positions for more dynamic and responsive Turtle graphics.

Turtle Movement​

There are four directions that a turtle can move in:

  • Forward
  • Backward
  • Left
  • Right

nerdEmoji

The turtle moves .forward() or .backward() in the direction that it’s facing. You can change this direction by turning it .left() or .right() by a certain degree.

Let's start with some simple movements. Use the following commands to move your turtle around the screen:

t.right(90)     # Turn right by 90 degrees
t.forward(100) # Move forward by 100 units
t.left(90) # Turn right by 90 degrees
t.backward(100) # Move backward by 100 units

When you run these commands,

  • the turtle will turn right by ninety degrees,
  • go forward by a hundred units,
  • turn left by ninety degrees,
  • and move backward by a hundred units.
info
Shorthand Notation

To save some time, we can use shortened versions of the function names as well.

t.rt() # instead of t.right()
t.fd() # instead of t.forward()
t.lt() # instead of t.left()
t.bk() # instead of t.backward()
CHALLENGE: Complete the Square πŸ‘‰ 🟩 πŸ‘ˆ

The commands we just ran form half of a square. Your goal is to now complete the square.

You’ll want your turtle to point in the upwards direction, move forward, and turn right, move forward.

If you need help, click below for some help!

HINT 🀫
import turtle
t = turtle.Turtle()

t.forward(100) # Move forward by 100 units
t.right(90) # Turn right by 90 degrees
t.forward(100) # Move forward by 100 units
t.right(90) # Turn right by 90 degrees
t.forward(100) # Move forward by 100 units
t.right(90) # Turn right by 90 degrees
t.forward(100) # Move forward by 100 units
t.right(90) # Turn right by 90 degrees

Turtle Positioning​

With Python Turtle, you can not only move your turtle around but also precisely position it anywhere on the screen. Let's dive into the world of coordinates and discover how to navigate the Turtle's universe!

Moving Your Turtle

To move your turtle to a specific position, use the .goto() function and specify the x and y coordinates. For example, to move to the point (100,100), you would write:

t.goto(100, 100)

Drawing Lines

You can also draw a line from your current position to another point on the screen using the .goto() function. This allows you to create intricate shapes and patterns with ease!

After exploring and creating your masterpiece, you can always bring your turtle back to its home position using the .home() function. This resets the turtle's position to (0,0), ready for your next adventure!

CHALLENGE: Exploring ✨ Quadrants ✨

Have your turtle draw a line towards its original position, to quadrant one, to quadrant two, to quadrant three, and finally quadrant four.

Check out the diagram below for some guidance:

nerdEmoji

Click below if you need some help!

HINT 🀫

You'll want to base your coordinates given to the ``` python goto(x,y)

 function above according to the quadrant visiting. 
```python
t.goto(50, 50) # X and Y are positive. This is Quadrant One.
t.goto(-50, 50) # X is negative, Y is positive. This is Quadrant Two.
t.goto(-50, -50) # Both X and Y are negative. This is Quadrant Three.
t.goto(50, -50) # X is positive, Y is negative. This is Quadrant Four.
t.home() # Sending our turtle back home

  • You've just embarked on an amazing coding journey, mastering the basics of turtle movement and positioning, setting the stage for more creative adventures!