Computer Science · Ch 5 — Getting Started with Python
Relational Operators
Relational Operators
A relational operator compares the values of the operands on either side of it and works out the relationship between them. The result of every relational comparison is a Boolean value — either True or False.
For all the examples below, assume the following variables have already been created:
>>> num1 = 10
>>> num2 = 0
>>> num3 = 10
>>> str1 = "Good"
>>> str2 = "Afternoon"
Remember from the start of Section 5.8 that strings are compared lexicographically using the ASCII values of their characters, character by character from the left.
The six relational operators
Equals to: ==
If the values of the two operands are equal, the condition is True; otherwise it is False.
>>> num1 == num2
False
>>> str1 == str2
False
num1 (10) is not equal to num2 (0), and "Good" is not the same string as "Afternoon", so both comparisons are False. Note carefully: == (double equals) tests equality; a single = is the assignment operator — mixing them up is a common beginner error.
Not equal to: !=
If the values of the two operands are not equal, the condition is True; otherwise it is False.
>>> num1 != num2
True
>>> str1 != str2
True
>>> num1 != num3
False
num1 and num3 both hold 10, so num1 != num3 is False.
Greater than: >
True if the value of the left-side operand is greater than the value of the right-side operand, otherwise False.
>>> num1 > num2
True
>>> str1 > str2
True
10 > 0 is clearly True. For the strings, "Good" is greater than "Afternoon" because the first character 'G' has a higher ASCII value than 'A'.
Less than: <
True if the value of the left-side operand is less than the value of the right-side operand, otherwise False.
>>> num1 < num3
False
>>> str2 < str1
True
num1 < num3 is 10 < 10, which is False (they are equal, not less). "Afternoon" < "Good" is True by the same ASCII rule as above.
Greater than or equal to: >=
True if the left-side operand is greater than or equal to the right-side operand.
>>> num1 >= num2
True
>>> num2 >= num3
False
>>> str1 >= str2
True
Less than or equal to: <=
True if the left operand is less than or equal to the right operand.
>>> num1 <= num2
False
>>> num2 <= num3
True
>>> str1 <= str2
False
Quick reference
| Operator | Operation | Result is True when… |
|---|---|---|
== | Equals to | both operands have equal values |
!= | Not equal to | the operands have different values |
| Operator | Operation | Description | Example (Try in Lab) |
|---|---|---|---|
| == | Equals to | If the values of two operands are equal, then the condition is True, otherwise it is False | >>> num1 == num2 False >> str1 == str2 False |
| != | Not equal to | If values of two operands are not equal, then condition is True, otherwise it is False | >>> num1 != num2 True >>> str1 != str2 True >>> num1 != num3 False |
| > | Greater than | If the value of the left-side operand is greater than the value of the right-side operand, then condition is True, otherwise it is False | >>> num1 > num2 True >>> str1 > str2 True |
| < | Less than | If the value of the left-side operand is less than the value of the right-side operand, then condition is True, otherwise it is False | >>> num1 < num3 False >>> str2 < str1 True |
| >= | Greater than or equal to | If the value of the left-side operand is greater than or equal to the value of the right-side operand, then condition is True, otherwise it is False | >>> num1 >= num2 True >>> num2 >= num3 False >>> str1 >= str2 True |