Python Booleans

🐍 Python Booleans (True / False)

Booleans represent truth values in Python.

There are only two Boolean values:

True
False

⚠ Note: The first letter must be capital (True, not true).


✅ Boolean Type

You can check the data type using type():

print(type(True)) # <class 'bool'>
print(type(False)) # <class 'bool'>

🧐 Boolean from Comparison

Booleans are commonly returned from comparison operators:

Operator Meaning
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater or equal
<= Less or equal

Example:

print(10 > 5) # True
print(10 < 5) # False
print(10 == 10) # True
print(10 != 5) # True

🧠 Boolean in if Condition

Booleans control program decisions:

age = 18

if age >= 18:
print(“You can vote.”)
else:
print(“You cannot vote.”)

Output:

You can vote.

🔄 Boolean from Values using bool()

Python automatically converts values to booleans:

print(bool("Hello")) # True
print(bool(123)) # True
print(bool(0)) # False
print(bool("")) # False

❗ Values that become False:

Value Result
0 False
0.0 False
None False
"" (empty string) False
[] (empty list) False
{} (empty dict) False
() (empty tuple) False

Everything else becomes True.


🔧 Boolean Operators

Python uses:

Operator Meaning
and Both must be True
or At least one must be True
not Reverses result

Examples:

print(True and False) # False
print(True or False) # True
print(not True) # False

Real example:

age = 20
student = True
print(age > 18 and student) # True

✔ Boolean with Strings

text = "Python"

print(“Py” in text) # True
print(“Java” in text) # False


🎯 Boolean Function Example

def is_even(num):
return num % 2 == 0
print(is_even(10)) # True
print(is_even(7)) # False

⭐ Mini Program

password = input("Enter password: ")

if len(password) >= 6:
print(“Password accepted 😊”)
else:
print(“Password too short ❌”)

You may also like...