When learning to code in Python, one of the first things you’ll encounter is a concept called booleans. These are values that help a program decide what to do next. Should it run a certain block of code? Is a condition true or false? Booleans make that possible. This guide breaks down booleans in Python, starting from the basics to uses, with simple explanations and clear examples that are easy to follow.
A boolean is a data type in Python that has only two possible values:
These values aren’t strings or variables — they’re actual built-in constants in Python. They’re used in conditions and logic statements to control how your program flows.
You can confirm that True and False are boolean types:
print(type(True)) #
print(type(False)) #
They belong to Python’s bool type, which is short for boolean.
Booleans help control logic in a program. Any decision — like checking if a user is old enough or if a number is even — will involve booleans. Without booleans, your program would have no way of knowing what to do in response to conditions. Booleans power:
A boolean expression is any statement that evaluates to True or False. In Python, these usually come from comparison operators.
print(10 == 10) # True
print(5 != 3) # True
print(7 > 9) # False
print(3 <= 3) # True
Here’s what the operators mean:
Operator | Meaning |
---|---|
== | Equals |
!= | Not equals |
> | Greater than |
< | Less than |
>= | Greater or equal |
<= | Less or equal |
Every comparison returns a boolean value.
You can also combine conditions using boolean operators. Python has three main ones:
Both sides must be True for the result to be True.
print(True and True) # True
print(True and False) # False
Only one side needs to be True.
print(False or True) # True
print(False or False) # False
Reverses a boolean value.
print(not True) # False
print(not False) # True
Not everything in Python is strictly True or False. Some values act as true or false in conditional contexts.
Everything else is considered truthy.
if "":
print("Truthy")
else:
print("Falsy") # This will print
Empty strings and containers are treated as False by default.
Booleans come into play most commonly inside if statements.
temperature = 35
if temperature > 30:
print("It's a hot day")
else:
print("It's not that hot")
Here, temperature > 30 evaluates to True, so the first block runs.
You can also use multiple conditions with elif.
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B") # This will print
else:
print("Grade: C")
Each condition returns a boolean, and the first True one gets executed.
Booleans also control loop behavior, especially while loops.
counter = 0
while counter < 5:
print("Counter is", counter)
counter += 1
The loop continues while the boolean expression counter < 5 remains True.
You can write functions that return True or False based on logic.
def is_even(number):
return number % 2 == 0
print(is_even(6)) # True
print(is_even(7)) # False
It is useful when validating inputs or checking conditions in your code.
You can combine multiple boolean expressions using and or or.
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
else:
print("Access denied")
You can also group logic using parentheses for clarity:
if (age > 18 and has_id) or age > 65:
print("Allowed by rule")
Python is smart. It stops evaluating expressions as soon as the outcome is known. It is called short-circuiting.
def expensive_check():
print("Function ran")
return True
print(False and expensive_check()) # Only False is printed
Since False and anything is always False, Python skips calling the function.
Boolean variables can be used as flags to track states in your program.
download_complete = False
def complete_download():
global download_complete
download_complete = True
complete_download()
if download_complete:
print("You can now open the file")
Flags are helpful in games, web apps, and system monitoring.
Python supports inline conditional expressions, also called ternary operators.
result = value_if_true if condition else value_if_false
age = 17
status = "Adult" if age >= 18 else "Minor"
print(status) # Minor
It’s great for writing short decisions.
You can even filter data using booleans. Here’s an example using list comprehension:
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers) # [2, 4, 6]
The expression n % 2 == 0 returns True for even numbers only.
Here are a few things to avoid:
value = 5
if value = 5: # SyntaxError (should be ==)
print("Match")
In Python, booleans are the foundation of logical thinking and decision-making. They allow your programs to evaluate conditions, control the flow of code, and respond dynamically to user input or data. Whether you're comparing values, looping through items, or managing system states, boolean values like True and False play a vital role. Mastering booleans helps you write more precise, readable, and efficient code. From simple conditionals to advanced logic, they appear in nearly every Python project. With a solid grasp of boolean logic, you’re now better equipped to build smarter and more interactive applications.
By Tessa Rodriguez / Apr 10, 2025
Discover how to use booleans in Python for writing conditions, managing logic, and building real-world applications.
By Tessa Rodriguez / Apr 17, 2025
AI output depends on temperature settings to determine both text creativity and random generation ability.
By Alison Perry / Apr 10, 2025
Easily build and deploy LLM agents using CrewAI's no-code tools. No coding is needed—just templates, inputs, and automation.
By Alison Perry / Apr 09, 2025
Using Microsoft Azure, 365, and Power Platform for corporate advancement and productivity, accelerate GenAI in your company
By Tessa Rodriguez / Apr 14, 2025
Learn 4 smart ways to generate passive income using GenAI tools like ChatGPT, Midjourney, and Synthesia—no coding needed!
By Tessa Rodriguez / Apr 11, 2025
ChatGPT for Amazon sellers helps optimize listings, streamline customer service, and improve overall workflow. Learn how this AI tool supports smarter business growth
By Tessa Rodriguez / Apr 16, 2025
Master SQL queries by learning how to read, write, and structure them step-by-step with clear syntax and query flow.
By Alison Perry / Apr 13, 2025
Discover how Python’s pop() method removes and returns elements from lists and dictionaries with built-in error handling.
By Alison Perry / Apr 09, 2025
Learn how to use AI presentation generators to create impactful, time-saving slides and enhance presentation delivery easily
By Alison Perry / Apr 17, 2025
The surge of small language models in the market, as well as their financial efficiency and specialty functions that make them perfectly suited for present-day AI applications
By Tessa Rodriguez / Apr 08, 2025
Real companies are using AI to save time, reduce errors, and boost daily productivity with smarter tools and systems.
By Alison Perry / Apr 15, 2025
ideas behind graph databases, building blocks of graph databases, main models of graph databases