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:

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

32 == 32

!=

Inequality

30 != 32

>

Greater than

60 > 32

>=

Greater than or equal

60 >= 32, 32 >= 32

<

Less than

20 < 32

<=

Less than or equal

20 < 32, 32 <= 32

⚠️ 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

and

4 > 0 and 2 < 4

Evaluates to True if both conditions are true; otherwise evaluates to False.

or

4 > 0 or 2 > 4

Evaluates to True if either condition is true; evaluates to False only if both are false.

not

not (5 == 0)

Evaluates to True if condition is false; evaluates to False if condition is true.

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:

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

🧠 Check Your Understanding

Which of the following function calls would return True?

➡️ Next up: Exercise: Logical expressions