- Python Boolean
- Introduction to Python Boolean data type
- The bool() function
- Falsy and Truthy values
- Summary
- Python Booleans
- Boolean Values
- Example
- Example
- Evaluate Values and Variables
- Example
- Example
- Most Values are True
- Example
- Some Values are False
- Example
- Example
- Functions can Return a Boolean
- Example
- Example
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
Python Boolean
Summary: in this tutorial, you’ll learn about the Python boolean data type, falsy and truthy values.
Introduction to Python Boolean data type
In programming, you often want to check if a condition is true or not and perform some actions based on the result.
To represent true and false, Python provides you with the boolean data type. The boolean value has a technical name as bool .
The boolean data type has two values: True and False .
Note that the boolean values True and False start with the capital letters ( T ) and ( F ).
The following example defines two boolean variables:
is_active = True is_admin = False
Code language: Python (python)
When you compare two numbers, Python returns the result as a boolean value. For example:
>>> 20 > 10 True >>> 20 < 10 False
Code language: Python (python)
Also, comparing two strings results in a boolean value:
>>> 'a' < 'b' True >>> 'a' > 'b' False
Code language: Python (python)
The bool() function
To find out if a value is True or False , you use the bool() function. For example:
>>> bool('Hi') True >>> bool('') False >>> bool(100) True >>> bool(0) False
Code language: Python (python)
As you can see clearly from the output, some values evaluate to True and the others evaluate to False .
Falsy and Truthy values
When a value evaluates to True , it’s truthy. And if a value evaluates to False , it’s falsy.
The following are falsy values in Python:
- The number zero ( 0 )
- An empty string »
- False
- None
- An empty list []
- An empty tuple ()
- An empty dictionary <>
The truthy values are the other values that aren’t falsy.
Note that you’ll learn more about the None , list , tuple , and dictionary in the upcoming tutorials.
Summary
- Python boolean data type has two values: True and False .
- Use the bool() function to test if a value is True or False .
- The falsy values evaluate to False while the truthy values evaluate to True .
- Falsy values are the number zero, an empty string, False, None, an empty list, an empty tuple, and an empty dictionary. Truthy values are the values that are not falsy.
Python Booleans
Booleans represent one of two values: True or False .
Boolean Values
In programming you often need to know if an expression is True or False .
You can evaluate any expression in Python, and get one of two answers, True or False .
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
Example
When you run a condition in an if statement, Python returns True or False :
Example
Print a message based on whether the condition is True or False :
if b > a:
print(«b is greater than a»)
else:
print(«b is not greater than a»)
Evaluate Values and Variables
The bool() function allows you to evaluate any value, and give you True or False in return,
Example
Evaluate a string and a number:
Example
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 , except 0 .
Any list, tuple, set, and dictionary are True , except empty ones.
Example
The following will return True:
Some Values are False
In fact, there are not many values that evaluate to False , except empty values, such as () , [] , <> , «» , the number 0 , and the value None . And of course the value False evaluates to False .
Example
The following will return False:
One more value, or object in this case, evaluates to False , and that is if you have an object that is made from a class with a __len__ function that returns 0 or False :
Example
class myclass():
def __len__(self):
return 0
Functions can Return a Boolean
You can create functions that returns a Boolean Value:
Example
Print the answer of a function:
def myFunction() :
return True
You can execute code based on the Boolean answer of a function:
Example
Print «YES!» if the function returns True, otherwise print «NO!»:
def myFunction() :
return True
if myFunction():
print(«YES!»)
else:
print(«NO!»)
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:
Example
Check if an object is an integer or not:
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.