BOOLEAN DATA-TYPE


BOOLEAN DATA-TYPE

There are two built-in values in Boolean data type, which are True and False. These help in determining whether the given statement is true or false.

In programming, there are various instances where you have to identify whether a statement is True or False. In Python, you can get the Boolean data types as the answer while evaluating any expression, like equality and inequality.

bool() function:

There is a bool() function in Python that helps evaluate any value as true or false.

In this function, the resultant for almost all the values are true if some content is stored in them. All the strings are also true, except for the empty ones. All numbers in the function come as True except for zero. Furthermore, any tuple, set, list, or dictionary is also true except for an empty one. However, the empty brackets apart from a few of the cases mentioned above give False as the result when placed in the bool() function.

Boolean Operations:

These are the simple arithmetic operations of Boolean values. The Boolean values can also be manipulated by using Boolean operators, including:

  • AND
  • OR
  • NOT
  • == (equivalent)
  • != (not equivalent)

Boolean Logic:

Booleans are used in code mainly to have it behave uniquely. They can be used in conjunction along with conditional statements to assure their simplicity. Many times multiple conditions must be evaluated in coding, and in that case, AND or OR keywords are used. AND only returns TRUE if both the conditions come out to be true, while OR can return true even if anyone's condition is true.

Example No.1:

print(10 > 9) print(10 == 9) print(10 < 9)

OUTPUT:

True
False
False

Example No.2:

class myclass(): def __len__(self): return 0 myobj = myclass() print(bool(myobj))

OUTPUT:

False