Logical expressions
In the code and functions that we've seen so far, all the lines of code are executed. But often in programming, we only want to execute code if some condition is true, and otherwise do nothing or execute some other bit of code. The addition of logic into our programs allows us to build much more powerful programs, programs that can actually make decisions. To add logic, we must learn:
Logical expressions that evaluate to either true or false.
Conditionals (if statements) that specify which code to execute when something is true.
Booleans
A Boolean value is a type of data that is either True
or False
, and is used frequently in computer science.
As a user, you probably see Booleans all the time. For example, Google Maps uses a Boolean to decide whether to avoid highways in driving directions. (I always avoid them!)
avoid_highways = True
Twitter uses a Boolean to remember where the user allows personalized ads. (I hate ads!)
personalized_ads = False
A logical expression is any expression that evaluates to a Boolean value. Logical expressions use comparison operators, logical operators, or a mix of both.
This expression uses a comparison operator, the greater-than operator:
passed_class = grade > 65
The next expression uses a logical operator, the or operator:
wear_jacket = is_raining or is_windy
Comparison operators
This table shows all the comparison operators and examples of true expressions using each:
Operator |
Meaning |
True expressions |
---|---|---|
|
Equality |
|
|
Inequality |
|
|
Greater than |
|
|
Greater than or equal |
|
|
Less than |
|
|
Less than or equal |
|
⚠️ Beware of a very common mistake: confusing ==
(equality operator) with =
(assignment operator). It's particularly easy to make this mistake since some programming languages do use =
as their equality operator. In Python, at least, they are two very different operators, so you need to make sure to use ==
when you want to check that one value equals another value.
Logical operators
This table describes the logical operators:
Operator |
True expressions |
Meaning |
---|---|---|
|
|
Evaluates to |
|
|
Evaluates to |
|
|
Evaluates to |
One thing to note about or
is that it's true even when both conditions are true, which might be different from how we sometimes use "or" in natural language.
There is actually a bit more complexity to and
/or
that we'll get into later when we discuss "truthiness" and "falsiness", but for now, as long as each expression is True
or False
, the description above is correct.
Compound Boolean expressions
We can mix and match any of the above operators into compound Boolean expression (versus a simple expression that uses only one operator).
may_have_mobility_issues = (age >= 0 and age < 2) or age > 90
That expression is true if either:
it's true that both age is greater than or equal to 0 and age is less than 2
it's true that age is greater than 90
Basically, it's true if someone's a toddler or an elder! (People that are less than 0 years old are either non-existent or happily snuggled in a warm womb 🤰🏽).
Boolean expressions in functions
To make a function that returns a Boolean, use a Boolean expression in the return statement. The simplest functions like this just return a Boolean based on the values of the parameters.
def passed_class(grade):
return grade > 65
Then we'd call the function like so:
passed_class(0) # Returns: False
passed_class(65) # Returns: False
passed_class(66) # Returns: True
Now consider this example:
def should_wear_jacket(is_rainy, is_windy):
return is_rainy or is_windy