2009-06-02 70 views
94

我正在學習Python和甚至不能寫的第一個例子:使用「print」時語法無效?

print 2 ** 100 

這給SyntaxError: invalid syntax

在2

指出這是爲什麼?我使用的是3.1版

+1

你在哪裏找到這樣的例子嗎?它是在一本書還是一個網站? – 2009-06-02 01:20:27

+0

它可能是*學習Python *。 – 2009-06-02 02:27:14

回答

201

這是因爲在Python 3,他們已經取代了print聲明print功能

的語法是現在和以前或多或少相同,但它需要的括號:

從「what's new in python 3」文檔:

Old: print "The answer is", 2*2 
New: print("The answer is", 2*2) 

Old: print x,   # Trailing comma suppresses newline 
New: print(x, end=" ") # Appends a space instead of a newline 

Old: print    # Prints a newline 
New: print()   # You must call the function! 

Old: print >>sys.stderr, "fatal error" 
New: print("fatal error", file=sys.stderr) 

Old: print (x, y)  # prints repr((x, y)) 
New: print((x, y))  # Not the same as print(x, y)! 
12

您需要括號:

print(2**100)