Python = = and is, encoding and decoding

Source: Internet
Author: User

is and = == = Compare values on both sides
' Alex '  'Alex'print(a = = b  )=ten = tenprint (n= = [= = [Li1]print(= = = Li2) True

is compare memory address query memory address ID ()
' Alex ' Print (ID (a))   # 36942544  memory address  = tenprint(ID (n))     # 1408197120  = [All in all]print(li)      #38922760
A ='[email protected]'A1='[email protected]'Print(A isA1)#FalesN= 5//2N1= 2Print(n isN1)#Truea='a'*21b='a'*21Print(A isb

Small Data Pool range: -5~256
n =-6=-6print is N1)  #False= 5= 5Print   is N1)    #True= 257= 257printis N1)     #True

String

The memory address within a single string of * 20 is the same (the string cannot have special symbols such as: +-*/@!). And so on

A single string *20 above the memory address is not the same

' [email protected] '  '[email protected]'print is A1)    #  fale 

' aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa '  'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'print is # True
List
Li =[1,2,3=[1,2,3]print is Li2)  #False
Meta-group
Tu = (tu1= (all in a)print is)   #  False
Dictionary
Dic1 = {'name':'Alex'= {'name ' ':'Alex'}print is dic)    #  False

Note: Pycharm is a pit, and all the same strings in a py file are generally using the same memory address

Encoding and decoding

The codes are: asiic,unicode,gbk,utf-8,utf-16

ASCII code: Chinese is not supported, English, numerals, symbols, 8 bits, one byte

GBK Code: GB Code, support Chinese, English, numerals, symbols. English every 16 bits two bytes, Chinese 16 bit two bytes

Unicode: Universal code, support Chinese, English, numerals, symbols, English 32-bit four bytes. Chinese 32 bit four bytes

Utf-8: Currently the most used encoding is the variable length of the universal code, with a minimum of 8 bits. English 8 bit one byte Chinese 24 bit three bytes

Unicode is used in the program run phase in Python3 to display all content

Bytes Type: Transport and storage are all using bytes

The default is to use Utf-8 when Pycharm storage

. Encode (' code ') after getting the plaintext after encoding the corresponding binary bytes

. Decode (' code ') decodes the encoded bytes into the corresponding plaintext

Both operations are linked through Unicode as an intermediate bridge.

s ='Alex'#Print (S.encode (' Utf-8 ')) # encoded encode (' Utf-8 ') Utf-8 is the type of encoding to be encoded into#Print (S1.decode (' Utf-8 ')) ##bytes#b ' Alex '#s = ' hungry '#S1 = s.encode (' GBK ') #b ' hungry ' #b ' \xe9\xa5\xbf\xe4\xba\x86\xe5\x90\x97 '#Print (S.encode (' GBK ')) #b ' \xb6\xf6\xc1\xcb\xc2\xf0 '#print (S1)#Print (S1.decode (' Utf-8 '))
Note: What to encode what to decode or you will be confused!

Python = = and is, 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.