Course 3.1Lesson 1 of 5
A comparison asks a question about two values and gives True or False. The six signs are ==, !=, <, >, <= and >=. One equals sign stores a value, and two equals signs compare. Text can be compared too: a capital letter makes it different, and text is compared in alphabetical order. The answer of a comparison is a bool value, so we can keep it in a variable.
Try the code and the simulations beside the lesson.
After this lesson you can
True or False,= and == apart, andThink about your day. If it rains, you take an umbrella. If the bell rings, you go to class. If you are hungry, you eat. All of these are decisions, and each one begins with a question that has a yes or no answer.
A program needs the same. A game asks: is the score more than the high score? A website asks: is the password right? A calculator asks: is the divisor zero? To ask such questions, Python has comparisons.
A comparison asks a question about two values. The answer is always True or False, the fourth type of value that you met in the variables course.
| Sign | Question | Example | Answer |
|---|---|---|---|
== | is it equal to? | 5 == 5 | True |
!= | is it not equal to? | 5 != 3 | True |
< | is it less than? | 3 < 5 | True |
> | is it greater than? | 3 > 5 | False |
<= | is it less than or equal to? | 5 <= 5 | True |
>= | is it greater than or equal to? | 4 >= 5 | False |
Notice the two-sign forms. <= is the less-than sign followed directly by an equals sign, and the other way round does not exist. There is no space between the two characters. != is an exclamation mark and an equals sign.
This is the most common mistake of beginners, and it is worth a moment.
When you write a decision, you ask a question, so you need two signs. If you write one, Python stops with a SyntaxError. This is a help: it tells you that you made the usual mistake.
a != b is always the opposite of a == b, and a >= b the opposite of a < b. Only one of each pair is True.
You can also compare text. Look at the next lines of the program.
"apple" == "apple" is True."apple" == "Apple" is False. A capital letter is a different character, so the two pieces of text are different."apple" < "banana" is True. Text is compared in alphabetical order, from the first letter on. a comes before b."Zebra" < "apple" is also True. In the computer, all the capital letters come before the small letters. So an alphabetical sort with capitals and small letters mixed gives surprises. A common solution is to make both small first, with lower().5 == 5.0 is True. A whole number and a decimal number with the same value are equal."5" == 5 is False. Text is never equal to a number. This is the reason why you must change what a user types with int().The answer of a comparison is a value, and a value can be kept in a variable. For example, is_adult = age >= 18 stores True or False. A name that starts with is_ or has_ is a good name for such a variable, because it reads like a question.
= when you want to ask. Use ==.=< or =>. The right forms are <= and >=, with the equals sign second."18" >= 18 gives a TypeError. Change the text first."yes" == "Yes" is False. Use answer.lower() == "yes".= = is wrong, == is right.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
True or False. The signs are ==, !=, <, >, <= and >=.= stores a value. Two == ask whether two values are equal.is_adult = age >= 18.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
7 > 7 and 7 >= 7?x = 3 and x == 3?"Cat" == "cat" give?=> or >=?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.