"x is between 0 and 100" — how would you write that as a single condition?
Two checks: x >= 0 and x <= 100. Both have to be true.
Python writes that with and:
if x >= 0 and x <= 100:
print("in range")Three logical operators total: and, or, not. Lowercase words, not symbols like && or || — Python prefers English where it can.
And or is "either one"?
Right. True or False is True. False or False is False. The OR is inclusive — True or True is also True.
And not?
Flips. not True is False, not False is True. Often used for clarity: if not is_logged_in: reads better than if is_logged_in == False:.
Precedence — does and bind tighter than or?
Yes. not first, then and, then or. So True or False and False is True or (False and False) is True or False is True. When in doubt, parens — never wrong, often clearer.
and / or / not — combining booleansPython uses English words, not symbols:
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | both true | True and True | True |
or | at least one true | True or False | True |
not | flip | not True | False |
From tightest to loosest: not → and → or. So:
False or True and False
# evaluates as:
False or (True and False)
# → False or False
# → FalseWhen mixing and and or, use parentheses. The reader shouldn't have to remember precedence.
For the very common "x is in a range" case, Python supports a shortcut:
if 0 <= x <= 100:
...That's exactly equivalent to 0 <= x and x <= 100, but reads more like maths. Use whichever feels clearer in context.
and stops as soon as it sees False; or stops as soon as it sees True. The right-hand side isn't evaluated when the result is already determined:
if data and len(data) > 0: # `len(data)` only runs if `data` is truthy
...Useful when the second check would error on the first being absent — guard with and first.
if x == 1 or 2: # always True — bug!x == 1 or 2 parses as (x == 1) or 2. The 2 on its own is truthy (non-zero), so the whole expression is always true. The fix: if x == 1 or x == 2: — both sides need their own comparison.
"x is between 0 and 100" — how would you write that as a single condition?
Two checks: x >= 0 and x <= 100. Both have to be true.
Python writes that with and:
if x >= 0 and x <= 100:
print("in range")Three logical operators total: and, or, not. Lowercase words, not symbols like && or || — Python prefers English where it can.
And or is "either one"?
Right. True or False is True. False or False is False. The OR is inclusive — True or True is also True.
And not?
Flips. not True is False, not False is True. Often used for clarity: if not is_logged_in: reads better than if is_logged_in == False:.
Precedence — does and bind tighter than or?
Yes. not first, then and, then or. So True or False and False is True or (False and False) is True or False is True. When in doubt, parens — never wrong, often clearer.
and / or / not — combining booleansPython uses English words, not symbols:
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | both true | True and True | True |
or | at least one true | True or False | True |
not | flip | not True | False |
From tightest to loosest: not → and → or. So:
False or True and False
# evaluates as:
False or (True and False)
# → False or False
# → FalseWhen mixing and and or, use parentheses. The reader shouldn't have to remember precedence.
For the very common "x is in a range" case, Python supports a shortcut:
if 0 <= x <= 100:
...That's exactly equivalent to 0 <= x and x <= 100, but reads more like maths. Use whichever feels clearer in context.
and stops as soon as it sees False; or stops as soon as it sees True. The right-hand side isn't evaluated when the result is already determined:
if data and len(data) > 0: # `len(data)` only runs if `data` is truthy
...Useful when the second check would error on the first being absent — guard with and first.
if x == 1 or 2: # always True — bug!x == 1 or 2 parses as (x == 1) or 2. The 2 on its own is truthy (non-zero), so the whole expression is always true. The fix: if x == 1 or x == 2: — both sides need their own comparison.
Create a free account to get started. Paid plans unlock all tracks.