On this page

Boolean Logic

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

What Boolean Logic Is​

Boolean logic is the foundation of all decision‑making in programming. A Boolean value represents one of two states: True or False. These values allow a program to evaluate conditions, choose between alternatives, and control the flow of execution.

Python follows classical Boolean algebra, which is used in mathematics, logic, and computer science. Understanding Boolean logic is essential before learning conditions, loops, and more advanced control‑flow structures.

Boolean Values in Python​

Python includes two Boolean constants:


True

False

They are written with capital letters and represent truth values. Any expression that compares values or evaluates a condition produces a Boolean result.


print(5 > 3)     # True

print(2 == 7)    # False

Comparison Expressions​

Comparison operators evaluate relationships between values and return Boolean results.


x = 10



print(x == 10)   # Equal

print(x != 5)    # Not equal

print(x > 7)     # Greater than

print(x < 3)     # Less than

print(x >= 10)   # Greater or equal

print(x <= 9)    # Less or equal

These expressions are the basis for conditional logic.

Logical Operators​

Logical operators combine Boolean values to form more complex expressions.

and​

Returns True only if both expressions are True.


is_member = True

has_paid = False



print(is_member and has_paid)   # False

or​

Returns True if at least one expression is True.


print(is_member or has_paid)    # True

not​

Reverses a Boolean value.


print(not is_member)            # False

Logical operators allow you to express multi‑part conditions clearly and precisely.

Truthiness and Falsiness​

In Python, some non‑Boolean values behave like True or False when evaluated in a Boolean context. This behavior is known as truthiness.

Values considered False in Boolean contexts include:

All other values are considered True.


if "":

    print("This will not run")



if "Python":

    print("This will run")

Understanding truthiness is important when writing conditions that check for empty or missing data.

Boolean Expressions in Practice​

Boolean expressions appear everywhere in control flow. For example:


age = 20

has_id = True



can_enter = age >= 18 and has_id

print(can_enter)

The variable can_enter will be True only if both conditions are satisfied.

Why Boolean Logic Matters​

Boolean logic determines how a program makes decisions. Every if statement, every loop condition, and every branching structure depends on Boolean expressions. Mastering Boolean logic ensures that you can write clear, correct, and predictable control‑flow structures throughout the rest of this module and the entire course.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Control Flow

Next

If/Else Statements