- 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!
- All You Need to Know About Boolean in Python
- Basics to Advanced — Learn It All!
- Introduction to Boolean Values
- Basics to Advanced — Learn It All!
- Boolean in Python
- The bool() in-built Function
- Evaluation of Boolean Expressions in Python
- Learn From The Best Mentors in the Industry!
- Boolean Operators
- The Not Operator
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.
All You Need to Know About Boolean in Python
Needless to say, Python is one of the most futuristic and popular programming languages which is widespread in almost all fields. It has a plethora of uses especially in blooming fields like Artificial Intelligence, Deep learning, and even Web Development. Consequently, a programmer of this generation must be well equipped with all the nooks and corners of Python.
Basics to Advanced — Learn It All!
In this article, we will discuss a trivial yet important topic in Python — Boolean Values. We will walk you through all the aspects and offer you deep insights that will make you feel confident enough to move forward to advanced topics in Python. We will explain the entire topic with periodic examples that will help you gain hands-on experience with Boolean in Python.
Introduction to Boolean Values
In general, a Boolean variable can have only two values — True or False. Or in other words, if a variable can have only these two values, we say that it’s a Boolean variable. It’s often used to represent the Truth value of any given expression.
Numerically, True is equal to 1 and False is equal to 0. In contrast with Electronics, when we say that a light bulb is switched on, it has a high value (that is 1) and vice-versa.
Basics to Advanced — Learn It All!
Boolean in Python
If you want to define a boolean in Python, you can simply assign a True or False value or even an expression that ultimately evaluates to one of these values.
You can check the type of the variable by using the built-in type function in Python.
Note that the type function is built-in in Python and you don’t have to import it separately.
Also, the word bool is not a keyword in Python. This means that you can assign a variable with the name bool. However, it’s not a good practice to do so.
bool = «Welcome to Simplilearn»
print(bool)
The bool() in-built Function
The bool() method in Python returns a boolean value and can be used to cast a variable to the type Boolean. It takes one parameter on which you want to apply the procedure. However, passing a parameter to the bool() method is optional, and if not passed one, it simply returns False.
It returns True if the value of x is True or it evaluates to True, else it returns False.
Evaluation of Boolean Expressions in Python
Mostly, if any value has some type of content in it, it is finally evaluated to True when we use the bool() method on it. In fact, except for empty strings, all the strings evaluate to True. Any number except 0, evaluates to True. Moreover, apart from the empty ones, all the sets, lists, tuples, and dictionaries also evaluate to True.
>>> bool([«Welcome», «to», «Simplilearn»])
>>> bool(846)
>>> bool(«Welcome»)
Usually, empty values such as empty strings, zero values, and None, evaluate to False.
You can use the bool method to cast a variable into Boolean datatype. In the following example, we have cast an integer into boolean type.
You can also use the bool method with expressions made up of comparison operators. The method will determine if the expression evaluates to True or False.
>>> bool (846.23 > 846.21)
>>> bool (0==1)
Learn From The Best Mentors in the Industry!
Boolean Operators
Boolean operators take Boolean values as inputs and in return, they generate a Boolean result. Broadly, we have three boolean operators in Python that are most frequently used. These are — Not, And, and Or operators.
The Not Operator
The Not operator takes only one argument and it just returns the opposite result. For example, if the argument is True, it returns False and vice-versa. The truth table for Not operator is mentioned below.