A tutorial for new coders
From the Urban Outfitter’s website to the mobile menu at Chipotle, Boolean logic is, well, everywhere.
If you’ve ever shopped online, or performed a Google search, odds are that you’ve used Boolean logic, and already know how to use some of its operators. This is a quick tutorial to sharpen your coding skills using Booleans and comparison operators.
What’s Boolean logic?
In coding, Boolean logic boils down to the idea that every statement is either ‘True’ or ‘False’.
We use Boolean operators — symbols you might recognize from algebra class — to pose questions in the form of Boolean expressions. These expressions ask: “does ‘x’ have [this relationship] to ‘y’?’
When we enter these expressions into our IDE, Python returns their values as either ‘True’ or ‘False’. (Note: Boolean operators are also known as logical operators.)
In Python, especially in while loops or other conditional statements, we can use Boolean expressions and comparison operators to evaluate whether or not a condition is met. When the condition is met, the loop terminates. This is shown in the example below:
#Example of a Boolean expression in a while loopagain = 'y'
while again == 'y':
for num in range(5):
again = input("Do you want to run this again? (y or n)") #This loop repeats until the user enters 'n' (which changes the value of the expression again == 'y' to 'False')
Common Comparison Operators Used in Boolean Expressions
< = Less than or equal to
Example) a <= b This expression asks: "is 'a' less than or equal to 'b'?" Output: If a is less than or equal to b, this expression returns 'True'; otherwise, it returns 'False'.
> = Greater than or equal to
Example) a >= b This expression asks: "is 'a' greater than or equal to 'b'?" Output: If a is greater than or equal to b, this expression returns 'True'; otherwise, it returns 'False'.
== Equal to
Example) a == b This expression asks: "is 'a' equal to 'b'?" Output: If a equals b, this expression returns 'True'. NOTE: '=' is an assignment operator (a = b changes the value of a) In contrast, '==' is a Boolean operator (asks if a and b are equal)
!= Not equal to
Example) a != b This expression asks: "is 'a' NOT equal to 'b'?"This one is a little counterintuitive -- it's posing the opposite question of the == operator. Output: If a IS equal to b, the statement 'a != b' returns FALSE. If a IS NOT equal to b, the statement 'a != b' returns TRUE.
Example) a < b and a != c This expression asks: "is 'a' less than 'b' AND is 'a' not equal to 'c'? Output: In order for this expression to return 'True', BOTH parts of this expression must be true. If the first part of the expression is 'False', the second part is not processed. For example: if a < b is true, but a != c is NOT true, then the conditions of this expression are not met, and the statement returns 'False'
Example) a < b or a < c This expression asks: "is 'a' less than 'b', OR is 'a' not equal to 'c'? Output: Unlike the 'and' operator, which requires that both parts of an expression be true to return 'True', the 'or' operator only requires one component of the expression to be true to return 'True'. For example: if a is greater than b, but a is less than c, this expression will still return 'True'.
Example) not a < b This expression asks: "is 'a' < 'b' NOT true? Output: If 'a' < 'b' is true, then this expression returns False. If 'a' < 'b' is false, then this expression returns True.
Just like mathematical operators have an order of operations (remember PEMDAS?), comparison and Boolean operators must be interpreted in a specific order. You can see the full list of operator precedence here.
The general rule of thumb: comparison operators come first (in no particular order), followed by the Boolean operators.
*** Operator Precedence *** 1) (These all have the same precedence): <, <=, >, >=, !=, == 2) 'not' 3) 'and' 4) 'or'
For example, let’s say we have the following statement:
a <= b and c <= b or a != c
If we assign the variables the following values, what will this statement return?
a = 1 b = 4 c = 7 This means our statement now looks like this: 1 <= 4 and 7 <= 4 or 1 != 7
Due to operations precedence, we need to break this statement into two sections:
1) 1 <= 4 and 7 <= 4 This expression is false. Although 1 is less than 4, 7 is not less than 4. Since this is an expression using ‘and’, both parts need to be true for the expression to be true. 2) 1 != 7 This expression is true. Since these expressions are connected with an 'or', only one of them needs to be true for the entire statement to be true. Therefore, the statement 1 <= 4 and 7 <= 4 or 1 != 7 is True.
Whether you want to up your Google search game, or if you want to write conditional statements in Python, play around! Experiment with Boolean and comparison operators. (And happy coding!)
If you want to learn more about Python Booleans, check out these resources: