Chapter 2a - Variables & Arithmetic
Variablesβ
Introductionβ

Image extracted from StevenCurtis Medium
myNum = 4
print(myNum)
# print(myNum+1)
π§ͺ Try the code out!
Re-assigning values to variablesβ

Image extracted from StevenCurtis Medium
myNum = 4
print(myNum)
myNum = 5
print(myNum)
π§ͺ Try the code out!
Operations using variablesβ

myNum = 4
mySecondNum = 10
print(myNum + mySecondNum)
π§ͺ Try the code out!
Mini quiz β variablesβ
Variables
1 / 2What does this print?
x = 4x = 5print(x)
Pick an answer to see if you are right.
Usage Exampleβ
Using Examples and doing math with it.β
mom_age = 40 # Mmom_age represents mom's age # Line 3
leah_age = mom_age - 25 # leah_age represents Leah's age # Line 4
print("Age of Leah's mom is " + str(mom_age)) # typecast all data into string type # Line 5
print ("Age of Leah is " + str(leah_age)) # there is an additional whitespace in the text to improve readablity # Line 6
π§ͺ Try the code out!
Arithmetic Operatorsβ

Assigment Operatorsβ

Mini quiz β operatorsβ
Operators
1 / 2What is 10 % 3?
Pick an answer to see if you are right.
Practice Activities: Apples πβ
Start with some apples, then:
- add 5
- multiply by 4
- integer-divide by 2 (
//) - subtract 6
For 20 that is 20 β 25 β 100 β 50 β 44.
Write final_apples(start) so it returns that last number (no story text).
Arithmetic Operators Practiceβ

π Solution
Challenge: leftover applesβ
Write leftover(apples, friends) so it returns apples % friends.
Hiking Problemβ
Pete and Shannon are hiking. Shannon is always 2 miles ahead of Pete. What is the distance Shannon has covered if Pete has covered 10 miles? How would the program change if Shannon has covered twice as much distance as Pete?
π Example
Activityβ
π Assigment: Shannon position changed. Can you complete the program so it prints the updated direction?
Turtle and Variablesβ
Lesson 2 Learning Activities [E1] : Turtle graphics and variablesβ
- Change the Length of the Square
- Change the Angle.
This activity used Trinket turtle / canvas graphics. Piston runs text programs only, so the interactive drawing pad is gone.
Run turtle locally in Python (or Thonny / Mu) with the snippets above. Text I/O practice elsewhere on this page still uses Piston.
Lesson 2 Learning Activities [R] : Practice Activity 4 - Smiley Faceβ
- Change Contants (values that are repeated ont he code) to Variables such as the
200
This activity used Trinket turtle / canvas graphics. Piston runs text programs only, so the interactive drawing pad is gone.
Run turtle locally in Python (or Thonny / Mu) with the snippets above. Text I/O practice elsewhere on this page still uses Piston.
Exercisesβ
Pete walked miles miles. Shannon walked twice as far. Write shannon(miles) so it returns Shannon's distance.
Expected for 10:
20