Python Booleans
Master True/False logic and conditional programming in Python
✅ What are Booleans?
Booleans are a Basic data type in Python that have two values:
True
or
False
. They are used in program for creating logical operations ,condtional statements and create logic flow in program. It is very essential for adding decisions in your programs.
# Boolean values in Python
is_active = True # A boolean True value
has_access = False # A boolean False value
# Boolean from comparison
age = 25
is_adult = age >= 18 # Evaluates to True
# Boolean in condition
if is_active:
print("User is active") # This will execute
Boolean Concepts
Basic Values
Only two possible values
is_sunny = True
is_raining = False
print(type(is_sunny)) #
Comparisons
Result from comparing values
age = 25
is_adult = age >= 18 # True
print(f"Is adult? {is_adult}")
Logical Operations
Combine multiple conditions
sunny = True
warm = False
nice_day = sunny and warm # False
Truthiness
Other values can be truthy/falsy
print(bool("hello")) # True
print(bool("")) # False
print(bool([1, 2])) # True
Basic Boolean Values
Python has two boolean literals: True and False
# Boolean literals
is_sunny = True
is_raining = False
print(f"Is it sunny? {is_sunny}") # True
print(f"Is it raining? {is_raining}") # False
print(f"Type of is_sunny: {type(is_sunny)}") #
# Boolean from simple comparisons
age = 25
is_adult = age >= 18
print(f"Is adult? {is_adult}") # True
# Boolean in conditions
if is_sunny:
print("Great day for a picnic!")
else:
print("Maybe stay indoors.")
Boolean from Comparisons
Most boolean values come from comparison operations
🔹 Numeric Comparisons
a = 10
b = 20
# Equal to
print(f"Is a equal to b? {a == b}") # False
# Not equal to
print(f"Is a not equal to b? {a != b}") # True
# Less than
print(f"Is a less than b? {a < b}") # True
# Greater than
print(f"Is a greater than b? {a > b}") # False
🔹 String Comparisons
name1 = "Alice"
name2 = "Bob"
# Compare if strings are equal
print(f"Is name1 equal to name2? {name1 == name2}") # False
# Compare strings alphabetically
print(f"Does name1 come before name2? {name1 < name2}") # True
🔹 Type Comparisons
# Comparing different types
number = 5
text = "5"
decimal = 5.0
print(f"Is number equal to text? {number == text}") # False
print(f"Is number equal to decimal? {number == decimal}") # True
Logical Operators
Combine multiple boolean values using and, or, not
🔹 AND Operator
Both conditions must be True for the result to be True
age = 25
has_license = True
can_drive = age >= 18 and has_license
print(f"Can drive? {can_drive}") # True
🔹 OR Operator
At least one condition must be True for the result to be True
is_weekend = True
is_holiday = False
can_sleep_in = is_weekend or is_holiday
print(f"Can sleep in? {can_sleep_in}") # True
🔹 NOT Operator
Reverses the boolean value
is_working = True
is_free = not is_working
print(f"Is free? {is_free}") # False
🔹 Combined Operators
You can combine multiple operators together
temp = 75
is_sunny = True
nice_day = temp > 60 and temp < 85 and is_sunny
print(f"Is it a nice day? {nice_day}") # True
Truthiness and Falsiness
Many values can be evaluated in a boolean context
🚫 Falsy Values (evaluate to False):
-
False,None -
0,0.0 -
""(empty string) -
[],(),{}(empty collections)
All other values are truthy!
# Let's see what Python considers True and False
print("Things that are False:")
print(bool(False)) # False
print(bool(0)) # False
print(bool("")) # False (empty string)
print("\nThings that are True:")
print(bool(True)) # True
print(bool(1)) # True
print(bool("hi")) # True
# Check if a list has anything in it
fruits = []
if fruits:
print("We have fruits!")
else:
print("No fruits yet")
Boolean in Numeric Context
In Python, True equals 1 and False equals 0 when used in math
# Converting True and False to numbers
print(f"True equals: {int(True)}") # 1
print(f"False equals: {int(False)}") # 0
# Simple math with True and False
print(f"True + True = {True + True}") # 2
print(f"True * 3 = {True * 3}") # 3
# A basic example: Counting passing grades
grades = [95, 85, 55, 75]
passing_grades = sum(grade >= 60 for grade in grades)
print(f"Number of passing grades: {passing_grades}") # 3
Practical Boolean Examples
Real-world applications of boolean logic
# Simple login check
def check_login(username, password):
"""Check if login is valid"""
# Check if username and password are correct
if username == "user" and password == "pass123":
print("Welcome!")
return True
else:
print("Wrong username or password")
return False
# Test the login
logged_in = check_login("user", "pass123")
print(f"Login successful: {logged_in}") # True
# Simple age check
def can_watch_movie(age):
"""Check if person can watch a PG-13 movie"""
if age >= 13:
print("You can watch the movie!")
return True
else:
print("Sorry, you're too young")
return False
# Test age check
allowed = can_watch_movie(15)
print(f"Can watch movie: {allowed}") # True