Booleans
In Python, boolean is a datatype, which can hold only one of two values: True
or False
.
Note: Please note that the values True
and False
start with an uppercase alphabet.
Assign Boolean Value to a Variable
To define a variable with a boolean value of True
, assign True
to the variable using assignment operator.
is_available = True
Similarly, to define a variable with a boolean value of False
, assign False
to the variable.
is_available = False
bool() builtin function
Python has a builtin function bool(). This function takes a value and returns if the given value is True
or False
.
In the following program, we define a number in x, and use underscores between the digits.
Python Program
print(bool(25))
print(bool(0))
print(bool('hello'))
print(bool(0.58))
Output
True
False
True
True
We can use bool() builtin function to assign a variable with a boolean value.
Python Program
a = bool('apple')
b = bool('')
print('a :', a)
print('b :', b)
Output
a : True
b : False
Relational Operators return Boolean Value
Relational Operators return a boolean value. For example, if we compare two numbers to find out if the first number is larger than the second number using greater-than operator, then the operator returns a boolean value of True
if the first number is greater than the second number, or False
otherwise.
Python Program
print(25 > 10)
print(25 > 80)
Output
True
False
Conclusion
In this Python Tutorial, we learned what boolean values are, how to assign them to a variable, what bool() builtin function does, and how relational operators return boolean values.