Class 3 - Definitions
Date: September 14, 2026
Skills: None
Notes (review before class)
The Editor
The interactions panel works like a calculator; the editor, above it, holds code to save in a file, and helps organize it as it grows.
Naming Values
A definition, NAME = EXPRESSION, associates the value of EXPRESSION with
NAME. Put this in your file, click PLL: Run Python File, then enter
red_circ at the prompt:
red_circ = circle(30, "solid", "red")
- From then on,
NAMEgives the value: afterx = 5 + 4,xis9, not5 + 4. - Using a name that isn't bound to a value, like
puppy, is aNameError. - Names (also called identifiers, or variables) can contain letters, digits,
and underscores, but no spaces or dashes: join words with underscores, as in
red_circ.
Names Versus Strings
- Quotes tell them apart:
puppyis a name,"puppy"a string."hot pink"is data, buthot pinkis not a name. - Entering
puppylooks up the value stored under that name;"puppy"just writes down data (like3). So an undefined name is an error, but a new string is fine.
Expressions Versus Statements
5 + 8
x = 14 + 16
triangle(20, "solid", "purple")
blue_circ = circle(x, "solid", "blue")
- Expressions (lines 1 and 3) produce values, which Python shows. A name on
its own, like
blue_circ, is an expression too. - Statements give other instructions. So far, definitions (lines 2 and 4) are our only statements; Python shows nothing for them.
The Program Directory
A definition adds an entry to Python's directory, which associates names with values:
width = 30
height = width * 3
- For
height, Python looks upwidth, substitutes its value (30 * 3), and stores the result,90, not the expression (this will matter when we use names to avoid repeating a computation). - Definitions are added in order: with these lines swapped,
widthisn't in the directory yet, sowidth * 3is aNameError. - You can't see the directory, so write it down on paper to work out what a
program does. Try: what appears when a file with
name = "Matthias"and then"name"runs, and how does each line use the directory? - Reassignment (giving a name that has a value a new one, like a later
width = 50) is allowed by Python, but at thebeginnerlevel PLL rules it out, as a common cause of bugs: it reports thatwidthis already assigned, and doesn't run the code. So, for now, a name keeps its first value.
The Run Button
PLL: Run Python File clears the directory (except Python's and PLL's
functions), then runs the file from the top. After a file with width,
height, and blue_rect = rectangle(width, height, "solid", "blue"), the
directory is:
| Name | Value |
|---|---|
circle, rectangle, ... | PLL's functions |
width | 30 |
height | 90 |
blue_rect | the blue rectangle image |
- Names used at the prompt are looked up there, but
beside(blue_rect, rectangle(20, 20, "solid", "purple"))adds nothing: neither image it makes has a name. A definition adds the combined image (but not the purple rectangle alone):
two_rects = beside(blue_rect, rectangle(20, 20, "solid", "purple"))
- Running clears the directory, so a name defined only at the prompt is gone after the next run.
Using Names to Build Images
Nested expressions get hard to read:
overlay(rotate(45, triangle(30, "solid", "purple")),
rectangle(60, 60, "solid", "green"))
Naming the shapes first is clearer (this version also adds a second triangle):
purple_tri = triangle(30, "solid", "purple")
green_sqr = rectangle(60, 60, "solid", "green")
above(purple_tri,
overlay(rotate(45, purple_tri), green_sqr))
- The names say what each shape is, and
purple_triis used twice, but itstriangleexpression is written once. - Running this file shows only the combined image: definitions show nothing.
- Name the values used more than once, or important for understanding the program, not every value.
- Try: rewrite your flag of Armenia from Class 2, naming each stripe.
Announcements
- Talk about HW submission, how it is graded with Assignment Chats -- first HW is due on Sunday, first Assignment Chats is next Tuesday.
- Make sure people are on Discord.
Recap (15 mins)
Here is a file that builds a window out of one blue square, and computes the area of a circle. Read it before running it:
blue_square = rectangle(40, 40, "solid", "blue")
two_squares = beside(blue_square, blue_square)
window = above(two_squares, two_squares)
radius = 10
area = 3.14 * radius * radius
window
area
"area"
- What appears in the interactions panel when this file runs, and in what order?
- Write down the directory after the run. How many blue squares are in
window, and how many times does Python evaluate arectangleexpression? - What would happen if the
areadefinition came beforeradius = 10? What if the file ended withradius = 20?
Class Exercise (40 mins)
-
Define an orange triangle with side length 35. Now evaluate the name (or identifier, or variable -- all of these are synonyms that programmers use for the same thing) (i.e.,
orange_triangle) in the interactions panel. -
Define a side length and color (as two separate definitions), and then define a square using those names for the side length and color.
-
Now define a second version where you do not use the side length and color variables (or name, or identifier). Notice that when you evaluate both identifiers in the interactions, they are exactly the same, but the code is easier to read with the separate definitions.
-
Try defining a new side length with the same name, later in the file. Note what happens when you try to run it. Giving a name that already has a value a new one is called "reassignment". Python allows it, but at the
beginnerlevel, PLL rules it out (and will not run your file), since it is a common cause of bugs. -
Now define an image that has a small yellow circle on top of a larger black rectangle. Try several different ways, using different number of definitions. See which is easiest to understand.
-
Go back to your previous examples and see if there are other definitions you can add. Did you make definitions for the radius of circles you made? The length of your orange triangle? Try changing the code to include all of those.
-
If you make a definition for the color of your orange triangle, what did you name it? If you want to change the color of the triangle, will you have to change the name of the definition (i.e., if you used
orange, but wanted to make the triangle purple, you should probably change it topurple = "purple")? If so, that's an indication that perhaps the definition was not helpful, or didn't have a good name. Having to change the definition name means you will also have to change the uses of the definition! -
Now make a new image that puts two copies of your small yellow circle side-by-side on top of your black rectangle.
-
Now think of a simple flag or logo design. Sketch out a plan on paper and come up with names for different parts of the design. If you have time, make definitions for each part, and compose them together to make the final image.
Wrap-up (5 mins)
- Definitions involve the first aspect of program design. Notice how there were often many different ways of writing the same program, some of which were easier to understand, easier to modify, and even possibly easier to write.
- One of the main lessons of this class, and indeed this whole curriculum, is that well designed code is something much more important, and often more challenging, than just getting code to work. Of course code has to work, but for it to work in real systems, at the scale that we are building them, messy, convoluted, or confusing code can make it so hard for people to understand what is going on that even getting it to work, as the programs get bigger, becomes impossible.
- By focusing on design, from the very beginning, you'll develop skills that will help you all the way through, as programs get bigger and bigger!