Home Tutorials Tutorial on Arithmetic Operators in MicroPython

Tutorial on Arithmetic Operators in MicroPython

by shedboy71

Arithmetic operators are essential in MicroPython, enabling basic mathematical operations like addition, subtraction, multiplication, and more. These operators are straightforward to use, and this tutorial will walk you through the various arithmetic operators in MicroPython with examples.

Table of Contents:

1. Overview of Arithmetic Operators

MicroPython supports several arithmetic operators for performing basic mathematical operations:

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
// Floor Division a // b
% Modulus (Remainder) a % b
** Exponentiation (Power) a ** b

Let’s explore each of these operators with examples.

2. Addition (+)

The + operator adds two numbers together.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 10
b = 5
result = a + b
print(result) # Output: 15
a = 10 b = 5 result = a + b print(result) # Output: 15
a = 10
b = 5
result = a + b
print(result) # Output: 15

 

You can also use the addition operator with other data types like strings to concatenate them.

Example with Strings:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World
str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # Output: Hello World
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World

 

3. Subtraction (-)

The – operator subtracts the second number from the first.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 10
b = 3
result = a - b
print(result) # Output: 7
a = 10 b = 3 result = a - b print(result) # Output: 7
a = 10
b = 3
result = a - b
print(result) # Output: 7

 

4. Multiplication (*)

The * operator multiplies two numbers.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 7
b = 6
result = a * b
print(result) # Output: 42
a = 7 b = 6 result = a * b print(result) # Output: 42
a = 7
b = 6
result = a * b
print(result) # Output: 42

You can also use the * operator with strings to repeat them a given number of times.

Example with Strings:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
text = "MicroPython"
result = text * 3
print(result) # Output: MicroPythonMicroPythonMicroPython
text = "MicroPython" result = text * 3 print(result) # Output: MicroPythonMicroPythonMicroPython
text = "MicroPython"
result = text * 3
print(result) # Output: MicroPythonMicroPythonMicroPython

5. Division (/)

The / operator divides the first number by the second. The result is always a floating-point number, even if both numbers are integers.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 20
b = 4
result = a / b
print(result) # Output: 5.0
a = 20 b = 4 result = a / b print(result) # Output: 5.0
a = 20
b = 4
result = a / b
print(result) # Output: 5.0

Even though both a and b are integers, the result is a floating-point number 5.0.

6. Floor Division (//)

The // operator performs floor division, which returns the quotient without the remainder. The result is an integer when both operands are integers.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 10
b = 3
result = a // b
print(result) # Output: 3
a = 10 b = 3 result = a // b print(result) # Output: 3
a = 10
b = 3
result = a // b
print(result) # Output: 3

The division result is 3.3333…, but with floor division, it becomes 3.

7. Modulus (%)

The modulus operator (%) returns the remainder of the division between two numbers.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 10
b = 3
result = a % b
print(result) # Output: 1
a = 10 b = 3 result = a % b print(result) # Output: 1
a = 10
b = 3
result = a % b
print(result) # Output: 1

In this case, 10 / 3 results in a quotient of 3 and a remainder of 1, so the modulus result is 1.

8. Exponentiation (**)

The ** operator raises the first number to the power of the second number.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a = 2
b = 3
result = a ** b
print(result) # Output: 8
a = 2 b = 3 result = a ** b print(result) # Output: 8
a = 2
b = 3
result = a ** b
print(result) # Output: 8

Here, 2 ** 3 is equal to 2 * 2 * 2, which is 8.

9. Operator Precedence

Operator precedence determines the order in which operations are performed. Multiplication, division, floor division, and modulus have higher precedence than addition and subtraction.

Here’s the precedence from highest to lowest:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
** (Exponentiation)
*, /, //, % (Multiplication, Division, Floor Division, Modulus)
+, - (Addition, Subtraction)
** (Exponentiation) *, /, //, % (Multiplication, Division, Floor Division, Modulus) +, - (Addition, Subtraction)
** (Exponentiation)
*, /, //, % (Multiplication, Division, Floor Division, Modulus)
+, - (Addition, Subtraction)

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
result = 5 + 3 * 2
print(result) # Output: 11
result = 5 + 3 * 2 print(result) # Output: 11
result = 5 + 3 * 2
print(result) # Output: 11

In this example, multiplication (3 * 2) is performed first, followed by addition (5 + 6), resulting in 11.

If you want to change the order of operations, use parentheses ().

Example with Parentheses:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
result = (5 + 3) * 2
print(result) # Output: 16
result = (5 + 3) * 2 print(result) # Output: 16
result = (5 + 3) * 2
print(result) # Output: 16

In this case, the expression inside the parentheses is evaluated first (5 + 3), then multiplied by 2.

Complete Example: Using Arithmetic Operators

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Arithmetic operations on two numbers
a = 12
b = 5
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
floor_division = a // b
modulus = a % b
exponentiation = a ** b
print("Addition:", addition) # Output: 17
print("Subtraction:", subtraction) # Output: 7
print("Multiplication:", multiplication) # Output: 60
print("Division:", division) # Output: 2.4
print("Floor Division:", floor_division) # Output: 2
print("Modulus:", modulus) # Output: 2
print("Exponentiation:", exponentiation) # Output: 248832
# Arithmetic operations on two numbers a = 12 b = 5 addition = a + b subtraction = a - b multiplication = a * b division = a / b floor_division = a // b modulus = a % b exponentiation = a ** b print("Addition:", addition) # Output: 17 print("Subtraction:", subtraction) # Output: 7 print("Multiplication:", multiplication) # Output: 60 print("Division:", division) # Output: 2.4 print("Floor Division:", floor_division) # Output: 2 print("Modulus:", modulus) # Output: 2 print("Exponentiation:", exponentiation) # Output: 248832
# Arithmetic operations on two numbers
a = 12
b = 5

addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
floor_division = a // b
modulus = a % b
exponentiation = a ** b

print("Addition:", addition) # Output: 17
print("Subtraction:", subtraction) # Output: 7
print("Multiplication:", multiplication) # Output: 60
print("Division:", division) # Output: 2.4
print("Floor Division:", floor_division) # Output: 2
print("Modulus:", modulus) # Output: 2
print("Exponentiation:", exponentiation) # Output: 248832

 

Conclusion

In MicroPython, arithmetic operators are straightforward and powerful tools for performing mathematical calculations.

Understanding how these operators work and how operator precedence affects the order of operations will allow you to perform complex arithmetic efficiently.

You now know how to:

Add, subtract, multiply, and divide numbers.
Use floor division and modulus for quotient and remainder.
Raise numbers to powers.
Control operator precedence with parentheses

You may also like