2 – Crunching Numbers

🎯 Learning Objectives

Develop the Programming and DevelopmentAlgorithms Learning Strands:

  • Describe the meaning of assignment statements
  • Use simple arithmetic expressions in assignment statements to calculate values
  • Receive input from the keyboard and convert it to a numerical value
💬 Key Vocabulary
  • Input
  • output
  • variables
  • operators
  • expressions
  • integer and string
  • execution
  • walk-through

🔎 Crunching Numbers in Python

Last lesson, you…

  • Defined what algorithms and programs are
  • Wrote and executed your first Python programs (and weeded out syntax errors)

In this lesson, you will…

  • Use arithmetic expressions to calculate values
  • Use variables to store and reference values
  • Follow walk-throughs of code and keep track of variable values
  • Write programs that receive numerical input from the keyboard

📖 Assignment and Expressions

  • Assignments are not equations.
  • This assignment does not mean that the days variable always equals 365.
  • Assignments are instructions to be executed.
  • The screenshot on the right is an instruction to assign the value 365 to the days variable.
  • A subsequent assignment can assign a new value to the days variable, replacing the previous value.
  • You can use expressions in assignments.
  • This is an instruction to evaluate the expression on the right and then assign the value to the days variable on the left.
  • A subsequent assignment can assign a new value to the days variable, replacing the previous value.

📖 Arithmetic operators (in Python)

You can use these operators to form arithmetic expressions.

SymbolMeaningExamplesMeaning
+additiona + 1a plus 1
differenceb – cb minus c
*multiplication3 * d3 times d
/division9 / 49 divided by 4 (value: 2.25)
//integer division15 // 2quotient of 15÷2 (value: 7)
%remainder of integer division15 % 2remainder of 15÷2 (value: 1)
**exponentiation2 ** 82 to the power of 8 (value: 256)

📖 Referring to Variables

  • An expression can refer to the values of variables.
  • To evaluate this expression, the days variable must have been assigned a value.
  • During program execution, a variable must have been assigned a value before that value is referred to.

📝 Activity 1 – Order Matters

  • You will be given a program that is supposed to convert a length of time from seconds to minutes.
  • Rearrange (change the order of) the statements, so that the program runs to completion without errors.

📝 Activity 4 – How to Input Numbers

  • Work on programs that receive numerical input from the keyboard and process it.
  • Use the trinket.io assignment to follow the instructions on the worksheet.

In this lesson, you…

  • Used arithmetic expressions to calculate values
  • Used variables to store and reference values
  • Followed walk-throughs of code and kept track of variable values
  • Wrote programs that receive numerical input from the keyboard

Next lesson, you will…

  • Use selection (if statements) to control the flow of program execution
  • Introduce elements of randomness into your programs

🏅 Badge it

🥈 Silver Badge
🥇 Gold Badge
  • Complete the Gold badge tasks in the worksheet by adding some extra features to your code.
🥉 Platinum Badge
  • Complete the Platinum badge tasks in the worksheet by changing how your weight on the moon is calculated.