# How to use Python boolean data type?

A data type specifies the values a variable can hold and what operations can be performed on it. In Python every type is a class, so we can say an object rather than a variable.

Boolean is one of the data types, that can have only two values either True or False. Following example shows a basic example for boolean variables.

```
x = True;
y = False;
#Check the type of x and y.
print(type(x));
print(type(y));
```

Output->

Type of x <class 'bool'> Type of y <class 'bool'>

NOTE: The True and False keywords are case sensitive, it must start with a capital letter. Ignoring the case (e.g true or false) will give an error.

## Integers/Floats values as Boolean-

The Integer and floating-point numbers having values of either zero or non zero. If we convert an integer or floating-point value into a boolean type, a value zero converts into False and all nonzero into True.

For conversion, we can use the bool() function, which takes an integer value as an argument and returns a boolean.

The following example shows how we can convert integers and floating-point values into Boolean types.

```
A = 100;
B = -20;
C = 3.6;
D = -4.3;
E = 0;
F = 0.0;
Conversion into boolean
print("Boolean value of", A, " after conversion = ", bool(A));
print("Boolean value of", B, " after conversion = ", bool(A));
print("Boolean value of", C, " after conversion = ", bool(C));
print("Boolean value of", D, " after conversion = ", bool(D));
print("Boolean value of", E, " after conversion = ", bool(E));
print("Boolean value of", F, " after conversion = ", bool(F));
```

*Output->*

Boolean value of 100 after conversion = True Boolean value of -20 after conversion = True Boolean value of 3.6 after conversion = True Boolean value of -4.3 after conversion = True Boolean value of 0 after conversion = False Boolean value of 0.0 after conversion = False

*Convert Boolean into Integer-*

The Boolean constants True and False represent One and Zero if converted into integers. The following example shows the conversion of Boolean values into integers.

```
A = True;
B = False;
print("Conversion of", A, " into an integer = ", int(A));
print("Conversion of", B, " into an integer = ", int(B));
```

Output->

Conversion of True into an integer = 1 Conversion of False into an integer = 0

### Why there is a boolean type if the value 0/1 can do the job?

The keyword True and False, looks that it’s two integer values. So why there is a separate type itself?

The reason is optimization, we know for Boolean value one a bit is sufficient to store in the memory.

## What are the Boolean Arithmetic operations?

All bit-level operations are applicable to the Boolean types. Arithmetic involves logical operations for single bit fields with operators. The operators know boolean operators.

Boolean Operator | Meaning and Use |
---|---|

== | Meaning: Check EqualityUsage: x = y, returns true if both x and y are the same |

!= | Meaning: Check not equalUsage: x != y, returns true if both x and y are not the same |

or | Meaning: EitherUsage: x or y, returns true if any of x or y is True. |

and | Meaning: BothUsage: x and y, returns true if both x and y are True. |

not | Meaning: Flip the valueUsage: not x, returns true if x is false or returns false if x is true. |

Following is an example in Python for all Boolean operators.

```
A = True;
B = False;
print(" Operator == , applies on A and B result = ", A == B);
print(" Operator != applies on A and B result = ", A != B);
print(" Operator and , applies on A and B result = ", A and B);
print(" Operator or , applies on A and B result = ", A or B);
print(" Operator not , applies on A result = ", not A);
print(" Operator or , applies on B result = ", not B);
```

Output->

Operator == , applies on A and B result = False Operator != applies on A and B result = True Operator and , applies on A and B result = False Operator or , applies on A and B result = True Operator not , applies on A result = False Operator or , applies on B result = Truee