This lab is about names: how naming things can be a powerful tool for thinking, and how to assign names to things in Python. One part philosophy, one part programming! There are a few things to do before you start. This setup will be the same for every lab from now on.

Variables: Naming values

A value is anything that is what it is, instead of referring to something else. In the turtle lab, you worked with integer values like 1, 100, and 65. Python has other types of values too, things like 3.1416, "hello", True, and False. Let's play with some values, using Python's interactive mode.

๐Ÿ’ป Run python. You will see >>>, Python's interactive prompt. Try out some common mathematical operators, which combine values to create new values:

>>> 10 + 10
>>> 100 - 99
>>> 6 * 4
>>> 72 / 9

A name is different from a value, because it refers to something else. For example, your name refers to you. But you are not the same as your name. You could change your name, and you would still be the same person. You might even be called by different names in different contexts.

A variable is a name that refers to a value. In Python, we assign a value to a variable by writing name = value. Take note: this use of = is very different from in math class! In math class, = is a statement expressing that two things are equal, such as 2 * 5 = 10. Here, we are not expressing that two things are equal. We are assigning the value on the right to the name on the left. Once you assign a value to a variable, you can use the variable's name instead of its value.

๐Ÿ’ป Try doing some math with variables instead of values. Here, we calculate the area of a circle.

>>> pi = 3.141592653
>>> radius = 10
>>> circle_area = pi * radius * radius
>>> circle_area

๐Ÿ’ป Exit interactive mode by running exit() (Control + D works too). Open by running code It's a simple program:

my_name ="Chris"
greeting = "Hello, " + my_name

๐Ÿ’ป What do you think this program will print? Run python

๐Ÿ’ป However, your name probably isn't Chris. Change so it says hello to you, save it, and run it again.

๐Ÿ’ป Now let's make more flexible. Change the first line of the program to:

my_name = input("What is your name? ")

Run the program again--now the program uses the input function to get a value from the user, and assigns whatever value the user typed in to my_name. By using the variable's name, the program can interact with an unknown value (the program can't know what the user will enter ahaed of time).

๐Ÿ’ป is a program which calculates the area of a circle, but it isn't finished. Finish the program, so that it prints out the area of the circle. Once the program is finished, it should work like this:

$ python
This program will calculate the area of a circle.
What is the circle's radius? 5

Functions: Naming code blocks

A code block is one or more lines of code. Here's a code block that draws a square:

1from turtle import *

You could name this block of code by defining a function:

1from turtle import *
3def square():
4 forward(100)
5 right(90)
6 forward(100)
7 right(90)
8 forward(100)
9 right(90)
10 forward(100)
11 right(90)

Take note of line 3: to define a function, we use the def keyword, then the function's name, then (), then :. The function's code block is indented on the following lines.

Defining a function assigns a name to a block of code, but it doesn't run the block of code. A function's code block doesn't run until the function is called. In the example above, square is called three times, on lines 13, 15, and 17. One reason for defining functions is to manage complexity. Once you have defined square (and tested it to make sure it's correct), you don't need to worry anymore about how it works. You can just call square(), and spend your attention on what you're trying to do with squares.

Functions with arguments

You might have noticed that square only draws one size of square, even though the code would be almost exactly the same for a square of a different size. To make square adjustable, we can change its definition so it takes an argument.

1from turtle import *
3def square(side_length):
4 forward(side_length)
5 right(90)
6 forward(side_length)
7 right(90)
8 forward(side_length)
9 right(90)
10 forward(side_length)
11 right(90)

An argument is a temporary variable which gets defined when a function is called. Instead of () on line 3, we now have (side_length). Because square now has one argument, it must be called with one value, as you can see on lines 13, 14, and 15. On line 13, square(100) calls square, temporarily assigning the variable side_length = 100. When square(80) is called on the next line, square is called again, but this time, side_length = 80. side_length does not exist as a variable outside of square.

๐Ÿ’ป Run code to open

Two functions are defined: triangle (which takes one argument), and rectangle (which takes two arguments). Neither function is finished--each code block is just pass, which means "do nothing." Replace pass with a code block which draws the correct shape for each function.

Note that defines triangle and rectangle, but does not call them. In fact, if you run python, nothing happens. Instead, test your functions by running:

$ python

Once this looks reasonable, run, which calls triangle and rectangle repeatedly to produce a nice effect.

$ python