Bootstrap

python3x和python2x唯一的区别就是_Python3.x和Python2.x的区别

展开全部

python3.4学习笔记(四) 3.x和2.x的区别

在2.x中:print html,3.x中必须改成:print(html)

import urllib2

ImportError: No module named 'urllib2'

在python3.x里面,用62616964757a686964616fe4b893e5b19e31333363363439urllib.request代替urllib2

import thread

ImportError: No module named 'thread'

在python3.x里面,用_thread(在前面加一个下划线)代替thread

在2.x中except Exception,e : 3.x中改为except (Exception):

=================================

print函数

虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来。

在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。

Python 2.7.6

print 'Python', python_version()

print 'Hello, World!'

print('Hello, World!')

print "text", ; print 'print more text on the same line'

输出:

Hello, World!

Hello, World!

text print more text on the same line

---------------------------

Python 3.4.1

print('Python', python_version())

print('Hello, World!')

print("some text,", end="")

print(' print more text on the same line')

输出:

Hello, World!

some text, print more text on the same line

print 'Hello, World!'

File "", line 1

print 'Hello, World!'

^

SyntaxError: invalid syntax

;