Comparison operations compare some value or operand and, based on a condition, they produce a Boolean. When comparing two values you can use these operators:
Let's say we assign a a value of 6. We can use the equality operator denoted with two equal (==) signs to determine if two values are equal.
In [1]:
a=5
a==6
Out[1]:
The result is false, as 5 does not equal 6.
Consider the following equality comparison operator i>5. If the value of the left operand, in this case the variable i, is higher than the value of the right operand, in this case 5, then the condition becomes True or else we get a False. If we set i equal to 6, we see that 6 is larger than 5 and as a result, we get True.
In [2]:
i=6
i>5
Out[2]:
If we set i=2 the condition is false as 2 is less than 5:
In [3]:
i=2
i>5
Out[3]:
Let's display some values for i in the figure. Set the values greater than 5 in green and the rest in red. The green region represents where the condition is True, the red where the statement is false. If the value of i is 2, we get False as the 2 falls in the red region. Similarly, if the value for i is 6 we get a True as the condition falls in the green region.
The inequality test uses an exclamation mark preceding the equal sign, if two operands are not equal then the condition becomes True. For example, the following condition will produce True as long as the value of i is not equal to 6:
In [4]:
i=2
i!=6
Out[4]:
When i equals six the expression produces False.
In [5]:
i=6
i!=6
Out[5]:
We can use a number line as before, when the condition is True the corresponding numbers are marked in green and for where the condition is False the corresponding number is marked in red. If we set i equal to 2 the operator is true as 2 is in the green region. If we set i equal to 6 we get a False as the condition falls in the red region.
We can use the same methods on strings, for example, if we use an equality operator on two different strings. As the strings are not equal, we get a False.
In [6]:
"ACDC"=="Michael Jackson"
Out[6]:
If we use the inequality operator, we get a True as the strings are not equal.
In [7]:
"ACDC"!="Michael Jackson"
Out[7]:
We can also perform inequality operations, the order of the letter depends on the ASCII value. The decimal value shown in the following table represents the order of the character:
For example, the decimal equivalent of ! is 21, while the decimal equivalent of + is 43. Therefore + is larger than !.
In [8]:
'+'>'!'
Out[8]:
Similarly, the value for A is 101, and the value for B is 102 therefore:
In [9]:
'B'>'A'
Out[9]:
When there are multiple letters, the first letter takes precedence in ordering:
In [10]:
'BA'>'AB'
Out[10]:
Branching allows us to run different statements for different inputs. It is helpful to think of an if statement as a locked room, if the statement is true you can enter the room and your program will run some predefined task, but if the statement is false your program will skip the task.
For example, consider the blue rectangle representing an ACDC concert. If the individual is 18 or older, they can enter the ACDC concert. If they are under the age of 18 they cannot enter the concert. We have the if statement and we have the expression that can be True or False, the brackets are not necessary. We have a colon within an indent; then we have the expression that is run if the condition is True. The statements after the if statement will run regardless of whether the condition is true or false.
In [11]:
age=19
#age=18
#expression that can be true or false
if age>18:
#within an indent, we have the expression that is run if the condition is true
print("you can enter" )
#The statements after the if statement will run regardless if the condition is true or false
print("move on")
Try uncommenting the age variable.
It is helpful to use the following diagram to illustrate the process. On the left side, we see what happens when the condition is True. The person enters the ACDC concert representing the code in the indent being executed; they then move on. On the right side, we see what happens when the condition is False; the person is not granted access, and the person moves on. In this case, the segment of code in the indent does not run, but the rest of the statements are run.
The else statement will run a different block of code if the same condition is false. Let's use the ACDC concert analogy again. If the user is 17 they can not go to the ACDC concert, but they can go to the Meatloaf concert. The syntax of the else statement is similar; we simply append the statement else with the new condition. Try changing the values of age to see what happens:
In [12]:
age=18
#age=19
if age>18:
print("you can enter" )
else:
print("go see Meat Loaf" )
print("move on")
The process is demonstrated below, where each of the possibilities is illustrated on each side of the image. On the left is the case where the age is 17, we set the value of the variable age to 17, and this corresponds to the individual attending the Meatloaf concert. The right portion shows what happens when the individual is over 18, and the individual is granted access to the concert.
The elif statement, short for else if, allows us to check additional conditions if the proceeding condition is false. If the condition is True, the alternate expressions will be run. Consider the concert example, where if the individual is 18 they will go to the Pink Floyd concert instead of attending the ACDC or Meat-loaf concert. The person of 18 years of age enters the area, and as they are not over 19 years of age they can not see ACDC, but as they are 18 years of age, they attend Pink Floyd. After seeing Pink Floyd, they move on. The syntax of the elif statement is similar in that we merely add the statement elif with the condition. We then add the expression we would like to execute it the statement is True with an indent.
In [13]:
age=18
if age>18:
print("you can enter" )
elif age==18:
print("go see Pink Floyd")
else:
print("go see Meat Loaf" )
print("move on")
The three combinations are shown in the figure below. The left-most region shows what happens when the individual is less than 18 years of age. The central component shows when the individual is 18. The rightmost shows when the individual is over 18.
Look at the following code:
In [14]:
album_year = 1983
album_year=1970
if album_year > 1980:
print("Album year is greater than 1980")
print("")
print('do something..')
Feel free to change album_year
value to other values -- you'll see that the result changes!
Notice that the code in the above indented block will only be executed if the results are True.
In [15]:
album_rating = 8.5
if album_rating > 8:
print("this album is Amazing !")
As before, we can add an else block to the if block. The code in the else block will only be executed if the result is False.
Syntax:
if (condition):
# do something
else:
# do something else
If the condition in the if statement is false, the statement after the else block will execute. This is demonstrated in the figure:
In [16]:
album_year = 1983
#album_year=1970
if album_year > 1980:
print("Album year is greater than 1980")
else:
print("less than 1980")
print("")
print('do something..')
Feel free to change the album_year
value to other values -- you'll see that the result changes based on it!
In [18]:
album_rating = 7.5
if album_rating > 8:
print("this album is Amazing !")
else:
print("this album is ok")
Sometimes you want to check more than one condition at once. For example, you might want to check if one condition and another condition is True. Logical operators allow you to combine or modify conditions.
These operators are summarized for two variables using the following truth tables:
The and statement is only True when both conditions are true. The or statement is true if one condition is True. Finally the not statement outputs the opposite truth value.
We would like to determine if an album was released after 1980 and before 1990. Both time periods between 1981 and 1989 satisfy this condition. This is demonstrated in the figure below. The green on lines a and b represents periods where the statement is True. The green on line c represents where both conditions are True, this corresponds to where the green regions overlap.
The block of code to perform this check is given by:
In [19]:
album_year = 1980
if(album_year > 1979) and (album_year < 1990):
print ("Album year was in between 1981 and 1989")
print("")
print("Do Stuff..")
To determine if an album was released before 1980 or after 1990, we can use an or statement. Periods before 1981 or after 1989 satisfy this condition. This is demonstrated in the following figure, the color green in a and b represents periods where the statement is true. The color green in c represents where at least one of the conditions are true.
The block of code to perform this check is given by:
In [20]:
album_year = 1990
if(album_year < 1980) or (album_year > 1989):
print ("Album was not made in the 1980's")
else:
print("The Album was made in the 1980's ")
The not statement checks if the statement is false:
In [21]:
album_year = 1983
if not (album_year == '1984'):
print ("Album year is not 1984")
In [22]:
album_year = 1983
if album_year < 1980 or album_year == 1989 or album_year == 1983:
print(album_year)
</div>
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.