PYTHON BASICS
Python Basics: Relational Operations
Boolean operators — Part 1
Last article we took a look at using arithmetic operations to evaluate math expressions. For example, the Fahrenheit to Celsius conversion formula in Python can be expressed as y = (x-32) * 5 / 9 where x is the temperature in Celsius, and y evaluates to the Fahrenheit equivalent.
Every arithmetic operations always evaluates to a numeric data type. But sometimes we may only want to know if the result fulfills a certain condition. Perhaps we may want to determine if a number is positive, or if it’s an even number. Maybe the number represents someone’s age and we want to determine if they are an adult. In these cases, we don’t really care about the exact value. We just want to know if the value satisfies a rule or condition.
Let’s assume I live in a city that has unstable weather patterns, and I’m contemplating whether to wear a sweater outdoors. I’d like to know the temperature outside: cold or warm. And I only wear sweaters when the temperature is less than 0°C.
A Boolean is a data type that represents two types of values: True or False.
Booleans can help me to decide on the outfit I should wear outdoors. A boolean is a data type that represents two types of values: True or False. In the weather example, I could have a variable, wear_sweater, that evaluates to True when it’s cold outside, and False otherwise. Depending on the value of wear_sweater, I can decide to either wear a sweater or select another attire.
Get Gideon Oludeyi’s stories in your inbox
Join Medium for free to get updates from this writer.
To determine whether it’s cold outside, I need to compare the current temperature with the threshold of 0°C. Relational operations, aka comparison operations, are part of a set of operations that evaluate to either a True or False value. There are six different relational operators in Python.
Relational Operators
Operator Name
---------- --------------------------
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal toEqual to (==)
The == operator tests whether two values are equal to each other.
1 == 1 # True
1 == 2 # FalseNot equal to (!=)
The != operator is the opposite of == — equal to. It tests whether two values are NOT equal to each other.
1 != 1 # False
1 != 2 # TrueGreater than (>)
The > operator tests whether the left operand is strictly greater than the right operand.
5 > 3 # True
3 > 5 # False
5 > 5 # FalseLess than (<)
The < operator tests whether the left operand is strictly less than the right operand.
4 < 2 # False
2 < 4 # True
4 < 4 # FalseGreater than or equal to (>=)
The >= operator is the opposite of < — less than. It tests whether the left operand is greater than or equal to the right operand.
4 >= 2 # True
2 >= 4 # False
4 >= 4 # TrueLess than or equal to (<=)
The <= operator is the opposite of > — greater than. It tests whether the left operand is less than or equal to the right operand.
5 <= 3 # False
3 <= 5 # True
5 <= 5 # TrueBack to our example
The < operator is most relevant for this example. I can use it to determine whether the current temperature is less than 0°C.
In this snippet, the wear_sweater evaluates to True. Regardless of the temperature outside, as long as it is below 0°C, I’m going to wear my sweater. On the other hand, if celsius was 15, the wear_sweater would have evaluated to False, in which case I would not wear my sweater outdoors.
Note: I could have used the
>operator as well. I’d just need to swap the left and right operandswear_sweater = 0 > celsius. But I feel the<operator is clearer to understand.
Suppose the current temperature was given in Fahrenheit, and I wanted to know if I should wear my sweater outdoors while using the 0°C threshold. I could convert the Fahrenheit temperature to Celsius using the formula from the last article, then compare the result with 0°C.
We can merge the computations, in which case the arithmetic operations will take precedence over relational operations:
wear_sweater = (fahrenheit-32) * 5 / 9 < 0The beauty of this is that we don’t need to know the exact value of the current temperature to make a decision of whether to wear a sweater. The temperature could be -20°C or -200°C, the program will always ensure that I wear my sweater at the right temperature. Although there’d be much bigger concerns at -200°C 🥶 than what sweater to wear .
Conclusion
In this article, we learned how to compare two numbers to each other. But we’ve only scratched the surface of what you can do with Booleans. The true power of Booleans lie in Conditional statements: a programming concept that allows a program to make decisions based on certain conditions.
But before we dive deeper into conditional statements, I’d like to spend some more time familiarizing ourselves with another set of Boolean operations called Logical operations.





