【320】Python 2.x 与 3.x 的区别

通过代码移植的报错进行梳理!

1. print 函数的区别

  Python 2.x 中可以加空格或者括号,但是 Python 3.x 只能是括号的

# Python 2.x
>>> print "processing..."
processing...
>>> print("processing...")
processing...

# Python 3.x
>>> print("processing...")
processing...

2. raw_input 与 input 函数

  Python 2.x 中 raw_input 与 Python 3.x 中 input 函数类似,而 Python 2.x 中的 input 函数接收数字或者带引号字符串

# Python 2.x
>>> a = raw_input("Value: ")
Value: Alex
>>> a
'Alex'
>>> b = input("Value: ")
Value: "Alex"
>>> b
'Alex'

# Python 3.x
>>> a = input("Value: ")
Value: Alex
>>> a
'Alex'

参考:python2.x和python3.x中raw_input( )和input( )区别

3. Tkinter 模块

  Python 2.x 为 Tkinter,但是 Python 3.x 为 tkinter(小写)

# Python 2.x
>>> import Tkinter

# Python 3.x
>>> import tkinter

参考:Python GUI编程(Tkinter)

4. 除法运算符

  Python 2.x 中 /、// 均为整除,但是 Python 3.x 中 / 表示除以,// 表示整除

# Python 2.x
>>> 77/60
1
>>> 77//60
1

# Python 3.x
>>> 77/60
1.2833333333333334
>>> int(77/60)
1
>>> 77//60
1

猜你喜欢

转载自www.cnblogs.com/alex-bn-lee/p/9206979.html