Next up, we're going to look at a different kind of operator - the logical operators.

Logical operators are used in expressions that combine or modify boolean values to produce a new boolean value.

In Python, there are three main logical operators: and, or, and not.

AND (and)

This operator only returns True if all the conditions it checks are true. It's used when your code requires every checked condition to be true to proceed.


print(True and True)  # Outputs True because both values are True
print(True and False)  # Outputs False because one value is False

Here, the first line returns True because both conditions are true. The second line returns False because one of the conditions is false.

OR (or)

This operator returns True if at least one of the conditions it checks is true. It's used when any condition being true is sufficient to proceed.


print(True or False)  # Outputs True because at least one value is True
print(False or False)  # Outputs False because both values are False

In this case, the first line returns True because at least one of the conditions is true. The second line returns False because both conditions are false.

NOT (not)

This operator inverts the truth value of the condition it checks. If the condition is true, it returns false, and if it's false, it returns true.


print(not True)  # Outputs False because 'not' inverts the value
print(not False)  # Outputs True because 'not' inverts the value

Not unlike the flip of a coin!