Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
85: Drawing Shapes with Turtle
Turtle graphics is one of those things that feels like a toy, but it's actually a fantastic way to visualize how loops and geometry interact in your code. Instead of staring at text in a console, you get to see your logic literally draw itself on the screen. Today, we're going to build a colorful, expanding geometric spiral.
Getting our canvas ready
First, we need to bring in the module and set up our "turtle"—which is really just a cursor that moves based on X and Y coordinates. I like to name my turtle t just to keep the code concise.
import turtle
t = turtle.Turtle()
t.speed(0) # This makes the turtle move as fast as possible
screen = turtle.Screen()
screen.bgcolor("black")
t.color("cyan")
The struggle with triangle angles
I want this spiral to be based on a triangle. My first instinct is usually to think, "A triangle has 60-degree angles, so I'll just turn 60 degrees." Let's see what happens when I try that in a loop.
# My first attempt (The Mistake)
for i in range(3):
t.forward(100)
t.right(60)
If you run that, you'll notice it doesn't actually make a triangle; it just makes a weird open zig-zag. I forgot a fundamental rule of Turtle: the turtle turns based on the exterior angle, not the interior one. To get a 60-degree interior angle, I actually need to turn 120 degrees (180 - 60 = 120). It's a common trip-up, even for experienced devs when they come back to Turtle after a few years.
Here is the corrected logic for a proper triangle:
for i in range(3):
t.forward(100)
t.right(120)
Turning a shape into a spiral
A simple triangle is boring. To make it a spiral, we need to do two things: increase the distance the turtle moves every time it turns, and run the loop many more times than just three. I'll use a variable for the distance and increment it inside the loop.
distance = 5
for i in range(100):
t.forward(distance)
t.right(120)
distance += 3 # Every side gets slightly longer
Now we have a growing triangular spiral. It looks clean, but it's a bit monochromatic. Let's spice it up.
Adding a splash of color
To make this look professional, I'll define a list of colors and use the modulo operator (%) to cycle through them. This ensures that no matter how many times the loop runs, we never go out of bounds of our color list.
colors = ["red", "purple", "blue", "green", "orange", "yellow"]
distance = 5
for i in range(150):
t.color(colors[i % 6]) # Cycle through the 6 colors
t.forward(distance)
t.right(120)
distance += 3
turtle.done()
By changing the angle from 120 to something like 121 or 119, you'll notice the whole shape starts to rotate slightly, creating a much more complex "vortex" effect. That's the beauty of this—small tweaks to the math create massive changes in the art.
📋 Practical Task
Exercise: Building a Nested Square Mandala
Your task is to create a geometric mandala using a square as the base shape. Modify the spiral logic from the lesson to achieve the following:
- Use a square as the base (think about what the turn angle should be).
- Create a list of at least four different colors and cycle through them.
- Set the loop to run 200 times.
- Slightly offset the angle (e.g., use 91 degrees instead of 90) to create a rotating, nested effect.
- Ensure the background color is set to "black" and the turtle speed is set to 0.
There are no comments for now.