There’s a subtle difference between the Python identity operator [is
] and the equality operator [==
]. Your code can run fine when you use the Python is
operator to compare numbers, until it suddenly doesn’t. You might have heard somewhere that the Python is
operator is faster than the ==
operator, or you may
feel that it looks more Pythonic. However, it’s crucial to keep in mind that these operators don’t behave quite the same.
The ==
operator compares the value or equality of two objects, whereas the Python is
operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators ==
and !=
,
except when you’re comparing to None
.
In this course, you’ll learn:
- What the difference is between object equality and identity
- When to use equality and identity operators to compare objects
- What these Python operators do under the hood
- Why using
is
andis not
to compare values leads to unexpected behavior - How to write a custom
__eq__[]
class method to define equality operator behavior
These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.
Assume variable a holds 10 and variable b holds 20, then −
== | If the values of two operands are equal, then the condition becomes true. | [a == b] is not true. |
!= | If values of two operands are not equal, then condition becomes true. | [a != b] is true. |
If values of two operands are not equal, then condition becomes true. | [a b] is true. This is similar to != operator. | |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | [a > b] is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | [a < b] is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | [a >= b] is not true. |
student_two:
print["Penny comes before Paul in the alphabet."]
elif student_one < student_two:
print["Paul comes before Penny in the alphabet."] Our code returns: "Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!" Venus, Software Engineer at Rockbot Paul comes before Penny in the alphabet. Let’s break down our code. On the first two lines, we declare two variables that store our student names. In this case, these names are Penny and Paul. Then, we create an if statement that uses the We also create an
In this case, Paul’s name comes before Penny’s in the alphabet, so the code in our ConclusionComparing two strings is an important feature of Python. For instance, you may be creating a login form that needs to compare the password a user has entered with the password they have set for their account. Python comparison operators can be used to compare strings in Python. These operators are: equal to [ Chủ Đề |