1. print
Python2:
在python2中print被看成是一个语句,而不是一个函数,想要输出“hello python”,print语句应该这样写:
print "hello python"
Python3:
在Python3中,print()会被看成是一个函数,因此要输出“hello python”,print语句这样写:
print("hello python")
2. 整数的除法
Python2:
整数的除法是强类型的,比如:
a = 5 / 2
print a # a=2
Python3:
Python3中整数的除法更直观,比如:
a = 5 / 2
print(a) # a=2.5
3. Unicode
Python2:
Python 2 默认使用 ASCII 字母表,前缀加 u 代表 Unicode。
u"Hello,python!" # 前缀 u 代表是Unicode
Python3:
Python3中默认使用Unicode,如果想让3兼容2,可以通过字符串的前面保留‘u’来实现。
4. 发展
Python 3 和 Python 2 之间的最大区别不是语法上的,而是 Python 2.7 将在 2020 年失去后续的支持,Python 3 将继续开发更多的功能和修复更多的错误。