Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between the = = in Python and the is operator

Shulou Source: shulou.com Published: 2022-06-01 21:21:38 09月30日 Update

This article mainly explains "what is the difference between == and is operator in Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and study and learn "what is the difference between == and is operator in Python" together!

Let's start with the twin cat analogy. Suppose you have identical twin cats. They have the same black fur and the same piercing green eyes. Just by looking, we can't tell which is which.

Now, bring the == and is operators from Python into this example.

The == operator compares by checking for equality. If these two cats are two Python objects, then if they are compared using the == operator,' Both cats are Equal' is returned as the answer.

The is operator compares by examining the identity. If we compare them with is, we get "two cats are not equal" as the answer.

To really understand this, we will write some code.

First, we'll create a list named a that contains elements [3, 4, 5], and another list b that will point to list a.

Let's check these two lists. As we can see, they point to lists that look the same.

Because these two list objects look identical, comparing them for equality using the == operator will yield the expected results.

However, this does not tell us whether a and b point to the same object. Of course, we know they are, because we've assigned them before, but suppose we don't know--how do we know?

The answer is to compare two variables with the is operator. This confirms that both variables actually point to a list object.

Now, let's use list() to create another list c that contains the elements of list a.

If we look at list c, it looks similar to the list pointed to by a and b.

Now we will compare a and c using the == and is operators.

Here, the == operator is true because they all have the same content.

Python tells us that c and a point to two different objects, although their contents may be the same.

So, to summarize, let's try to break down the difference between is and == into two short definitions.

If two variables point to the same object, the is expression evaluates to True.

The == expression evaluates to True if the variable references the same object (same content).

Thank you for reading, the above is the content of "Python == and is operator what is the difference", after the study of this article, I believe that we have a deeper understanding of Python == and is operator what is the difference between this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Operator two point object same content variable answer learning check element twin result expression twin different similar short sharp between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Redmi Shulou Technology macOS