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 as 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
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 bool value. If the first letter is lowercase you will get "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
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
The bool values True and False can be used in different boolen operations. As expected the boolean operations results 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
The equal operation will check if the given two values or data are same and if they are same returned value will be True. If they are not 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 boolen oepration equal is
Not equal . Not equal operation and operator is expressed as
!= and check two given values. If the values are 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.
|True and False||False|
|True and True||True|
|False and True||False|
|False and False||False|
Boolean OR Logic
or logic or operator is used with the calculation of two bool values.
or operator returns a boolean value according to the provided parameters. The following table provides all calculation variations about the and operator.
|True or False||True|
|True or True||True|
|False or True||True|
|False or False||False|
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 if False the not operator wil lreturn True.
print(not False) # True print(not True) # False print(not True == False) # True print(not True == (1 != 1)) # True