python

Boolean Datatype


Boolean datatype usually known as binary system, that means 0 or 1

0= False

1= True

  • Binary number are not for calculations rather than much used in conversions conditional checking and to define truthiness of objects.
  • Truthiness in the sense default value or None type results False, any other values results in True.

 

Creating Boolean Values:

Booleans are created by assigning True or False to a variable, or as a result of logical operations.

Example of Boolean datatype:

a = True
b = False
print(type(a))  # Output: <class 'bool'>
print(type(b))  # Output: <class 'bool'>

 

Boolean Operations:

Python supports several operations that return boolean values, including comparison operators and logical operators.

Boolean Conversion:

You can convert other data types to boolean using the bool() function. In Python, the following values are considered False in a boolean context:

  • ‘None’
  • ‘False’
  • Zero of any numeric type: 0, 0.0, 0j, etc.
  • Empty sequences and collections: '', (), [], {}, set(), range(0).

All other values are considered True.

Examples:

print(bool(1))         # Output: True
print(bool(0))         # Output: False
print(bool(-1))        # Output: True
print(bool(3.14))      # Output: True
print(bool(''))        # Output: False
print(bool('Hello'))   # Output: True
print(bool([]))        # Output: False
print(bool([1, 2, 3])) # Output: True
print(bool(None))      # Output: False

 


python