python day6 little knowledge transfer bytes type str summary

------------ ------------ restore content begins

1. python2, the difference python3

python2 ascii code python3 UTF-8

python2 prin support without parentheses is not supported by python

There are also python range Xrange (generator) python ordered list of range only

python in user interaction raw_input python3 as input

 

2. =, ==, is (relatively memory address), id (content) view id  

= Assignment 

== compare values ​​for equality

It is relatively memory address

LI1 = [1,2,3]

li1 = li2

print(li1 is li2)

print(id(li1),id(li2))

 

3. breakdown, a small string of data values

A digital value corresponding to the variable range of -5-256 consistent, using a common memory address

String range using the same memory address can not contain special characters 1. 2.s * 20 same address, s * 21 addresses two

Il = 6

i2 = 6

print(id(i1),id(i2))

 

In addition to digital and str list and dict tuple set no small data pool concept

 

4. encoding 

ascii code A: a one byte character represented by one byte 8 0000100

unicode A: a character is represented by four bytes 32 00010001 0001010 0,010,001,000,101,010

utf-8 A: a character represented by one byte in the 8 00101001: a Chinese represented by three bytes 24 0,101,010,100,100,100 00101001 

gbk A: a character represented by one byte in the 8 01010101: 16 represents a Chinese two bytes 0,101,010,100,100,101

Between individual coding binary 1 is not recognized each other, will be garbled

2. The storage and transmission of documents can not be unicode, only utf-8 or utf-16 or gbk or gbk2312 unicode or ascii because too much traffic occupancy

 

python3:

str python3 the encoding in memory is unicode

str bytes data type to be transmitted or stored, if need be converted to a type of data bytes

 

 

For English speaking  

forms str str = 'alex'

  01010101 unicode encoding

bytes form of s = b''alex '(represented preceded b)

    Encoding: 00100100 utf-8 gbk gk2312

 

For Chinese

str manifestation str = 'China'

  01010101 unicode encoding

bytes form of s = b'x \ e91 \ e91 \ e01 \ e21 \ e31 \ e32 '(16 hex)

    Encoding: 00100100 utf-8 gbk gk2312

 

str convert bytes English

  s1 = ‘alex’

       s11 = s1.encode('utf-8')

  print(s11)

str conversion bt \ ytes Chinese

  s2 = 'Chinese'

  s22 = s2.encode('utf-8')

  print(s22 )

 

 

End ------------ ------------ restore content

Guess you like

Origin www.cnblogs.com/longtongyu/p/12122672.html