What is the difference between is and == in Python? Introduction to the difference between is and == in Python

不言
Release: 2018-10-29 17:26:00
forward
2793 people have browsed it

What this article brings to you is about the difference between is and == in Python? The introduction of is and == in Python has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

There are many operators in Python. Today we will talk about the essential difference in application between the is and == operators.

Before talking about the difference between the two operators is and ==, you must first know the three basic elements contained in objects in Python, namely: id (identity), type (data type) and value ( value).

Is and == are both used to compare and judge objects, but the contents of object comparison and judgment are different. Let’s take a look at the specific differences.

==The difference between comparison operator and is identity operator

== is a comparison operator among python standard operators, used to compare and determine whether the value of two objects (value) Equality, for example, the comparison between the following two strings:

Example 1.

>>> a = 'cheesezh'
>>> b = 'cheesezh'
>>> a == b
True
Copy after login

is is also called the identity operator. This operator compares and determines the unique identity between objects. , that is, whether the ids are the same. By comparing the following lists, you will understand how the is identity operator works:

>>> x = y = [4,5,6]
>>> z = [4,5,6]
>>> x == y
True
>>> x == z
True
>>> x is y
True
>>> x is z
False
>>>
>>> print id(x)
3075326572
>>> print id(y)
3075326572
>>> print id(z)
3075328140
Copy after login

The first three examples are all True, so why is the last one False? The values ​​of x, y and z are the same, so there is no problem with the first two being True. As for why the last one is False, you will understand if you look at the IDs of the three objects.

Let’s look at another example. In Example 3, (a==b) of a and b of the same type is both True, but (a is b) is not.

>>> a = 1 #a和b为数值类型
>>> b = 1
>>> a is b
True
>>> id(a)
14318944
>>> id(b)
14318944
>>> a = 'cheesezh' #a和b为字符串类型
>>> b = 'cheesezh'
>>> a is b
True
>>> id(a)
42111872
>>> id(b)
42111872
>>> a = (1,2,3) #a和b为元组类型
>>> b = (1,2,3)
>>> a is b
False
>>> id(a)
15001280
>>> id(b)
14790408
>>> a = [1,2,3] #a和b为list类型
>>> b = [1,2,3]
>>> a is b
False
>>> id(a)
42091624
>>> id(b)
42082016
>>> a = {'cheese':1,'zh':2} #a和b为dict类型
>>> b = {'cheese':1,'zh':2}
>>> a is b
False
>>> id(a)
42101616
>>> id(b)
42098736
>>> a = set([1,2,3])#a和b为set类型
>>> b = set([1,2,3])
>>> a is b
False
>>> id(a)
14819976
>>> id(b)
14822256
Copy after login

It can be seen from Example 3 that a is b is True only when it is of numerical type and string type. When a and b are of tuple, list, dict or set type, a is b is False.

Try it yourself and see if their IDs will be equal when a=257 and b=257. In fact Python In order to optimize speed, a small integer object pool is used to avoid frequent application and destruction of memory space for integers. Python’s definition of small integers is [-5, 257), only if the numbers are between -5 and 256 will their IDs be equal. If they exceed this range, it will not work. In the same way, string objects also have a similar buffer pool. Naturally, they will not be equal if they exceed the range. .

In general, a is b is True only if it is numeric and string and in the general object pool. Otherwise, when a and b are int, str, tuple, list, When it is dict or set type, a is b are both False.

The above is the detailed content of What is the difference between is and == in Python? Introduction to the difference between is and == in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template