Lecture 5: Shapes!
Overview
A Python turtle graphics lesson builds from drawing equilateral triangles, squares, and octagons to using for loops and variables to generalize regular polygons. It then approximates circles with many short turtle movements, deriving each segment from the circumference formula 2πr, and applies turtle positioning, stamping, and pen controls to lab tasks such as a clock, initials, a Triforce, and Olympic rings.
Key takeaways
- A turtle's turn at a polygon vertex is an exterior angle, so a regular n-sided polygon uses turns of 360/n degrees, not its interior angle.
- For a regular polygon with radius r used to set its approximate circumference, each of n equal turtle movements can be set to 2πr/n, with a turn of 360/n degrees.
- Python's for _ in range(n) loop replaces repeated copy-pasted commands, and indentation controls which turtle actions execute n times.
- More polygon sides improve a circle approximation but increase drawing time because the turtle must perform more forward movements and turns.
- Use turtle up() and down() to reposition without drawing; combine them with partial circle arcs to control which Olympic ring segments appear to overlap.
Chapters
- The lesson continues Module 3 work on imports and connects to textbook material on turtle graphics and chapters 3 through 5.
- A common Python import error happens when a program is named after a library, such as turtle, random, or math; renaming the file resolves the conflict.
- The main task is to draw shapes with turtle, beginning with a triangle and progressing toward a circle.
- A turtle program begins with import turtle and a turtle object such as Bob = turtle.Turtle().
- Bob.forward(distance) moves the turtle by a number of pixels, while Bob.left(degrees) changes its heading.
- Bob.shape("turtle") changes the cursor's appearance, but is optional; the turtle starts at the screen center facing right.
- A triangle's three interior angles total 180 degrees, but turtle turns happen outside the polygon at each vertex.
- A turtle that completes a closed shape and returns to its original heading turns through 360 degrees in total.
- Confusing interior angles with turning angles leads to the wrong shape, such as drawing a hexagon instead of a triangle.
- An equilateral triangle uses three equal forward movements and three equal turns.
- Each turn is 120 degrees because 3 × 120 = 360, even though each interior angle is 60 degrees.
- A side-length variable lets the program use a chosen pixel distance, and int(input(...)) can convert a user's entry into an integer.
- A square requires four equal forward movements, each followed by a 90-degree left turn.
- Repeating the same commands by copy and paste becomes difficult to count and debug, especially as a shape gains more sides.
- The dice-face example illustrates chunking: people recognize small groups quickly, but larger repeated sequences are harder to count at a glance.
- A loop such as for _ in range(4): repeats its indented code block four times; indentation determines which commands repeat.
- The underscore marks a loop value that the program does not need to use.
- range(0) and range(-1) execute the loop body zero times, while range(1) executes it once.
- A for loop can iterate over collections as well as repeat a fixed number of times, including later examples such as for letter in text.
- The same loop can draw different regular polygons by setting a side count and turning 360 divided by that count after each movement.
- A square uses 4 sides and 90-degree turns; a triangle uses 3 sides and 120-degree turns.
- An octagon uses 8 sides and 45-degree turns, since 8 × 45 = 360; shorter sides help keep the larger shape within the drawing area.
- A computer screen is pixel-based, so a turtle cannot draw a mathematically perfect circle using straight movements.
- Increasing a regular polygon's side count makes it look more circular: an octagon looks rounder than a square, and a 20-sided shape looks rounder still.
- A 30- to 50-sided polygon is a practical approximation, while very high counts such as 360 require many more drawing operations.
- For a requested radius r, the total distance around the circle is its circumference, calculated as 2πr.
- For a polygon with n sides, each forward movement is circumference divided by n, and each turn is 360 divided by n.
- The example uses 3.14 for π, stores the radius, circumference, side count, segment length, and turn angle in variables, then repeats the movement with a for loop.
- More sides create a smoother-looking circle but slow the drawing because the turtle must execute more movements and turns.
- The turtle's up() and down() commands lift and lower its pen, allowing it to move to a new starting point without drawing a connecting line.
- Lab work includes a turtle clock using stamp(), initials drawn from line segments, and a Triforce assembled from triangles.
- The clock can be made with one turtle stamping its shape 12 times inside a loop rather than creating 12 separate turtles.
- Do not save a program as turtle.py, because that filename can shadow Python's turtle library.
- Regular credit requires five colored circles; the rings do not need to weave correctly over and under one another.
- The suggested structure uses five turtles, each responsible for moving to a ring position and drawing one circle.
- Python's turtle.circle(radius, extent) can draw a partial arc; an extent of 180 draws half a circle.
- For the extra-credit overlap effect, combine partial arcs with up() and down() to skip under-passes and redraw the portions that should appear on top.
Transcript excerpt
0:00 Uh so today is a particularly fun day because we get to go over shapes. Uh this is going to be a um a bit silly at first because you know the math we're doing is basically going to be um you know elementary school level for a bit and then we're going to jump into something that definitely feels a high school more high school level. Um but the project we are going to be working on today is right here in canvas. So what we'll do again for what you need to do this each week is that we go over here we select um the the module three which is right now we're learning about how to really
0:47 import stuff and just as a reminder with these videos. Okay. Um, these are embedded playlists, but for some reason they don't show the playlist anymore. So, the easy thing to do is just hit watch on YouTube. Um, really it doesn't show up the that anymore. Okay. I guess watch it in the browser right there and then you can hit next and it'll go to the next video in the playlist. >> Let's go ahead. So, um, >> one of the things >> also again, um, over here in this playlist is actually the video that has the most, >> uh, con it, I think it has the most views of of anything on my channel, which I find very funny because it's not
1:34 people from Temple who are necessarily looking this up. It is, um, 8.2,000 views, which is not that many. All this is is just basically um a very common issue that students have or beginners have, which is when you accidentally name um a program the name of the thing you're trying to import. This I'm not
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.