Module 1

Variables

4 Topics
Module 2

Data Structures

3 Topics
Module 3

Module 4

Documentation

2 Topics | 1 Quiz
Errors and Exceptions

2 Topics | 1 Quiz
Module 5

In Python, a compound Boolean expression combines multiple Boolean values or expressions using logical operators. The three logical operators used in Python are **“and”**, **“or”**, and **“not”**.

The **“and”** operator returns True if both the expressions on either side of the operator are True. Otherwise, it returns False. In other words, the whole expression is true only if each individual part of the expression is true. Consider the example below:

` ````
```if temperature > 70 and weather == "sunny":
print("Go to the beach 🏖️")

In the above example, the compound expression will only be true if the temperature is greater than 70 and the weather is sunny; if either one of these individual expressions is false, the whole expression becomes false.

The **“or”** operator returns True if at least one of the expressions on either side of the operator is True. Otherwise, it returns False. In other words, the whole expression is true if just one individual part of the expression is true. Consider the example below:

` ````
```if temperature < 50 or weather == "rainy":
print("It's not nice outside, you should stay in")

In the above example, the compound expression will be true if the temperature is less than 50 or the weather is rainy; if only one of these individual expressions is true, it makes the whole expression false. For instance, if the temperature is 80 degrees but the weather is rainy, the expression would be true because one part of it is true.

The **“not”** operator is a unary operator that returns the opposite of the Boolean value of the expression it precedes. For example, *not True* would evaluate to *False*, while *not False* would evaluate to *True*. Consider the example below:

` ````
```rainy = False
if not rainy:
print("Play Ball!")
else:
print("Rain delay")

The following table may help make sense of the outcomes of various compound Boolean expressions:

Login

Accessing this course requires a login. Please enter your credentials below!

Loading...