Chapter 2a - Variables & Arithmetic
Variablesโ
Introductionโ
Variables are like boxes
Image extracted from StevenCurtis Medium
myNum = 4
print(myNum)
# print(myNum+1)
๐งช Try the code out!
Re-assigning values to variablesโ
Adding a different Object into the box
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!
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โ
Practice Activities: Apples ๐โ
Count of Apples ๐
write a program that tracks the mumber of apples the user ends up with after starting with a set number of apples. Students can set the initial number of apples to a number of their choice. The output should follow this script:
Intended Output
You have 20 apples.
You plant one tree from a seed you found in your attic and harvest five apples from it. You now have 25 apples.
You extract seeds from every apple you have, discard the apples, and plant all the seeds. You harvest four times as many apples as you had. You now have 100 apples.
You look over your apples and realize some of the trees were diseased! You have to throw out half of your apples. You now have 50.0 apples.
You decide to bake an apple pie. This requires six apples. You now have 44.0 apples.
โ You can solve the problem by fixing the following code using Trinket You can see that the code prints 20 in each instance instead of 25 | 100 | 50 | 44
Arithmetic Operators Practiceโ
๐ Solution
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โ
Exercise: Play with the code
- Change the Length of the Square
- Change the Angle.
Lesson 2 Learning Activities [R] : Practice Activity 4 - Smiley Faceโ
Exercise: Play with the code
- Change Contants (values that are repeated ont he code) to Variables such as the
200