Moneyball
Reading Questions
Quiz
Turtle in Idle
- We can also run turtle programs in Idle, but there is one important thing to watch out for
- Do not name a file turtle.py
- If you do, then your program will try to import the file you created instead of the turtle library, and your program will not work
- Name it anything else and it should work fine
Variable Reassignment
- Once a variable has been given a value, it’s value can be changed
That’s why they are called variables
x = 1 x = 2 print(x)
- Recall that the assignment operator, =, does not have the same meaning that it has in mathematics.
- The above two equations would be impossible in mathematics
- In a program they are possible because only line is executed at a time
When a variable is assigned to antoher variable, the value is copied
x = 1 y = x x = 2 print(y)
- So the variable y in the above example does not change it’s value when the value of x is changed
And don’t forget order matters
x = 1 print(y) y = x
The above program produces an error because the variable y does not have a value when the print statement is executed
Variable Updating
Can also use a variable’s current value in computing a new value for it
x = 1 x = x + 2 print(x)
- This looks weird becasue in mathmatics this would be impossible
- But remember the assignment operator, =, is not equality
- This works because the assignment operator has the lowest precedence
- So on the second line in the above example, x is added to 2, and becasue x already has a value, 3, the assigment changes the value of x
Beware of updating a variable that has not been defined yet
y = y + 3 print(y)
- Will produce an error because y has no value to add to 3
- This can be used to reduce the number of variables a program needs
Take this version of the paper folding program from lab
zero_fold_thickness = 0.004 one_fold_thickness = zero_fold_thickness * 2 two_fold_thickness = one_fold_thickness * 2 three_fold_thickness = two_fold_thickness * 2 four_fold_thickness = three_fold_thickness * 2 five_fold_thickness = four_fold_thickness * 2 six_fold_thickness = five_fold_thickness * 2 seven_fold_thickness = six_fold_thickness * 2 eight_fold_thickness = seven_fold_thickness * 2 nine_fold_thickness = eight_fold_thickness * 2 ten_fold_thickness = nine_fold_thickness * 2 print("A piece of paper folded in half 10 times would be", ten_fold_thickness, "inches thick.")
By using variable updating, this can be simplified
paper_thickness = 0.004 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 paper_thickness = paper_thickness * 2 print("A piece of paper folded in half 10 times would be", paper_thickness, "inches thick.")
Of course, this could also be much simplified by using an exponent, but not allo programs will have that advantage