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. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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
B = False
C = (1==3)

You can check the type of the variable by using the built-in type function in Python.

a-true

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)

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.

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
>>> bool(A)
>>> A = 23
>>> B = 23.01
>>> bool(A==B)

a-bool

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"])
>>> bool(846)
>>> bool("Welcome")

bool-welcome

Usually, empty values such as empty strings, zero values, and None, evaluate to False.

>>> bool("")
>>> bool([])
>>> bool(0)
>>> bool(None)
>>> bool()

bool-false-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.

>>> a = 0
>>> b = bool(a)
>>> print(b)

bool-o

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)
>>> bool (0==1)

bool-true

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
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.

A

Not A

True

False

False

True

>>> not True
>>> not False

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
>>> False and True
>>> True and False
>>> False and 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
>>> False or True
>>> True or False
>>> False or False

true-false

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():
return True

if simplilearn() == True:
print("I use simplilearn")
else:
print("I don't use simplilearn")

def-simplilearn

Example 2.

Several in-built functions return a Boolean in Python. Let’s see a few examples.

>>> my_string = "Welcome To Simplilearn"
>>> my_string.isalnum()

>>> my_string.istitle()

>>> my_string.endswith("n")

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.

my-string

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):
return (bool(value%2==0))

if(func(9)):
print("It\'s Even")
else:
print("It\'s Odd")

def-dunc

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449