Reading Questions
Quiz
Variables
- Variables allow you to name values in a program
- They can be useful especially if you use a value multiple times
For example, the star program from the previous lab
import turtle turtle.forward(100) turtle.left(144) turtle.forward(100) turtle.left(144) turtle.forward(100) turtle.left(144) turtle.forward(100) turtle.left(144) turtle.forward(100) turtle.left(144)
- If you want to change the size of the star, you have to change 5 numbers
With a variable, you only have to change 1
import turtle size = 100 turtle.forward(size) turtle.left(144) turtle.forward(size) turtle.left(144) turtle.forward(size) turtle.left(144) turtle.forward(size) turtle.left(144) turtle.forward(size) turtle.left(144)
- Using a varible for the angle
Changing it makes the program no longer create a perfect star, but when you were creating the program you probably were annoyed that you had to go back and change all of those numbers multiple times
import turtle size = 100 turn_angle = 144 turtle.forward(size) turtle.left(turn_angle) turtle.forward(size) turtle.left(turn_angle) turtle.forward(size) turtle.left(turn_angle) turtle.forward(size) turtle.left(turn_angle) turtle.forward(size) turtle.left(turn_angle)
- Using a variable would have made that easier
- Variables can also make your code easier to read and easier to debug
Take the program from the last lab to draw a centered square
import turtle turtle.up() turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.down() turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90)
We can use a variable to specify the size of the square
import turtle size = 100 turtle.up() turtle.forward(size / 2) turtle.left(90) turtle.forward(size / 2) turtle.left(90) turtle.down() turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90)
- Note, we can use an expression, in this case a mathematical equation, anywhere you can use a value
But it might make for sense to use another variable
import turtle size = 100 center_to_edge_dist = size / 2 turtle.up() turtle.forward(center_to_edge_dist) turtle.left(90) turtle.forward(center_to_edge_dist) turtle.left(90) turtle.down() turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90)
- This is a really simple example, but it has two advantages
- The variable name serves as an explanation for why the turtle is moving size / 2
- It makes the code more readable, easier to understand
- This is important for you, because it will reduce the chances of you making a silly mistake
- It can also make finding a fixing errors in your code easier
- Each variable you create is an opportunity to print and check the progress of your program when it is not behaving as you expect or want it to
The fewer computations each line does are fewer places that there could be a mistake (and fewer things you have to check when there is an error on a line)
Style
- Variable names must begin with a letter or underscore and can contain numbers
- By convention we use all lowercase, and use underscores to separate words
- There are also a list of reserved words you can’t use as variable names
- Don’t worry about remembering these, as idle highlights them to let you know
- Meaninful names are important, they help make the code easier to read
- single letter variable names a, b, c, etc., are not meaningful
- Can also use comments and white space to make code easier to read
- Comments are lines of code that are ignored when running the program but are useful when reading the program
Comments start with the # character
import turtle size = 100 center_to_edge_dist = size / 2 # move the turtle to the upper right corner of the square turtle.up() turtle.forward(center_to_edge_dist) turtle.left(90) turtle.forward(center_to_edge_dist) turtle.left(90) turtle.down() # draw the square turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90)
- These can also help you debug your code
When a program gets big it helps you find the part of your code that contains the problem
Expressions
- Expressions are code that evaluate to a value
- Mathematical equations for example are expressions
For example,
4 / 2 + 1
- Evaluates to the value 3.0
As we saw above, variables can be used in expressions
20 + size / 2
- Expressions behave much like mathematical expressions you know, there is precedence and order of operations
One operation is performed at a time until a value is computed
1 + 3 / 2 # division first 1 + 1.5 # then addtion 2.5 # final value
You can use parentheses to change the order
(1 + 3) / 2 # addition first 4 / 2 # then division 2.0 # final value
- These are all the rules you are already familiar with
- A couple operators may be new
- The ** is exponentiation
- The // operator is integer division, when the operands are integers the result is how many times the denominator evenly goes into the numerator
- The % operator is the mod operator, the remainder of integer division
These operators together can be really useful for some types of unit conversion like minutes to hours, or cents into coins, or inches into feet.
total_minutes = 121 hours = total_minutes // 60 minutes = total_minutes % 60 print(total_minutes, "is", hours, "hours and", minutes, "minutes")
- Each of the above lines is a statement, they don’t evaluate to a value
- Statements are commands to the computer
- The first three are assignment statements, they create a variable that associates the name of the left of the = symbol to the value on the right, which may be an expression that evaluates to a value
- The print statement commands the computer to display some text
- Print statements can contain multiple values separated by a comma
- Note, each of the values is separated with a space, when printed
If you want to print text, but it in quotes