Index
- Definition
- Evaluate Values and Variables
- Comparison operators return Boolean values
- Functions can Return a Boolean
Definition
In programming you often need to know if an expression is True
or False
Evaluate Values and Variables
You can evaluate any expression in Python, and get one of two answers, True
or `False.
- The
bool()
function allows you to evaluate any value, and give youTrue
orFalse
in return,
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
Most Values are True
- Almost any value is evaluated to
True
if it has some sort of content.- Any string is
True
, except empty strings. - Any number is
True
, except0
. - Any list, tuple, set, and dictionary are
True
, except empty ones.
- Any string is
Some Values are False
- In fact, there are not many values that evaluate to
False
, except empty values, such as()
,[]
,{}
,""
, the number0
, and the valueNone
. And of course the valueFalse
evaluates toFalse
.
Comparison operators return Boolean values
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
Particular cases:
Functions can Return a Boolean
You can create functions that returns a Boolean Value:
Python also has many built-in functions that return a boolean value, like the isinstance()
function, which can be used to determine if an object is of a certain data type: