Forum

Learn Python Boolea…
 
Notifications
Clear all

Learn Python Boolean statements

1 Posts
1 Users
0 Reactions
11 Views
 josh
(@josh)
Member Admin
Joined: 2 months ago
Posts: 510
Topic starter  

Python Boolean statements are all about making decisions—your code asks “Is this true?” and acts accordingly. Let’s break it down 🧠🔍


✅ What Is a Boolean?

A Boolean is a data type that can only be:

  • True
  • False

These are special keywords in Python and always start with a capital letter.


🔍 Using Booleans in Conditions

Booleans are most commonly used in if statements:

is_raining = True

if is_raining:
    print("Grab an umbrella!")
else:
    print("Enjoy the sunshine!")
  • If is_raining is True, the first block runs.
  • If it’s False, the else block runs.

🧪 Boolean Expressions

You can create Boolean values using comparison operators:

x = 10
print(x > 5)   # True
print(x == 10) # True
print(x < 3)   # False

These expressions return True or False.


🔗 Logical Operators

Combine multiple Boolean expressions:

age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("Welcome to the concert!")
  • and: Both conditions must be true.
  • or: At least one must be true.
  • not: Reverses the Boolean value.

🧠 Evaluating Values with bool()

You can convert other types to Boolean using bool():

print(bool(0))        # False
print(bool(123))      # True
print(bool(""))       # False
print(bool("hello"))  # True
  • Empty values (like 0, "", [], None) are False.
  • Non-empty values are True.

 


   
Quote
Share: