HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Booleans (True / False)

Booleans (True / False)

True/false values, comparisons producing a bool, and the "empty/full" truthiness rules.

Simple enough for a 5-year-old to understand

True and False

A boolean takes only two values: True and False. Their first letters are capitalized.

Example code
1
isOpen = True
True.
VariableTrue.
2
print(isOpen)
Writes True.
Output / InputWrites True.
3
print(10 > 3)
A comparison produces a bool: True
ConditionA comparison produces a bool: True
4
print(10 == 3)
False
ConditionFalse
VariableOutput / InputCondition
💡 Hint: true/false (lowercase) causes an ERROR; in Python, True/False start with an uppercase letter.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.What does bool("") (empty text) return?

About this topic

A boolean value can only be two things: True or False. It looks simple, but every decision in the program relies on it — every if condition ultimately boils down to a boolean value. That's why knowing which values Python considers true and which ones it considers false is the foundation of writing conditions.

In Python, not only True and False, but every value can be evaluated in a boolean context. The number zero, empty text, empty list, and None are considered false; everything else is considered true. This feature shortens code, but if you're not aware of it, it leads to unexpected behavior.

What you will learn

  • True and False values and the bool type
  • Which values are considered false
  • The and, or, and not operators
  • What short-circuit evaluation means
  • The difference between is and double equals

When to use?

You use boolean values to flag a state: whether the user is logged in, whether the operation is complete, whether a setting is turned on. Functions also answer the question "did it succeed?" by returning a boolean value.

You take advantage of Python's flexibility in checking for emptiness: checking whether a list is full by writing the list directly into the condition rather than comparing its length is both concise and idiomatic.

Common mistakes

Wrong

if is_active == True:
    print("open")

Comparing a value that is already boolean with True is unnecessary and reduces readability. Writing the value directly in the condition is sufficient; the Python style guide recommends this as well.

Correct

if is_active:
    print("open")

Wrong

a = 1000
b = 1000
print(a is b)

The is operator does not compare values, but rather whether two names point to the SAME object. With small numbers, Python uses caching so it might return True, but with large numbers, it returns False — the result is unpredictable. Double equals is used for value comparison.

Correct

a = 1000
b = 1000
print(a == b)

Wrong

if value == None:
    print("empty")

Comparison with None should be done using is rather than double equals. Double equals works in most cases, but if a class has modified its equality behavior, it might give the wrong result; since is checks identity, it is always correct.

Correct

if value is None:
    print("empty")

Frequently asked questions

What is the difference between is and double equals?

Double equals compares VALUES: if two different lists contain the same elements, they are equal. is compares IDENTITY: it checks if they are the exact same object in memory. Rule of thumb: use is for comparisons with None, True, and False, and double equals for everything else.

What is short-circuit evaluation?

When the first false is found in an and chain, or the first true in an or chain, Python doesn't run the rest at all. This is used for safety: if you check whether a list is empty and then access its first element, if the list is empty, the second part never runs and you don't get an error.

Why do True and False start with uppercase letters?

In Python, these are built-in constant names and are written with capital letters. If you write true or false, Python thinks they are undefined variables and raises a NameError. This is the most common mistake made by people coming from JavaScript and the C family.

Summary

Every condition ultimately boils down to a boolean value; in Python, zero, empty sequences, and None are considered false, and everything else is true. The most critical distinction is between is and double equals: one compares identity, the other compares value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code