Book Free Trial

Curriculum

Logical Operators

Subject: ProgrammingCourse: Coding In PythonAges: Primary, Junior

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

OperatorMeaningReturns True if…Example
andLogical ANDboth operands are Truea > 0 and b > 0
orLogical ORat least one operand is Truea > 0 or b > 0
notLogical NOT (negation)the operand is Falsenot (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:

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
<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: and stops if the first operand is False, or stops if the first operand is True.
  • not flips the boolean value: not TrueFalse, not FalseTrue.

Quick practice

  1. Check if both entered numbers are positive.
  2. Check if at least one of the numbers is divisible by 5.
  3. 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))

Key takeaways: and requires all conditions to be true, or requires at least one, and not reverses the truth value. Combine them to build powerful logical expressions.