Skip to main content

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!


Mini quiz β€” variables​

Quiz

Variables

1 / 2

What does this print?

x = 4
x = 5
print(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​

Quiz

Operators

1 / 2

What is 10 % 3?

Pick an answer to see if you are right.

Practice Activities: Apples πŸŽβ€‹

Start with some apples, then:

  1. add 5
  2. multiply by 4
  3. integer-divide by 2 (//)
  4. 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​

πŸ‘€ Lesson 2 Graphics

πŸ”¨Exercise: Play with the code
  • Change the Length of the Square
  • Change the Angle.
Canvas not available on Piston

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​

πŸ‘€ l3 a4 - Smiley

πŸ”¨Exercise: Play with the code
  • Change Contants (values that are repeated ont he code) to Variables such as the 200
Canvas not available on Piston

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​

Activity: Hiking math

Pete walked miles miles. Shannon walked twice as far. Write shannon(miles) so it returns Shannon's distance.

Expected for 10:

20
Instructor Notes