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.
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.
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.
A = True |
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" |
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.
Syntax:
>>> bool([x]) |
It returns True if the value of x is True or it evaluates to True, else it returns False.
Example:
>>> A = 1.123 |
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.
Examples:
>>> bool(["Welcome", "to", "Simplilearn"]) |
Usually, empty values such as empty strings, zero values, and None, evaluate to False.
>>> bool("") |
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.
>>> a = 0 |
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.
For example,
>>> bool (846.23 > 846.21) |
Our Free Courses with Certificate
Python for Beginners Enroll Now! Python Libraries for Data Science Enroll Now! Python Pandas Basics Course Enroll Now! Getting Python Interview Ready Enroll Now! Machine Learning using Python Enroll Now!
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.
A |
Not A |
True |
False |
False |
True |
>>> not True |
-
The And Operator
It takes two input arguments and evaluates to True only if both the arguments are True. Please note that if the value of A is False, then the value of B doesn’t matter. This is called short-circuit evaluation. In such cases, knowing only one input is enough, and hence, the other input is not evaluated.
A |
B |
A and B |
True |
True |
True |
False |
True |
False |
True |
False |
False |
False |
False |
False |
>>> True and True |
-
The Or Operator
The Or operator returns False only when both the inputs to the operator are False, else it always returns True.
If the first argument is True, then it’s always true. Hence, it also uses short-circuit evaluation.
A |
B |
A or B |
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
>>> True or True |
Using Functions that Return Boolean in Python
You can also create functions in Python that return Boolean Values. In fact, many in-built functions in Python return their output in the form of Boolean values.
Example 1.
def simplilearn(): |
Example 2.
Several in-built functions return a Boolean in Python. Let’s see a few examples.
>>> my_string = "Welcome To Simplilearn" |
The method isalnum() returns whether or not the string is alpha-numeric. The method istitle() returns True if the string has all its words starting with an upper-case letter. The method endswith() returns True if the string ends with the letter mentioned in the argument.
Use Cases of Boolean in Python
The use of Boolean in Python is inevitable. Whether it’s an if-else condition, a simple function, or even a for-loop, Boolean is often used either directly or in disguise. Let’s code out a function that uses Boolean to determine whether a number is odd or even.
def func(value): |
You can see that using a simple Boolean check, how easy it becomes to build a function that returns whether a number is even or odd.
Conclusion
Boolean in Python is often used to compare values, check for membership, equality, or identity. It’s used to control the flow of a program in if-else conditions. In this tutorial, we have covered each and every aspect of the topic with hands-on examples that should give you enough confidence to move forward with Python.