Arithmetic Operator Playground
Run the Arithmetic Operator Playground MicroSim Fullscreen
About This MicroSim
MicroPython has seven arithmetic operators. Most work just like math class. Three of them surprise beginners:
- Division
/always gives a float (a number with a decimal point), even for two whole numbers.7 / 2gives3.5, and6 / 2gives3.0. - Integer division
//counts only the full groups.7 // 2gives3. - Remainder
%(also called modulo) gives what is left over.7 % 2gives1.
The table shows all seven results for the same two numbers, a and b,
plus the type of each result. The picture draws a as squares and groups
them into blocks of b. The green blocks are the // answer. The orange
squares left over are the % answer.
How to Use
- Change a and b in the number fields. Before you look at the table, predict each result.
- Look at the picture. Count the green groups and the orange leftovers.
Do they match the
//and%rows? - Check the Type column. Which operator gives a float even when both numbers are ints?
- Tick "Use decimal a" to set
a = 7.5. Now every result is a float. - Pick a Robot example to see where each operator shows up in real robot code.
Challenge: Set a = 23 and b = 5. What are a // b and a % b? Then
decide how a robot could beep on every 5th trip through its main loop. The
operators are introduced in
Chapter 3: Arithmetic Operators.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Learning Objective
Students will apply MicroPython's seven arithmetic operators to predict
results and result types, and will select the appropriate operator
(/, //, or %) for a given robot task (Bloom's Taxonomy: Apply).
Grade Level
Grades 8–12.
Duration
15 minutes.
Prerequisites
- The "Integers" and "Floats" sections of Chapter 3.
- Using the REPL to evaluate an expression, from the REPL section of the same chapter.
Background
Two behaviors cause most novice errors here. First, Python 3 (and
MicroPython) made / true division, so 6 / 2 is the float 3.0; passing
that value to an API that expects an int, such as a PWM duty value, raises a
TypeError. Second, students rarely have a concrete model of %. The block
picture grounds both // and % in the same division-with-remainder
image students learned in elementary arithmetic, so the two operators are
seen as the two halves of one idea.
Activities
- Predict the table (3 min). With
a = 7andb = 2hidden behind a sheet of paper, students write all seven results and types, then reveal and check. - Pattern hunt (4 min). Students try
afrom 20 to 25 withb = 5and describe the pattern in the%column (it cycles 0, 1, 2, 3, 4, 0). - Challenge (3 min).
23 // 5 = 4and23 % 5 = 3. Students then write the conditionloop_count % 5 == 0for "beep every 5th loop." - Robot examples (3 min). Students step through the four presets and state, for each, why that operator is the right choice.
- REPL check (2 min). Students type two of the expressions into the Thonny REPL and confirm the MicroSim matches real MicroPython.
Assessment
- Formative: Check the prediction sheets from Activity 1; the most
common error is writing
3instead of3.5for7 / 2. - Exit ticket: "A robot should turn around after every 4th wall it
finds. Write the condition using
%. Then say what30 / 2and30 // 2each give." (Answers:walls % 4 == 0;15.0and15.) - Rubric (4-point): Exemplary — correct results and types for all
seven operators, including float inputs, and a correct
%condition; Proficient — correct results with one type error; Developing — confuses/and//or//and%; Beginning — treats all division as the same operation.
References
- Chapter 3: MicroPython and Development Environment Setup — the arithmetic operators table this MicroSim explores.
- Python Tutorial: Using Python as a Calculator — official examples of
/,//,%, and**. - Modulo — Wikipedia — the remainder operation and its uses.
- MicroPython documentation — MicroPython follows Python 3 arithmetic rules.
- Python Operator Playground (Moving Rainbow) — the MicroSim this version was adapted from.