- Courses
- đ»Programming Languages
- đPython
- Control Flow
- Boolean Logic
On this page
Boolean Logic
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:
00.0""(empty string)[](empty list){}(empty dictionary)None
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!
Previous
Control Flow
Next
If/Else Statements
-
What Boolean Logic Is
-
Boolean Values in Python
-
Comparison Expressions
-
Logical Operators
andornot
-
Truthiness and Falsiness
-
Boolean Expressions in Practice
-
Why Boolean Logic Matters
