本文整理 Python 入門必備基礎知識,包含互動模式與腳本模式執行方式、識別字命名規則、保留字清單、縮排規則、多行陳述式撰寫方式,適合初學者快速建立正確觀念。
$ python
>>> print "Hello, Python!"
Hello, Python!
# test.py
print "Hello, Python!"
$ python test.py
Hello, Python!
# 加入 shebang 直接執行
#!/usr/bin/python
print "Hello, Python!"
$ chmod +x test.py
$ ./test.py
_ 開頭,後接字母、底線、數字(0-9)。Manpower ≠ manpower)。MyClass)。my_function)。_private:慣例為私有。__strong_private:名稱修飾(name mangling)。__special__:語言內建特殊名稱。
完整清單:and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield 等(Python 2.x)。
\ 續行:total = item_one + \ item_two + \ item_three() [] {} 內隱含續行:days = ['Mon', 'Tue', 'Wed']# 這是註解""" 多行註解 """(實為字串常數)。