
Python
A boolean is a data type with only two possible values: True or False. Booleans are essential in programming because they allow you to make decisions and control what code runs. The None type represents "nothing" or "no value" and is used to indicate the absence of data. Together, booleans and None form the foundation of decision-making and control flow in Python.
A boolean is simply True or False — there's nothing complicated about it. You create booleans by assigning the values True or False directly to variables, or by using comparison operators that return boolean results. True and False are Python keywords (capitalized) that represent the two boolean states.
# Direct boolean values
is_student = True
is_adult = False
has_license = True
# Booleans from comparisons
age = 20
is_older_than_18 = age > 18 # True
is_exactly_20 = age == 20 # True
can_vote = age >= 18 # True
print(f"Student: {is_student}")
print(f"Older than 18: {is_older_than_18}")
print(type(True)) # <class 'bool'>Comparison operators compare two values and return a boolean: True if the comparison is true, False otherwise. These are fundamental for making decisions in your code. The main comparison operators are: == (equal), != (not equal), > (greater), < (less), >= (greater or equal), <= (less or equal).
# Equality and inequality
x = 10
y = 5
print(x == y) # False (not equal)
print(x != y) # True (not equal)
print(x > y) # True (10 is greater)
print(x < y) # False (10 is not less)
print(x >= 10) # True (10 equals 10)
print(x <= 5) # False (10 is not less/equal to 5)
# String comparisons
name1 = "Alice"
name2 = "Bob"
print(name1 == name2) # False
print(name1 != name2) # TrueBoolean logic lets you combine multiple conditions using AND (both must be true), OR (at least one must be true), and NOT (flip the value). These logical operators are crucial for expressing complex conditions in if statements and other control structures.
# AND - both conditions must be true
age = 20
has_license = True
can_drive = age >= 18 and has_license # True (both are true)
# OR - at least one condition must be true
is_weekend = True
is_holiday = False
can_relax = is_weekend or is_holiday # True (one is true)
# NOT - opposite value
is_raining = False
is_nice_weather = not is_raining # True
# Combining multiple
temp = 75
is_sunny = True
perfect_day = temp > 70 and is_sunny and not is_raining
print(perfect_day) # TrueNone represents "nothing" or "the absence of a value". It's used when you have no data yet, when a variable hasn't been initialized, or when a function doesn't return anything. None is a singleton object in Python, and there's only one None value. It's commonly used as a default value or placeholder.
# None as a placeholder
result = None
user_data = None
# Checking for None
if result is None:
print("No result yet")
if user_data is not None:
print("We have data")
else:
print("No data available")
# None as default
def get_value(should_return):
if should_return:
return 42
else:
return None # Explicitly return nothing
value = get_value(False)
print(value) # Output: Noneage = 22
has_experience = True
passed_interview = True
is_eligible = age >= 18 and has_experience and passed_interview
if is_eligible:
print("You're hired!")
else:
print("Thanks for applying!")username = "alice123"
is_valid = len(username) >= 3 and len(username) <= 20
if is_valid:
print("Username is valid")
else:
print("Username must be 3-20 characters")inventory = {
"apples": 10,
"oranges": 0,
"bananas": 5
}
has_apples = inventory["apples"] > 0
has_oranges = inventory["oranges"] > 0
print(f"Apples available: {has_apples}")
print(f"Oranges available: {has_oranges}")| Concept | Remember |
|---|---|
| True | Boolean value representing yes/on/correct |
| False | Boolean value representing no/off/incorrect |
| == | Equals (comparison) |
| != | Not equals |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
| and | Both must be true |
| or | At least one must be true |
| not | Opposite value |
| None | Represents nothing/no value |
Now you understand booleans and comparisons. Let's learn about type conversion — changing data between types!
Ready to practice? Try challenges or take the quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward