Hintor
Python Basics for Programmers

Booleans, None, and Comparisons

Python for Developers/Python Basics for Programmers/

Booleans, None, and Comparisons

Python

Lesson explanation

Booleans, None, and Comparisons


🎯 Learning Objective

After this lesson, you will be able to:

  • Use Python's True, False, and None values correctly.
  • Compare values with equality, ordering, and chained comparisons.
  • Check optional values with is None and is not None.
  • Combine boolean expressions with and, or, and not.

Short Explanation

Python boolean values are True and False with capital letters. Comparisons such as ==, !=, <, <=, >, and >= produce boolean results that can be stored, printed, or later used in conditions.

Python also supports chained comparisons:

Python
minimum <= value <= maximum

This reads naturally and evaluates whether the value satisfies both boundaries.

None represents the absence of a value. Use is None or is not None for this special value rather than treating it like an ordinary string or number.

Logical operators are words in Python: and, or, and not. Comparisons run before not, then and, then or. Parentheses are still useful when a business rule contains several parts.

💡 Tip: Use == to compare ordinary values. Reserve is for identity checks, especially None.


Clean Code Examples

1. Produce boolean results with comparisons

Each comparison evaluates to True or False.

Python
status = "ready"
attempts = 2

print(status == "ready")
print(attempts < 3)

Output:

TEXT
True
True

2. Check an optional value

Use identity checks when a value may be missing.

Python
last_error = None

print(last_error is None)
print(last_error is not None)

Output:

TEXT
True
False

3. Combine a policy with logical operators

Every required part of this policy must be true.

Python
role = "editor"
account_active = True

can_publish = role == "editor" and account_active
print(can_publish)

Output:

TEXT
True

4. Check an inclusive range

A chained comparison can express both boundaries clearly.

Python
latency_ms = 180

print(100 <= latency_ms <= 250)
print(not latency_ms > 500)

Output:

TEXT
True
True


⚠️ Common Mistakes

  1. Do not write true, false, null, &&, ||, or ! in Python.
  2. Do not use assignment = when you need equality comparison ==.
  3. Do not write value == None as the preferred missing-value check.
  4. Do not write status == "failed" or "queued"; each side of or must be a complete boolean expression.
Start Practice

Python for Developers

Booleans, None, and Comparisons

5 tasks
Lesson progress0 / 5 tasks completed

Tasks

Practice progress

Check whether a sync time is missing

View task

Complete the preferred missing-value check.

Requirements

  • Fill only the comparison phrase.
  • Keep the existing variable unchanged.
  • Print exactly the expected output.

Expected Output

TEXT
Sync time missing: True

Expected output

Sync time missing: True
Code: PythonTask 1

Check whether a sync time is missing

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 5 tasks completed

Previous lessonBack to unitBack to track
GoalPython
Equalitya == b
Inequalitya != b
Inclusive rangeminimum <= value <= maximum
Missing valuevalue is None
Present valuevalue is not None
Both rules must passrule_a and rule_b
Either rule may passrule_a or rule_b
Reverse a boolean resultnot rule