Python Boolean Type and Operators

Boolean is a mathematical or logical term used for True and False and related operations. Python also provides boolean types, operators, and operations. Boolean provides two types called True and False. Keep in mind that “True” and “False” are string types that are different.

Bool or Boolean Type

Boolean is a type which can store True and False values. The boolean type can not store another value. The boolean type is called as Bool in Python and the base class is named as bool and expressed as <class 'bool'> .

b = True

print(type(b))
<class 'bool'>


c = False

print(type(c))
<class 'bool'>

Keep in mind that the True and False first letter should be uppercase in order to specify the bool value. If the first letter is lowercase you will get the “NameError: name ‘true’ is not defined” error.

>>> b = true
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined

Boolean True is a value used to describe truthness or positive value. Generally, other types like integer, floating-point, string, etc can be converted into bool type by using the bool() method. The bool() method is provided as builtin.

print( bool(1) )
# True

print( bool(-1) )
# True

print( bool(0) )
# False

print( bool(1.1) )
# True

print( bool(-1.1) )
# True

print( bool("") )
# False

print( bool('') )
# False

print( bool("python") )
# True

print( bool([]) )
# False

print( bool([ 1, 2 , 3 ]) )
# True

Boolean Operations

The bool values True and False can be used in different boolean operations. As expected the results of the boolean operation will be a bool value True or False. Python provides the following bool or boolean operations.

  • Equal Operation
  • Not Equal Operation
  • And Operation
  • Or Operation
  • Not Operation

Boolean Equal

The equal operation will check if the given two values or data are the same and if they are the same returned value will be True. If they are not the same the returned bool value will be False.

print( 1==1 )
# True

print( ""=='' )
# True

print( "ismail"=="ismail" )
# True

print( 1==-1 )
# False

print( 0==-0 )
# True

Boolean Not Equal

The negative form of the boolean operation equal is Not equal . Not equal operation and operator is expressed as != to check two given values. If the values are the same it will return False and if the values are not the same it will return True.

print( 1!=1 )
# False

print( ""!='' )
# False

print( "ismail"!="ismail" )
# False

print( 1!=-1 )
# True

print( 0!=-0 )
# False

Boolean AND Logic

and logic or operator is used with the calculation of two bool values. and the operator returns a bool value according to the provided parameters. The following table provides all calculation variations about the and operator.

AND OperatorResult
True and FalseFalse
True and TrueTrue
False and TrueFalse
False and FalseFalse

Boolean OR Logic

or logic or operator is used with the calculation of two bool values. or the operator returns a boolean value according to the provided parameters. The following table provides all calculation variations about the and operator.

OR OperatorResult
True or FalseTrue
True or TrueTrue
False or TrueTrue
False or FalseFalse

Boolean NOT Logic

not logic or operator is used to return the reverse of the given boolean value. If the boolean value is True not operator will return False. If the boolean value is False the not operator will return True.

print(not False)
# True

print(not True)
# False

print(not True == False)
# True

print(not True == (1 != 1))
# True

Complete Boolean Logic Operator and Operations Table

ABnot Anot BA == BA =! BA or BA and B
TrueFalseFalseTrueFalseTrueTrueFalse
FalseTrueTrueFalseFalseTrueTrueFalse
TrueTrueFalseFalseTrueFalseTrueTrue
FalseFalseTrueTrueTrueFalseFalseFalse

Leave a Comment