In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the difference between "python" = "and" is ". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the difference between "python" = "and" is "!
What's the difference between "=" and "is"?
Both "= =" and "is" are operators in Python. For beginners, they may interpret "an equal to b" as "an equals b", and "an is b" as "an is b". This may be why beginners confuse "=" and "is" in Python.
Before I begin, I'd like to show some examples of using "=" and "is".
> a = 5 > b = 5 > a = = bTrue > an is bTrue
It's simple, isn't it? Both a = = b and an is b return the result True. Let's look at another example:
> a = 1000 > b = 1000 > a = = bTrue > > an is bFalse
WTF?!? The only change from the first example to the second example is the values of an and b from 5 to 1000. But the returned results are different in "= =" and "is". Let's look at another example:
> a = [] > > b = [] > a = = bTrue > an is bFalse
Here is the last example. Do you feel like your brain is going to explode after reading it?
> > a = 1000 > b = 1000 > a = = bTrue > > an is bFalse > a = b > a = = bTrue > an is bTrue
The formal operation of "= =" is equal, while the operation of "is" is the identity. "=" is generally used to compare the values of two objects. "a = = b" should be interpreted as "whether the value of an is equal to the value of b". In all the above examples, the value of an is always equal to the value of b (even for the empty column example). Therefore, "a = = b" is always true.
(recommended tutorial: python tutorial)
Before I can explain my identity, I need to introduce the id function. We can use the id function to get the identity of the object. This identity is unique and constant for this object throughout the time. This identity is unique and constant to the object for the entire time. You can think of it as the address of the object. If two objects have the same identity, their values must also be the same.
> id (a) 2047616
The operator "is" compares whether the identity of two objects is the same. "an is b" means "the identity of an is the same as that of b".
If you know the actual meaning of "=" and "is", we can start to study the above examples.
The first is that the results in the first and second examples are different. The reason for displaying different results is that Python stores an array list of integers between-5 and 256, each with a fixed identity. When we assign an integer variable in this range, Python assigns the identity of the variable to an integer in the array list. As a result, for the first example, because the identities of an and b are both obtained from the array list, their identities are of course the same, so "an is b" is True.
> a = 5 > id (a) 1450375152 > b = 5 > id (b) 1450375152
But once the value of the variable is outside the range, because there is no object with that value within Python, Python creates a new identity for the variable and assigns the value to the variable. As mentioned earlier, identity is unique to each creation, so even if two variables have the same value, their identities will never be equal. This is why "an is b" turns out to be False in the second example.
> a = 1000 > id (a) 12728608 > b = 1000 > id (b) 13620208
PS: if you open two consoles and the value is still within that range, then you will get the same logo. However, if the value is not within that range, the result changes.
If you understand the difference between the first example and the second example, it is easy to understand the results of the third example. Because Python does not store an empty list object, Python creates a new object and assigns an empty list value. Whether the two lists are empty or the elements are the same, the result is the same.
> a = [1 True 10 > b = [1] > a = = b True > an is bFalse > id (a) 12578024 > id (b) 1000
Next, let's move on to the last example. The only difference between the second and last example is that there is another line of code a = b, which changes the fate of the variable a. The following results will tell you the real reason:
> > a = 1000 > > b = 2000 > id (a) 2047616 > > id (b) 5034992 > a = b > id (a) 5034992 > id (b) 5034992 > > a2000 > > b2000
As shown above, after a = b, the identity of an is changed to the identity of b. A = b assigns b's identity to a. So an and b have the same identity, so the value of an is now the same as the value of b (that is, 2000).
(recommended micro-course: python3 basic micro-course)
The last example tells us an important message that we may inadvertently change the value of the object without prior notice, especially if the object is a list.
> a = [1 id (a) 5237992 > > b = a > id (b) 5237992 > a.append (4) > a [1, 2, 4] > > b [1, 2, 4]
In the above example, because an and b have the same identity, their values must be the same. Therefore, after the new element is added to a, the value of b is also affected. To avoid this, if you want to copy values from one object to another without referencing the same identity, one of all ways is to use deepcopy in the copy module. For lists, we can also execute them with b = a [:].
> import copy > a = [1Jing 2Jing 3] > > b = copy.deepcopy (a) > id (a) 39785256 > id (b) 5237992
Use [:] to copy the element to the new variable.
Id (a) 39785256 > b = a [:] > id (b) 23850216 > > a.append (4) > a [1pime 2pas 3,4] > > b [1pr 2pr 3] so far, I believe everyone has a deeper understanding of what is the difference between "python" = "and" is ". You might as well have a deeper understanding of it. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.