Loading...

Python Logical Operators

and is True only when both answers are True. The previous page asked one question at a time. This page joins two of those answers. print(5 > 2 and 3 > 1) writes True, because 5 is larger than 2 and 3 is larger than 1. print(5 > 2 and 3 > 9) writes False. The first answer is True, but the second is False, so and is False. print(5 > 2 or 3 > 9) writes True. or needs only one True answer. The result is still True or False, not the number 5 and not the number 3.

Both sides, or one side

python
print(5 > 2 and 3 > 1)
print(5 > 2 and 3 > 9)
print(5 > 2 or 3 > 9)

The three lines are True, then False, then True.

not flips the answer

print(not True) writes False. print(not False) writes True. print(not 5 > 2) writes False. Python asks 5 > 2 first, gets True, then not flips that True into False. not does not store a new number. It only flips the answer you already have.

Flip True and False

python
print(not True)
print(not False)
print(not 5 > 2)

The three lines are False, then True, then False.

and is decided before or

print(5 > 2 or 3 > 9 and 1 > 4) writes True. and is decided before or, so the question is 5 > 2, or the pair 3 > 9 and 1 > 4. Both 3 > 9 and 1 > 4 are False, so that pair is False. Then True or False is True. print((5 > 2 or 3 > 9) and 1 > 4) writes False. The parentheses make or happen first. True or False is True, then True and False is False. Parentheses choose which question comes first. They are not a second kind of and.

and before or, then parentheses

python
print(5 > 2 or 3 > 9 and 1 > 4)
print((5 > 2 or 3 > 9) and 1 > 4)

The first line is True. The second line is False.

Three words, and when each one is True.

📊 When the answer is True

Word When the answer is True
and Both sides are True
or At least one side is True
not The side was False

The next page chooses a path

This page only joins answers. It does not add numbers, and it does not choose which lines run. The next page uses these True and False answers to pick a path, with if and else.

Say these yourself

  1. and is True only when both sides are True. 5 > 2 and 3 > 9 is False.
  2. or is True when one side is True. 5 > 2 or 3 > 9 is True.
  3. not flips the answer. not True is False. not 5 > 2 is False.
  4. and is decided before or.
  5. (5 > 2 or 3 > 9) and 1 > 4 is False.
  6. The next page chooses a path with if and else.

Easy to misread

  • Reading and as "one side is enough"
  • Reading or as "both sides must be True"
  • Expecting the number 5 instead of True or False
  • Writing AND, OR, or NOT in capitals. The program stops. The words are and, or, and not
  • Leaving out parentheses and letting and run first

Keep

and is True only when both answers are True.
or is True when at least one answer is True.
not flips True into False, and False into True.
and is decided before or. Parentheses pick the question you meant.
The answer is still True or False, not the numbers.
The next page chooses a path with if and else.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top