Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
What are Variables?Numbers — Integers and FloatsNumber OperationsStrings — Creating and Using TextString FormattingBooleans and NoneType ConversionGetting User InputBest Practices
Python/Variables Data Types/Booleans None

✅ Booleans and None — Truth Values and Nothing

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.


🎯 Boolean Values

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

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)     # True

🧩 Boolean Logic (AND, OR, NOT)

Boolean 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)  # True

🚫 The None Type

None 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: None

💡 Real-World Examples

Check User Eligibility

age = 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!")

Validate User Input

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")

Check Available Resources

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}")

✅ Key Takeaways

ConceptRemember
TrueBoolean value representing yes/on/correct
FalseBoolean value representing no/off/incorrect
==Equals (comparison)
!=Not equals
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
andBoth must be true
orAt least one must be true
notOpposite value
NoneRepresents nothing/no value

🔗 What's Next?

Now you understand booleans and comparisons. Let's learn about type conversion — changing data between types!

Next: Type Conversion →


Ready to practice? Try challenges or take the quiz


Resources

Python Docs

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

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn