Python Boolean Data Type

Python Boolean Data Type


Boolean or bool is a built-in data type in the python programming language. It can have two possible values –

  1.   True
  2.  False

These are equivalent to 1 and 0 respectively, the values that are used in languages like c or c++.  It is generally associated with conditional statements. Either a condition will be true or false on the basis of this different actions are taken by changing the control flow.

For example –


# Check whether a and b are equal or not
if a==b:
 print ("a and b are equal")
 print("a and b are not equal")

When you run this program first condition will be checked that whether a and b are equal or not. You can see the output in the image below –

Since both variables contain different values the condition will become false and the statement under else will be executed.

Booleans are numeric type in Python

The bool type is a subclass of int class which is the standard integer type in Python. You can perform all the arithmetic operations on them as well. Even you can compare them to numbers.

For example –

# Statement 1
# Statement 2

Here statement 1 will print 1 and statement 2 will print 0.0 as the output. You can check this by executing it on your system.

True and False values in Python

In Python, the numeric value zero (integer or fraction), empty strings, null value (None), empty containers such as list, set, etc are considered as false.

All other values are considered true.

You can check this by using bool() which will evaluate and return true or false. Now see the code below.

# The given statements will print False as output
print (bool(0))
print (bool(None))
print (bool(""))
print (bool([]))
# The following statements will print True as output

Now see the output in the image below –

Functions returning a boolean value

In Python, there are many functions that can also return true or false for example isinstance() function which can be used to check a certain type of data type.

print(isinstance(x, float))

Since 15.3 is a float value this code will print True as the output.

Next, we will learn and work with the binary type of data in Python.

Previous Python Numeric Data Types
Next Escape sequences in Python

About author

You might also like

Python Programming 0 Comments

Data Types in Python

This post was last updated on February 2nd, 2021 at 07:54 pmIn programming, the data type is the classification of data items. In Python, it tells the interpreter how the

Programming 0 Comments

Escape sequences in Python

An escape sequence or escape character allows you to insert a special character in a string. For example, you can insert a new line in a string using \n character.

Python Programming 0 Comments

Python Set Comprehension

A Python set is an unordered collection of unique items that is iterable, mutable, and has no duplicate items. You can perform mathematical set operations such as union, intersection, set


No Comments Yet!

You can be first to comment this post!

Leave a Reply