The difference between = = and is in Python

Source: Internet
Author: User

= = compares the values on both sides

Is compares the memory addresses on both sides to get memory addresses by ID ()

Small Data pools: The values we use are stored in small data pools for use by other data.

Small data pools are limited to numbers and strings:

Number of decimal pool ranges-5---256

String 1. Cannot have spaces;

2. The length cannot exceed 20 characters;

3. Cannot have special characters such as: @#$

1. ID () Get memory address

' Alex ' Print (ID (a))   # 36942544  memory address

2. = = Compare values on both sides

' Alex '  'Alex'print(a = = b  )=ten = tenprint (n= = [li1 = [n/a]print(= = = Li2) #True

3. Is compare memory address

  3.1 List Dictionary tuple collection

#ListLi =[1,2,3]li2=[1,2,3]Print(Li isLI2)#False#Meta-groupTu = (A) TU1= (A)Print(TU isTU1)#False#DictionaryDic1 = {'name':'Alex'}dic= {'name':'Alex'}Print(Dic1 isDiC#False

Compare the above is#ListLi =[1,2,3]li2=[1,2,3]Print(Li = = li2)#True#Meta-groupTu = (A) TU1= (A)Print(Tu = = TU1)#True#DictionaryDic1 = {'name':'Alex'}dic= {'name':'Alex'}Print(Dic1 = = dic)#True

3.2 for the STR small data pool

String 1. Cannot have spaces;

2. The length cannot exceed 20 characters;

3. Cannot have special characters such as: @#$

A ='[email protected]'A1='[email protected]'Print(A isA1)#Falesa='a'*21b='a'*21Print(A isb#Falsea="AB"b="AB"Print(A isb#False But the result in Pycharm is true

3.3 Small Data pool for int

Number of decimal pool ranges-5---256

n =-6=-6  # out of range print is N1)  #False  =-5=-5print is N1)    #True

n = 257
N1 = 257
Print (n is N1) #cmd中为False displayed as true in Pycharm

 

The difference between = = and is in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.