Boolean Data type

Boolean data type has only two values - True and False

In [1]:
True
Out[1]:
True
In [2]:
False
Out[2]:
False

Like other values, boolean values can be used in expressions and variable. But write them with a capital T and F

In [3]:
spam = True
In [4]:
spam
Out[4]:
True

Comparison Operators

OperatorMeaning
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>Greater than or equal to

Expressions with comparison operators evaluate to Boolean values.

In [5]:
42 == 42
Out[5]:
True
In [6]:
8 > 9
Out[6]:
False
In [7]:
8 > 8
Out[7]:
False
In [8]:
8 >= 8
Out[8]:
True

42 < 42

In [9]:
Number = 42
In [10]:
42 == Number
Out[10]:
True
In [11]:
100 < Number
Out[11]:
False

Integers and strings will always not be equal to each other.

In [12]:
42 == '42'
Out[12]:
False
In [13]:
42 == int('42')
Out[13]:
True

Float values and integer values can be equal to each other.

In [14]:
42.0 == 42
Out[14]:
True

Single = (=) is a variable assignment operator
Double = (==) is equal to operator.

Boolens Operators - and, or, not

and

and operator evaluates an expression to True if both boolean values are true. Otherwise it evaluates the expression to False

In [15]:
True and True
Out[15]:
True
In [16]:
True and False
Out[16]:
False
In [17]:
False and True
Out[17]:
False
In [18]:
False and False
Out[18]:
False

or

or operator evaluates an expression to True if either or both of the values are true. It evaluates the expression to False only if both the values are false.

In [19]:
False or False
Out[19]:
False
In [20]:
True or False
Out[20]:
True
In [21]:
False or True
Out[21]:
True
In [22]:
True or True
Out[22]:
True

not

not operator evaluates to the opposite of the boolean value.

In [23]:
not True
Out[23]:
False
In [24]:
not False
Out[24]:
True
In [25]:
Number = 42
In [26]:
string = 'spam'
In [27]:
Number > 10 and string == 'spam'
Out[27]:
True