Theory
Logical Operators in Python
Sometimes you need to check multiple conditions at once. For example:
n % 2 == 0 checks if n is even.
To check if both n and m are even, combine conditions with the logical and:
n % 2 == 0 and m % 2 == 0
Logical operators in Python
| Operator | Meaning | Returns True if… | Example |
|---|---|---|---|
and | Logical AND | both operands are True | a > 0 and b > 0 |
or | Logical OR | at least one operand is True | a > 0 or b > 0 |
not | Logical NOT (negation) | the operand is False | not (a > 0) |
Examples
1) Both numbers are even
n = 4
m = 8
if n % 2 == 0 and m % 2 == 0:
print("Both are even")
<h3 style="margin:12px 0 6px; font-size:1.05rem;">2) At least one number ends with 0</h3>
<pre style="background:#f6f8fa; padding:12px; border-radius:8px; overflow:auto;"><code class="language-python">a = 25
b = 40 if a % 10 == 0 or b % 10 == 0: print("At least one ends with 0")
<h3 style="margin:12px 0 6px; font-size:1.05rem;">3) Positive and non-negative check</h3>
<pre style="background:#f6f8fa; padding:12px; border-radius:8px; overflow:auto;"><code class="language-python">a = 5
b = 0 if a > 0 and not (b < 0): print("a is positive and b is non-negative")
Equivalent without 'not'
if a > 0 and b >= 0: print("a is positive and b is non-negative")
Truth tables
and operator:
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
<p><strong>or</strong> operator:</p>
<table style="border-collapse: collapse; margin-bottom: 12px;">
<thead>
<tr style="background: #f0f0f0;">
<th style="padding:6px; border:1px solid #ccc;">A</th>
<th style="padding:6px; border:1px solid #ccc;">B</th>
<th style="padding:6px; border:1px solid #ccc;">A or B</th>
</tr>
</thead>
<tbody>
<tr><td style="padding:6px; border:1px solid #ccc;">True</td><td style="padding:6px; border:1px solid #ccc;">True</td><td style="padding:6px; border:1px solid #ccc;">True</td></tr>
<tr><td>True</td><td>False</td><td>True</td></tr>
<tr><td>False</td><td>True</td><td>True</td></tr>
<tr><td>False</td><td>False</td><td>False</td></tr>
</tbody>
</table>
<p><strong>not</strong> operator:</p>
<table style="border-collapse: collapse;">
<thead>
<tr style="background: #f0f0f0;">
<th style="padding:6px; border:1px solid #ccc;">A</th>
<th style="padding:6px; border:1px solid #ccc;">not A</th>
</tr>
</thead>
<tbody>
<tr><td style="padding:6px; border:1px solid #ccc;">True</td><td style="padding:6px; border:1px solid #ccc;">False</td></tr>
<tr><td>False</td><td>True</td></tr>
</tbody>
</table>
Tips
- Use parentheses
(...)to make complex logic easier to read. - Short-circuit evaluation:
andstops if the first operand isFalse,orstops if the first operand isTrue. notflips the boolean value:not True→False,not False→True.
Quick practice
- Check if both entered numbers are positive.
- Check if at least one of the numbers is divisible by 5.
- Check if a number is not between 10 and 20 (inclusive).
Show sample solutions
# 1) Both positive
a, b = map(int, input().split())
print(a > 0 and b > 0)
2) At least one divisible by 5
a, b = map(int, input().split())
print(a % 5 == 0 or b % 5 == 0)
3) Not between 10 and 20 inclusive
n = int(input())
print(not (10 <= n <= 20))