Python interview questions nine (the difference between python2 and python3)

List at least 5 differences between Python3 and Python2?

  • print from statement to function

python2:print 1, 2+3

python3:: print ( 1, 2+3 )

  • range 与 xrange

python2:range( 0, 4 ) 结果 是 列表 [0,1,2,3 ]

python3:list( range(0,4) )

python2 :xrange( 0, 4 ) 适用于 for 循环的变量控制
python3:range(0,4)

  • String

python2: strings are stored as 8-bit strings

python3: Strings are stored as 16-bit Unicode strings

  • Changes to the try except statement

python2:

try:
	pass
except    Exception, e :
	pass

python3:

try:
	pass
except    Exception as e :
	pass
  • open a file

python2:file( ..... ) 或 open(.....)

python3:只能用 open(.....)

  • Enter a string from the keyboard

python2: raw_input ("prompt information")

python3: input ("Prompt message")

  • The bytes data type
    bytes can be regarded as a "byte array" object, each element is an 8-bit byte, and the value range is 0 ~ 255.

Since the string is stored in unicode encoding in Python 3.0, when writing to a binary file, the string cannot be directly written (or read), and must be encoded as a sequence of bytes in some way before it can be written.

Published 44 original articles · liked 0 · visits 1226

Guess you like

Origin blog.csdn.net/weixin520520/article/details/105451013