Python Booleans

You can use this data type to represent boolean values. Internally Python represents True as 1 and False as 0.

The only allowed values for this data type are: True and False.

>>> b=True
>>> print(type(b))
<class 'bool'>

For Instance:

print(5 > 4) ==> True
print(5 == 4) ==> False
print(5 < 4) ==> False

You can get boolean value using a condition in an if statement, Python returns True or False.

x = 100
y = 55
if y > x:
    print("y is greater than x")
else:
    print("y is not greater than x")

#Output
y is not greater than x

Evaluate Values and Variables using bool()

You can evaluate values using bool() function. It returns either True or False.

  • All string is True, except empty strings.
  • All number is True, except 0.
  • All list, tuple, set, and dictionary are True, except empty ones.
a = "Python"
b = 5
print(bool(a))
print(bool(b))

While declaring values using bool() function, mostly it returns True values. Only few values are False. Let’s have a look on them.

print(bool(False))
print(bool(None))
print(bool(()))
print(bool([]))
print(bool(""))
print(bool(0))
print(bool({}))

Functions returns a Boolean

Functions can be created to return a Boolean Value.

def boolFunction():
    return True
print(boolFunction())

Previous Read : Python Variables

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “Python Booleans”

  1. Pingback: Python Operators

Leave a Comment