Python's is and = = and encoding and decoding

Source: Internet
Author: User

One, is and = =

1.1== comparison, compare = = = value on both sides of the data

1A ='Alex'2b ='Alex'3 Print(A = = b)#True4 5n = 106N1 = 107 Print(n = = n1)#True8 9Li1 = [A]TenLi2 = [A] One Print(Li1 = = Li2)#True

  

1.1.1id () or Fetch data storage address

1 ' Alex ' 2 Print (ID (a))   # 36942544  memory address 34 n =5print(ID (n))     # 1408197120 6 7 li = [8)print(ID (LI))      #38922760

Is comparison, is the address of the data on both sides

1 #string2A ='Alex'3b ='Alex'4 Print(A isb#True5 #Digital6n = 107N1 = 108 Print(n isN1)#True

1.2 Small Data pools

Small Data pool range for 1.2.1 numbers-5 to 256 (including-5 and 256)

1.2.2 Small Data pool range for strings

Single character *20, data store address is different

The data store has a different address if there are special characters in the string

1A ='[email protected]'2A1 ='[email protected]'3 Print(A isA1)#Fales4 5n = 5//26N1 = 27 Print(n isN1)#True8 9A ='a'*21Tenb ='a'*21 One Print(A isb) # False  A  -A ='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' -b ='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' the Print(A isb

Second, encoding and decoding

First look at several common coding methods:

ASCII code: is used to denote English, which is expressed in 1 bytes, where the first digit is 0 and the other 7 bits store data, which can represent 128 characters altogether.

GBK /gb2312/gb18030: The represents the kanji. gbk/gb2312 represents Simplified Chinese, GB18030 means traditional Chinese, and a character is represented by 16 bits.

Unicode encoding: contains all the characters in the world, is a character set, and 32 bits represents one character.

UTF-8: is one of the ways Unicode characters are implemented, which uses 1-4 characters to represent a symbol, varying the length of a byte depending on the symbol .

English          8-bit

European text 16-bit

Chinese 24-bit

2.1 encoding

encode (encoded) in parentheses specifies what type of encoding to encode into. The corresponding byte

after getting the encoding

 1  s =  "  Alex   " 2  s1 =  "  are you hungry   " 3  print  (S.encode (  " utf-8      )) #   b ' Alex '  4  print  (S1.encode (  " utf-8     )) #  b ' \xe9\xa5\xbf\xe4\xba\x86\xe4\xb9\x88 '  


Decode (encoded) decodes the encoded bytecode into the corresponding plaintext

1s ='Alex'2S1 ='are you hungry?'3 Print(S.encode ('Utf-8'))#b ' Alex '4 Print(S1.encode ('Utf-8'))#b ' \xe9\xa5\xbf\xe4\xba\x86\xe4\xb9\x88 '5 6S2 = S1.encode ('Utf-8')#b ' \xe9\xa5\xbf\xe4\xba\x86\xe4\xb9\x88 '7S3 = S2.decode ('Utf-8')#are you hungry?

  

Python's is and = = and encoding and decoding

Related Article

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.