Comparison and Logical Operators
Learn how to compare values.
Comparison and logical operators
We know how to use built-in functions as well as define our own functions. We also know how to take input from the user and assign it to a variable for storage (and reuse) or pass it as further input to a function. We also know how to output something on the screen for the user. But we can only write sequential programs, where the computer fetches and executes one line of our code after another, from the first executable line to the last, in a strictly sequential order.
This equips us with the necessary skills to solve a lot of interesting problems, but there are still some problems that sequential programs cannot solve. Sometimes, a solution might require non-sequential execution as well:
This chapter is about learning how to write programs that can, while they are executing, be selective about executing lines of code. Secondly, we will learn to write programs that can execute some lines of our code multiple times. Our project will require us to be able to code this way.
The project requirements
Let’s attend to our client’s requirements for the project once again, this time focusing on the highlighted requirements for announcing the correct score to the user.
Imagine Alex, an elementary school student, uses our app and manages to get three questions right. When Betty, another student, uses our app, she gets all five questions right. We are not going to write two different programs—one for Alex and one for Betty. But, the same piece of code should compute that Alex’s score is 3 and Betty’s score is 5.
Of course, our app cannot know in advance what the final score of its user is eventually going to be. It is going to be known to our program while the code is being executed by Alex and Betty, respectively.
Note: How does the code know if the user has entered the correct answer to the question?
Till now, we have seen how to compute the correct expected answer, given a question. For instance, if the question was 4 * 7:
user_answer = int(input("What is 4 * 7? "))
correct_answer = 4 * 7
So we have two numbers in two appropriately named variables. How would we know if user_answer is correct? Because if we know it is correct, we need to update the user’s score by 1. Line 3 is inviting you; what can be done there right after we have correct_answer and user_answer?
Well, we know what you’re thinking. You’re thinking that it’s as simple as comparing the two numbers to see if they are equal or not!
Right, we do have a symbol for equality, we have known it since childhood, but we have been using that in Python for assigning some value to a variable. We have even used that in lines 1–2. But why not take a chance and use that for comparing two numbers as well to see if they are equal?
user_answer = int(input("What is 4 * 7? "))
correct_answer = 4 * 7
user_answer = correct_answerThat did not work as planned, or did it? How would we know? We need to be able to test our hypotheses. How about, if we insert a print statement before and after line 3? This way, we might know what user_answer = correct_answer is doing.
user_answer = int(input("What is 4 * 7? "))
correct_answer = 4 * 7
print("User's answer: ", user_answer, "Correct answer: ", correct_answer)
user_answer = correct_answer
print("User's answer: ", user_answer, "Correct answer: ", correct_answer)Enter 28 and it seems to work! But, run it again.This time, give a wrong answer, let’s say 20, and see what happens! Line 4 clearly ends up assigning the value in correct_answer to user_answer. But we wanted to use = for checking if the two numbers are equal or not.
Equality, not assignment
Since a single = is taken by Python for assignment purposes, what’s the next best option for an equality operator? How about double equals ==?
user_answer = int(input("What is 4 * 7? "))
correct_answer = 4 * 7
print("User's answer: ", user_answer, "Correct answer: ", correct_answer)
user_answer == correct_answer
print("User's answer: ", user_answer, "Correct answer: ", correct_answer)If we try 28, we’ll have the same output as before. If we try 20, this time, the user_answer has not been updated! So, we do know that == did not assign the value 28 to user_answer, since the value of user_answer remained 20 even after line 4 has been executed. However, we need to encapsulate the code in line 4 within print in the hope of figuring out what == has been up to.
user_answer = int(input("What is 4 * 7?"))
correct_answer = 4 * 7
print("User's answer: ", user_answer, "Correct answer: ", correct_answer)
print(user_answer == correct_answer)
print("User's answer: ", user_answer, "Correct answer: ", correct_answer)Run it for 28 and then 20. When executed, line 4 prints True first, then False. That is the comparison operator we wanted!
A final word about ==
Conceptually, think of == as another built-in function that takes in two numbers and returns True or False.
Other comparison operators
We have seen == in its complete manifestation. There are other comparison operators as well:
Operators | Description | Example Usage |
| The two numbers are exactly equal | 2 == 2 would return |
| The two numbers are not equal | 2 != 2 would return |
| The first number is greater than the second | 2 > 2 would return |
| The first number is greater than or equal to the second | 2 >= 2 would return |
| The first number is less than the second | 2 < 2 would return |
| The first number is less than or equal to the second | 2 <= 2 would return |
How do we update the user’s score?
Now, we know how to compare user_answer to correct_answer for equality. We also know how to update the score by 1 (score = score + 1). However, our client’s project requires our code to update the score only if the user has correctly answered the question, not otherwise. How do we do that? Let’s go to the next lesson.