Home Tutorials Tutorial on Logical Operators in MicroPython

Tutorial on Logical Operators in MicroPython

by shedboy71

Logical operators in MicroPython allow you to combine or manipulate boolean values (True and False).

These operators are essential when making decisions in your code, particularly when working with conditions in if statements, loops, or other control flow structures.

This tutorial will walk you through the logical operators available in MicroPython, along with practical examples.

Table of Contents:

1. Overview of Logical Operators

MicroPython supports three logical operators:

Operator Description Example
and Returns True if both operands are True, otherwise False a and b
or Returns True if at least one operand is True a or b
not Returns True if the operand is False, and vice versa not a

These operators are typically used to evaluate conditions or expressions that return boolean values.

 

2. and Operator

The and operator returns True only if both conditions are True. If any of the conditions are False, the result will be False.

Syntax:

result = condition1 and condition2

Example:

a = True
b = False

result = a and b
print(result)  # Output: False

Explanation:

Since b is False, the entire expression evaluates to False even though a is True.

Example with Numeric Comparisons:

x = 10
y = 5

# Checking if both conditions are true
result = (x > 5) and (y < 10)
print(result)  # Output: True

3. or Operator

The or operator returns True if at least one of the conditions is True. It only returns False if both conditions are False.

Syntax:

result = condition1 or condition2

Example:

a = True
b = False

result = a or b
print(result)  # Output: True

Explanation:

Since a is True, the expression evaluates to True regardless of b.

Example with Numeric Comparisons:

x = 10
y = 15

# Checking if at least one condition is true
result = (x < 5) or (y > 10)
print(result)  # Output: True

4. not Operator

The not operator negates the boolean value of a condition. If the condition is True, not makes it False, and vice versa.

Syntax:

result = not condition

Example:

a = True

result = not a
print(result)  # Output: False

Explanation:

Since a is True, the not operator negates it and returns False.

Example with Numeric Comparison:

x = 7

# Negating the result of a comparison
result = not (x > 5)
print(result)  # Output: False

5. Combining Logical Operators

You can combine and, or, and not operators to create more complex logical expressions.

Example:

x = 8
y = 3
z = 12

# Combining logical operators
result = (x > y) and (z > x) or (y == 3)
print(result)  # Output: True

Explanation:

The expression (x > y) is True, and (z > x) is True, so (x > y) and (z > x) evaluates to True.
Since the first part is True, the or condition is not even checked (short-circuiting).

6. Short-Circuiting in Logical Operators

In MicroPython, logical operators use short-circuit evaluation, meaning that the second operand is evaluated only if necessary:

For and: If the first condition is False, the second condition is not checked because the result will be False regardless.
For or: If the first condition is True, the second condition is not checked because the result will be True regardless.

Example of Short-Circuiting with and:

a = False
b = True

# Since `a` is False, `b` is not evaluated
result = a and b
print(result)  # Output: False

Example of Short-Circuiting with or:

a = True
b = False

# Since `a` is True, `b` is not evaluated
result = a or b
print(result)  # Output: True

7. Practical Examples with Logical Operators

Example 1: Checking Multiple Conditions

You can use logical operators to check multiple conditions at once. For instance, you can check if a number is within a certain range.

x = 15

# Check if x is between 10 and 20
if (x > 10) and (x < 20):
    print("x is between 10 and 20")
else:
    print("x is not in the range")

Output:

x is between 10 and 20

Example 2: Combining not, and, and or

You can combine multiple logical operators for complex conditions. In this example, we check for multiple conditions while negating some results.

age = 18
is_student = True

# Check if the person is a student or an adult, but not both
if (age >= 18) and not is_student:
    print("You are an adult but not a student.")
elif (age < 18) or is_student:
    print("You are either underage or a student.")

Output:

You are either underage or a student.

Example 3: Input Validation

Logical operators are often used to validate multiple inputs or conditions in MicroPython projects.

temperature = 25
humidity = 65

# Check if temperature is in a safe range and humidity is not too high
if (temperature > 20 and temperature < 30) and (humidity < 70):
    print("The environment is optimal.")
else:
    print("The environment is not suitable.")

Output:

The environment is optimal.

Conclusion

Logical operators in MicroPython (and, or, not) are powerful tools for building complex conditions and decision-making logic. You can use them to combine multiple conditions, validate input, control flow, and implement efficient algorithms. Key points to remember:

and: Returns True if both conditions are True.
or: Returns True if at least one condition is True.
not: Negates the boolean value of a condition.
With logical operators, you can make your code more flexible and intelligent, especially when dealing with multiple conditions in if statements or loops.

You may also like

Leave a Comment