Comparison Operators
So far, we have used two comparison operators, which are <
(less than) and >
(greater than). Now let's list them all.
==
- Equals
!=
- Not equals
>
- Greater than
>=
- Greater than or equal to
<
- Less than
<=
- Less than or equal to
In addition to numbers, we can also compare text. For example:
brand = "Toyota"
if brand == "Toyota":
print("The car brand is Toyota.")
else:
print("The car brand is not Toyota.")
# Prints: The car brand is Toyota.
Remember that all comparisons are ultimately either true or false. If the comparison is true, the code in the if
statement is executed. If the comparison is false, the code in the else
statement is executed.
"Shakespeare" != "Shakespeare" # False
10 == 10 # True
10 < 10 # False
10 >= 10 # True
Instructions
Create a variable brand
with a value of "Volkswagen"
.
Create an if
statement that prints Das Auto
if the variable brand
is equal to Volkswagen
.
Start programming for free
3/10