The difference between IS and = = and encode () and Decode ()

Source: Internet
Author: User

The difference between IS and = =:

To know the difference between IS and = =, let us first look at the ID () method, the return value of the ID method is the memory address of the object , in short, this will give us a bunch of values can not understand

' Brother Jay ' Print (Element)

Program Run Result: 1936055642416

It turns out that this is a bunch of numbers that we really can't read

Why do you mention this? Because the next thing I'm going to say is the difference between IS and = =.

We all know that = = is used to compare the two objects are equal, but is it? is actually used to compare two objects are the same (note, here is the same), and is a deeper comparison, is the direct comparison of memory address

To improve memory utilization, Python uses a method of reusing object memory for simple int objects and simple str objects, so that Python does not allocate memory for those simple objects two times, but assigns only one

A = 2= 2print is b)

Result: True

Encode () and decode ():

From the literal point of view, encode and decode are encoded and decoded respectively. In Python, the default encoding is Unicode, which means:

     decode                 ---------> str (Unicode)---------> str

' Text '
str2 = U.encode ('gbk') # encode U with GBK encoding to get bytes type Object
Print  output result: B ' \xce\xc4\xd7\xd6 '

STR3 = U.encode ('utf-8') # encode U with utf-8 encoding to get bytes type Object
Print  output result: B ' \xe6\x96\x87\xe5\xad\x97 '


u1 = Str2.decode (' GBK ') # with GBK decoding str2
Print (U1)
Output Result: Text


U2 = Str3.decode (' utf-8 ') #以utf -8 decoding STR3
Print (U2)
Output Result: Text

  

The difference between IS and = = and encode () and Decode ()

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.