Related Articles
Boolean data type in Python
• Last Updated : 23 Dec, 2020

Python boolean type is one of the built-in data types provided by Python, which are defined by the True or False keywords. Generally, it is used to represent the truth values of the expressions. The output <class ‘bool’> indicates the variable is a boolean data type.

## Python3

 `a ``=` `True``type``(a)`` ` `b ``=` `False``type``(b)`

Output:

```<class 'bool'>
<class 'bool'>```

Generally, booleans values are returned as a result of some sort of comparison.

In the example below the variable res will store the boolean value of False after the equality comparison takes place.

## Python3

 `# Declaring variables``a ``=` `10``b ``=` `20`` ` `# Comparing variables``print``(a ``=``=` `b)`

Output:

`False`

## Integers and Floats as Booleans

Numbers can be used as bool values by using Python’s built-in bool() method. Any integer, floating-point number, or complex number having zero as a value is considered as False, while if they are having value as any positive or negative number then it is considered as True.

## Python3

 `var1 ``=` `0``print``(``bool``(var1))`` ` `var2 ``=` `1``print``(``bool``(var2))`` ` `var3 ``=` `-``9.7``print``(``bool``(var3))`

Output:

```False
True
True```

## Boolean Operations

Boolean Operations are simple arithmetic of True and False values. These values can be manipulated by the use of boolean operators which include AND, Or, and NOT. Common boolean operations are –

• or
• and
• not
• == (equivalent)
• != (not equivalent)

Example:

## Python3

 `A ``=` `True``B ``=` `False`` ` `print``(A ``or` `B)`` ` `print``(A ``and` `B)`` ` `print``(``not` `A)`` ` `print``(``not` `B)`` ` `print``(A ``=``=` `B)`` ` `print``(A !``=` `B)`

Output:

```True
False
False
True
False
True```

## Boolean Logic

Booleans are commonly used in the code to make it behave differently. Boolean can be used in conjunction with conditional statements to make it simpler. In many cases multiple conditions are needed to evaluate, for this purpose, AND and OR keywords are used. The AND returns True only if both the conditions are true, while OR returns true if any one of the conditions is true.

## Python3

 `num ``=` `10`` ` `if` `num > ``1` `and` `num < ``10` `:``    ``print``(``"less then 10"``)`` ` `elif` `num >``10` `or` `num ``=` `10` `:``    ``print``(``"geater or equal to 10"``)`` ` `else` `:``    ``print``(``"not in range"``)`

Output:

`geater or equal to 10`

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up