Let’s do some Booleans!

  1. First let’s see how Boolean variables work in Python.  Variables that are of Boolean type have only two values ‘True’ or ‘False’

Type and run:

 a_Bool = 3>4

Abul  = 6<6.3

Ab_Wool =  ‘cat’==’cat’

a_bull =  ‘cow’==’bull’

print(a_Bool, Abul, Ab_Wool, a_bull)

All the statements on the right side of each equation becomes a  value of either ‘True’ or ‘False’  after you work them out.  And those variables on the left side store only that ‘True’ or ‘False’ value.

Three is  NOT greater than 4. So this statement is ‘False’

Six is indeed less than six point three. So this statement is ‘True’

The two strings ‘cat’ are exactly the same string. So the ‘cat’==’cat’  is ‘True’

The  string ‘cow’ is not the same string as ‘bull’.  So the statement equating them is ‘False’ 

2. >, < and == are all comparison operators.  Note that when we are assigning values to a variable, we use single operation ‘=’.  But when we are comparing we use double ‘==’

If we want to make a statement using NOT EQUAL TO , we use !=

Type and run:

zila = 64

print(zila>30)

print(zila!=8)

print(zila==’zila’)

print(” \”Bangladesh has more that 30 zilas\” : %s ”  %(zila>30))

print(” \”Bangladesh has 8 divisions, not zilas\” : %s ”  %(zila!=8))

print(” \”The boolean variable zila is the same as the string ‘zila’\” : %s ”  %(zila==’zila’))

Note that although a Boolean variable and a string cannot be equal (like in the third sentence) , we can use the argument specifier of string,  %s,  when using Boolean variables.  Weird, I know – but that is the best option – because Boolean variables have no argument specifier such as %b of  their own

3. There is this handy operator called ‘in’ which can be used to check if an item exists in  a list.

Type and run:

dutyRoster =[‘Mikasa’, ‘Armin’]

print(‘Mikasa’ in dutyRoster)

print(‘Armin’ in dutyRoster)

print(“Mikasa has duty today?  – %s”  %(‘Mikasa’ in dutyRoster))

print(“Eren has duty today?  – %s”  %(‘Eren’ in dutyRoster))

4. The ‘==’ operator matches  the values of two variables .

 The ‘is’ operation matches  the ‘instances’  – aka, whether those  two variables are the EXACT same variable. 

Type and run

avatar= [‘earth’, ‘fire’, ‘air’, ‘water’]

frozen2 =[‘earth’, ‘fire’, ‘air’, ‘water’]

print(avatar==frozen2)

print(avatar is frozen2)

What do you get

5. In the above, avatar and frozen2  are two variables with same exact VALUE   so  first print function evaluates as ‘True’ but they are NOT the SAME VARIABLE so  the second statement evaluates as ‘False’

To summarize :

The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not.

From <https://www.geeksforgeeks.org/difference-operator-python/>

Let’s work on this a little more to really internalize the concept.

Type and run

a= [2,1]

b= [2,1]

c=a

print(id(a))

print(id(b))

print(id(c))

print(a==b)

print(a is b)

print(a is c)

c = c+a

print(a is c)