1, python2 and the difference between frequently used finishing python3
Python2 #
Print 'ABC'
# returns a list
Range ()
# Returns a value generator , not list. Every time you build only one value, avoid temporary use a lot of memory
xrange () # Builder
# input
raw_input (...)
# python3
Print ( 'abc')
# returns an iterator value , you need to generate a list you need to use list ( the Range (...))
the Range ()
the INPUT (...)
2, copy == comparison value comparison is equality, the memory address comparing id (Content)
In Li1 = >>> [l, 2,3 ] >>> = LI2 in Li1 >>> Pring (ID (in Li1), ID (LI2)) >>> Print (ID (in Li1), ID (LI2)) 2,472,533,256,584 2,472,533,256,584 # presence decimal numbers and strings pools, pool the decimal range, are using the same memory address # numbers 256 ~ -5 >>> = I1. 6 >>> = I2. 6 >>> Print (ID (I1), ID (I2)) 140,717,920,019,440 140,717,920,019,440 >>> I1 = 300 >>> 300 I2 = >>> Print (ID (I1), ID (I2)) 2472536947344 2472536947248 >>> I1 = 256 >>> 256 I2 = >>> Print (the above mentioned id (I1), id(i2)) 140717920027440 140717920027440 >>> i1 = 257 >>> >>> i2 = 257print(id(i1),id(i2)) 2472536947344 2472536947248 >>> i1 = -5 >>> i2 = -5 >>> print(id(i1),id(i2)) 140717920019088 140717920019088 >>> i2 = -6 >>> i1 = -6 >>> print(id(i1),id(i2)) 2472536947344 2472536947248
# Strings: 1, can not have special characters # 2, s * 20 or the same address, s * After the 21 are two addresses ( . To be verified, during the test, more than 1 * are estimated to be changes in the different versions of lead ) >>> S1 = ' 121212asdasdasdadasdasdasdadadqrjladksfjaiodfu ' >>> S2 = ' 121212asdasdasdadasdasdasdadadqrjladksfjaiodfu ' >>> Print (ID (S1), ID (S2)) 2472537146064 2472537146064 >>> S1 = ' 121212asdasdasdadasdasdasdadadqrjladksfjaiodfu @ ' >>> S2 = ' 121212asdasdasdadasdasdasdadadqrjladksfjaiodfu @ ' >>> Print (ID (S1), id(s2)) 2472537146160 2472537146064# list、dict、tuple、set # list >>> l1 = [1,] >>> l2 = [1,] >>> print(l1 is l2) False >>> >>> l2 = l1 >>> print(l1 is l2) True
3, character set encoding
ASCII
A: 00000010. 8 bits of a byte
unicode A: 00000000 00000001 00000010 00000100 32 four bit bytes
in: 0,000,000,000,000,001 0,000,001,000,000,110 32 four byte
utf-8 A: 00100000 8 bits of a byte
are: 00000001 0,000,001,000,000,110 24 three bytes
gbk a: 00000110 8 bits of a byte
in which: 0,000,001,000,000,110 two 16-bit bytes
1, between the various binary coding, can not recognize each other, it will be garbled.
2, file storage, transmission, is not unicode (only utf-8 utf-16 gbk, gb2312, asciid etc.)
Py3:
STR is unicode encoded in memory.
Type bytes
for English:
STR: Expressions: s = 'beer'
encoding: Unicode
bytes: Expressions: s = b'beer '
encoding: utf-8 gbk. . . .
For Chinese:
str: Expressions: s = 'Xiong'
encoding: Unicode
bytes: Expressions: s = b '\ xe9 \ x9b \ x84 \ xe5 \ xa4 \ xa7'
encoding: utf-8 gbk. . . .
>>> s = 'beer' >>> s1 = b'beer' >>> print(s,type(s)) beer <class 'str'> >>> print(s1,type(s1)) b'beer' <class 'bytes'> >>> >>> >>> s = '雄大' >>> print(s,type(s) ) 雄大 <class 'majestic' >>> S1 = B>'Str' SyntaxError: bytes can only contain ASCII literal characters. >>> >>> >>> s1 = s.encode('utf-8') >>> print(s1) b'\xe9\x9b\x84\xe5\xa4\xa7' >>> s1 = s.encode('gbk') >>> print(s1) b'\xd0\xdb\xb4\xf3'