Comparison operators in MicroPython are used to compare two values. They return a boolean value, either True
or False
, based on the comparison. These operators are commonly used in control flow structures like if
statements and loops to make decisions.
This tutorial will guide you through the comparison operators in MicroPython with code examples to demonstrate their use.
Table of Contents:
1. Overview of Comparison Operators
MicroPython supports the following comparison operators:
Operator | Description | Example |
---|---|---|
== |
Equal to: Returns True if both values are equal |
a == b |
!= |
Not equal to: Returns True if values are not equal |
a != b |
> |
Greater than: Returns True if the left value is greater than the right |
a > b |
< |
Less than: Returns True if the left value is less than the right |
a < b |
>= |
Greater than or equal to: Returns True if the left value is greater than or equal to the right |
a >= b |
<= |
Less than or equal to: Returns True if the left value is less than or equal to the right |
a <= b |
2. Equality (==)
The equality operator == checks if two values are equal. If they are equal, it returns True; otherwise, it returns False.
Syntax:
result = a == b
Example:
a = 5 b = 5 result = a == b print(result) # Output: True
Explanation:
Since both a and b are equal, the comparison returns True.
3. Inequality (!=)
The inequality operator != checks if two values are not equal. If they are not equal, it returns True; otherwise, it returns False.
Syntax:
result = a != b
Example:
a = 5 b = 3 result = a != b print(result) # Output: True
Explanation:
Since a is not equal to b, the comparison returns True.
4. Greater Than (>)
The greater than operator > checks if the value on the left is greater than the value on the right. It returns True if the left value is greater.
Syntax:
result = a > b
Example:
a = 10 b = 5 result = a > b print(result) # Output: True
Explanation:
Since 10 is greater than 5, the comparison returns True.
5. Less Than (<)
The less than operator < checks if the value on the left is less than the value on the right. It returns True if the left value is less.
Syntax:
result = a < b
Example:
a = 3 b = 7 result = a < b print(result) # Output: True
Explanation:
Since 3 is less than 7, the comparison returns True.
6. Greater Than or Equal To (>=)
The greater than or equal to operator >= checks if the value on the left is either greater than or equal to the value on the right. It returns True if the left value is greater than or equal to the right.
Syntax:
result = a >= b
Example:
a = 8 b = 8 result = a >= b print(result) # Output: True
Explanation:
Since 8 is equal to 8, the comparison returns True.
7. Less Than or Equal To (<=)
The less than or equal to operator <= checks if the value on the left is either less than or equal to the value on the right. It returns True if the left value is less than or equal to the right.
Syntax:
result = a <= b
Example:
a = 5 b = 10 result = a <= b print(result) # Output: True
Explanation:
Since 5 is less than 10, the comparison returns True.
8. Using Comparison Operators with Strings
Comparison operators can also be used with strings. In MicroPython, strings are compared lexicographically, which means the comparison is based on the alphabetical order of the characters.
Example:
str1 = "apple" str2 = "banana" # Comparing strings result = str1 < str2 print(result) # Output: True
Explanation:
Since “apple” comes before “banana” in alphabetical order, the comparison returns True.
9. Chaining Comparison Operators
MicroPython allows chaining of comparison operators. This can be useful when you need to check if a value lies within a certain range.
Example:
x = 15 # Chaining comparison operators result = 10 < x < 20 print(result) # Output: True
Explanation:
The expression 10 < x < 20 checks if x is greater than 10 and less than 20. Since x = 15, the expression returns True.
10. Practical Examples
Example 1: Checking Age Eligibility
age = 18 # Check if age is within the voting age range if age >= 18: print("You are eligible to vote.") else: print("You are not eligible to vote.")
Output:
You are eligible to vote.
Example 2: Grading System
score = 85 # Use comparison operators to assign grades if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") else: print("Grade: F")
Output:
Grade: B
Example 3: Range Check
temperature = 22 # Check if temperature is within a comfortable range if 18 <= temperature <= 25: print("The temperature is comfortable.") else: print("The temperature is not comfortable.") Output:
The temperature is comfortable.
Conclusion
Comparison operators are fundamental to decision-making in MicroPython. They allow you to compare values and execute code based on the result.
You now know how to:
Use equality and inequality operators (==, !=).
Compare values using greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
Apply these operators to both numeric and string values.
Chain comparison operators for more complex conditions.
Understanding and using comparison operators effectively will enable you to write more dynamic and responsive code in your MicroPython projects.