CS110A Week 16 Day 1 lesson on Tuesday 12/01/2026.
Overview
CS110A develops a reusable Python turtle function for drawing filled equilateral polygons, deriving a turn angle of 360 divided by the number of sides and using a loop to draw the edges. The lesson also explains how mutable turtle and list objects can be changed through function parameters, introduces DNS lookup and caching, and contrasts local and global scope, including why global-variable changes require the global statement and are usually best avoided.
Key takeaways
- An equilateral polygon with n sides can be drawn by repeatedly moving the turtle one side length and turning it 360/n degrees.
- A reusable turtle drawing function can accept the Turtle object, starting angle, side count, side length, fill color, and an (x, y) tuple as parameters.
- Changes to a shared mutable object such as a Turtle or list can remain observable outside a function, while rebinding a local name does not by itself update a global name.
- DNS translates a domain name such as yahoo.com into an IP address through resolver caching and, when needed, root, top-level-domain, and authoritative name servers.
- Python local variables are visible within their function; assigning to a global name from a function requires a global declaration, but avoiding mutable global state is generally safer.
- The trapezoid exercise demonstrates function reuse through composition: three equilateral triangles, positioned with different starting angles, can form a larger non-equilateral shape.
Chapters
- The class begins by revisiting an equilateral-polygon drawing problem related to an earlier hexagon exercise.
- Candidate function inputs include starting angle, number of sides, side length, fill color, and an (x, y) location.
- A Turtle object can also be passed as an input so the function can draw with a turtle created elsewhere.
- Parameters become names available locally inside the function that receives them.
- Changing a shared mutable object, such as a Python list or Turtle, can affect the same object outside the function.
- The lesson contrasts mutable objects with immutable values such as strings and tuples, while noting that the deeper mechanics are beyond the course scope.
- The number of sides determines the polygon to draw: for example, 3 produces a triangle and 6 a hexagon.
- A general polygon function should use the side count as data rather than write separate fixed code for each shape.
- The discussion notes that a larger polygon can also be assembled from triangles, though that may require extra drawing logic.
- The turtle first moves to the requested starting location with its pen lifted, then turns by the supplied starting angle.
- For an equilateral triangle, the turtle turns 120 degrees at each corner; for a square, it turns 90 degrees.
- The general exterior turning angle is 360 degrees divided by the number of sides.
- The drawing plan starts with pen-up positioning, then applies the starting angle before drawing edges.
- For n sides, the plan counts n minus one turns after the initial setup, illustrated with 2 turns for a triangle and 3 for a square.
- A for loop is appropriate because the number of repetitions depends on the variable side count.
- The proposed function inputs are starting angle, number of sides, side length, fill color, and location.
- The x and y coordinates are grouped into one tuple, such as (x, y), rather than passed as separate location values.
- The algorithm moves to the location with the pen up, lowers the pen, turns to the starting angle, and then draws the polygon.
- The turn angle is calculated once as 360 divided by the number of sides.
- Each loop iteration moves forward by the side length and turns left by the calculated angle.
- The lesson maps the algorithm into numbered pseudocode steps before translating it into Python.
- The function is named draw_polygon and receives a Turtle object along with its drawing parameters.
- The location tuple is indexed at 0 for x and 1 for y when passing coordinates to the turtle's go-to operation.
- The code lifts the pen for positioning, lowers it before drawing, applies the starting-angle turn, and calculates the turn angle.
- A for loop with range uses the number of sides as its repetition bound; range stops before its endpoint.
- Inside the loop, the turtle moves forward by side length and turns by the precomputed turn angle.
- The fill is wrapped with begin_fill and end_fill, using the provided fill color.
- The example imports turtle, constructs a Turtle object, sets its speed, and calls draw_polygon with test arguments.
- The test uses a 30-degree starting angle, 12 sides, a red fill, and location (50, 50), then checks the result by counting edges.
- A simpler three-side test is used to confirm the loop draws a triangle, and the class fixes a missing comma and a fill_color spelling error.
- A trapezoid cannot be drawn directly by a function designed for equilateral polygons because its sides are not all equal.
- The class identifies a way to compose the target shape from three equilateral triangles.
- The polygon function can be called three times with adjusted starting angles to place the triangles together.
- The DNS explainer distinguishes human-readable domain names, such as yahoo.com, from the IP addresses computers use to communicate.
- A resolver, commonly associated in the explanation with an ISP, checks its cache before querying DNS hierarchy servers.
- The described lookup proceeds through a root server, a top-level-domain server such as .com, and an authoritative name server.
- Once the resolver obtains an IP address, it can return it to the computer and cache the result for later requests.
- Scope is introduced as the visibility of a name in a program.
- A variable created inside a function, such as total_inches, is local to that function and cannot be printed from the surrounding global scope.
- Trying to access total_inches outside its function produces a NameError because that local name is not defined there.
- A variable defined in the main program is described as global and can be read inside a function.
- The inches_per_foot example starts with a global value of 11 and attempts to correct it to 12 inside a function.
- Assigning to the name inside the function does not update the global value unless the function declares global inches_per_foot.
- With the global statement in place, the function's assignment changes the module-level value to 12.
- The class reviews statement types, including assignment, import, pass, return, and global statements.
- The class advises against relying on global variables because code in a large program can change shared state unexpectedly.
- Global names are presented as appropriate mainly for constants, while function parameters and local variables are recommended for ordinary values.
- A Module 12 assignment asks students to simulate a randomly moving drunkard with a Python Turtle; part one is assigned for home and part two is planned for Thursday.
- Rock-paper-scissors and random popping shapes are mentioned as useful practice with conditionals and related programming skills.
Transcript excerpt
0:00 Good morning, guys. Sorry about the delay. Okay, again. All right. So we're going to this is our agenda today. So we're going to solve that problem first, okay? So I already set it up over here. So this is the problem. Equilateral hexagon. And then we we did the not hexagon,
0:48 right? So equilateral polygon. Right? And then we did the hexagon, right? So that's a good match, okay? And then All right. So what are the inputs? What are the inputs in this problem? Yeah, so all of these, right? So polygon of a starting angle, a given number of sides, all of these, right?
1:40 So right? And the side length and given color at a given point, all of these are inputs, okay? So like 1 2 3 4, right? If you take the the the coordinate also as two input, then probably there are about five, right? Or six. Okay? So, all of this. Turtle is an input? Yeah, but since we this is turtle, yeah. Turtle is our drawing tool. I mean, it's up to you. I mean, I'm I'm okay with either way, right? So, turtle yeah, if you pass the turtle, yeah, it's actually an input. Okay? So,
2:31 Mhm. Um inputs eventually become local variable to the function that we passed it. So, input don't take it as in the sense that we use the keyboard to enter input. No, it's we are talking about the function that going to draw the polygon and then all of those going come in as the input to that function and then eventually they become local variables. So, parameters become local variables. Okay? And then the scope is going to be inside the function there. Okay? Yeah, so well, yeah, you pass the turtle
3:27 and then you do something with that
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, indika walimuni.