$ python Python 3.3.2 (default, Dec 10 2013, 11:35:01) [GCC 4.6.3] on Linux Type "help", "copyright", "credits", or "license" for more information. >>>
在 Windows 系統(tǒng)上: C:\Users\Administrator>python Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>在 Python 提示符下編寫下面的文本代碼,然后按Enter:
>>> print ("Hello, Python!")
如果你使用的是 Python(Python2.X)的舊版本,在print 函數(shù)使用括號(hào)是一個(gè)可選項(xiàng)。這將產(chǎn)生以下的結(jié)果:
Hello, Python!
解釋器調(diào)用一個(gè)腳本參數(shù)開始執(zhí)行腳本,并一直持續(xù)到腳本完成。當(dāng)腳本完成,解釋器活動(dòng)失效。
讓我們在腳本中寫一個(gè)簡單的 Python 程序。Python文件使用 .py 擴(kuò)展名。 創(chuàng)建一個(gè)文件 test.py 并寫入以下代碼:
print ("Hello, Python!")
假設(shè)你已經(jīng)在 PATH 變量中設(shè)置了 Python 解釋器路徑。 現(xiàn)在,試著如下運(yùn)行這個(gè)程序 -
在Linux上執(zhí)行$ python test.py
Hello, Python!在 Windows 上執(zhí)行
C:\Python3>Python test.py
Hello, Python!
#!/usr/bin/python3
print ("Hello, Python!")
假設(shè)你已經(jīng)在 /usr/bin目錄安裝好了 Python 解釋器。現(xiàn)在,試著如下運(yùn)行這個(gè)程序 -
$ chmod +x test.py # This is to make file executable $./test.py
Hello, Python!
Python標(biāo)識(shí)符是用來標(biāo)識(shí)變量,函數(shù),類,模塊或其他對象的名稱。標(biāo)識(shí)符是以字母A到Z開始或a?z或后跟零個(gè)或多個(gè)字母下劃線(_),下劃線和數(shù)字(0?9)。
Python標(biāo)識(shí)符范圍內(nèi)的不容許有如:@, $ 和 % 符號(hào)。Python是一種區(qū)分大小寫的編程語言。因此,Manpower 和 manpower 在Python中是兩種不同的標(biāo)識(shí)符。
Python 的關(guān)鍵字如下列出。這些是保留字,不能把它們作為常量或變量或任何其他標(biāo)識(shí)符名稱。 所有Python的關(guān)鍵字僅包含小寫字母。
| and | exec | Not |
| as | finally | or |
| assert | for | pass |
| break | from | |
| class | global | raise |
| continue | if | return |
| def | import | try |
| del | in | while |
| elif | is | with |
| else | lambda | yield |
| except |
|
|
Python不使用大括號(hào)({})來表示的代碼塊類和函數(shù)定義或流程控制。代碼塊由行縮進(jìn),這是嚴(yán)格執(zhí)行表示。
在縮進(jìn)位的數(shù)目是可變的,但該塊內(nèi)的所有語句的縮進(jìn)量必須相同。 例如 -
if True:
print ("True")
else:
print ("False")
if True:
print ("Answer")
print ("True")
else:
print "(Answer")
print ("False")
因此,Python中所有連續(xù)不換行,同樣數(shù)量的空格縮進(jìn)將形成一個(gè)塊。下面的例子有各種各樣的語句塊 -
注意:不要試圖理解其中的邏輯在這個(gè)時(shí)候。只要你明白,即使他們不使用括號(hào)在各個(gè)模塊。
#!/usr/bin/python3
import sys
try:
# open file stream
file = open(file_name, "w")
except IOError:
print ("There was an error writing to", file_name)
sys.exit()
print ("Enter '", file_finish,)
print "' When finished"
while file_text != file_finish:
file_text = raw_input("Enter text: ")
if file_text == file_finish:
# close the file
file.close
break
file.write(file_text)
file.write("\n")
file.close()
file_name = input("Enter filename: ")
if len(file_name) == 0:
print ("Next time please enter something")
sys.exit()
try:
file = open(file_name, "r")
except IOError:
print ("There was an error reading file")
sys.exit()
file_text = file.read()
file.close()
print (file_text)
在Python語句通常使用一個(gè)新行作為結(jié)束。但是,Python 允許使用續(xù)行字符(\)表示讓行可以連續(xù)下去。例如-
total = item_one + \
item_two + \
item_three
語句中包含 [], {}, 或() 括號(hào)內(nèi)不需要使用續(xù)行字符。 例如?
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences."""
哈希符號(hào)(#)這是一個(gè)字符作為注釋的開頭。在#之后到行末的所有字符都是注釋的一部分,Python解釋器會(huì)忽略它們。
#!/usr/bin/python3
# First comment
print ("Hello, Python!") # second comment
Hello, Python!
name = "Madisetti" # This is again comment
# This is a comment. # This is a comment, too. # This is a comment, too. # I said that already.
#!/usr/bin/python3
input("\n\nPress the enter key to exit.")
在這里,“\n\n”是用來顯示實(shí)際行之前創(chuàng)建的兩條新行。一旦用戶按下鍵時(shí),程序就結(jié)束。 這是一個(gè)很好的技巧,以保持控制臺(tái)窗口打開,直到用戶來指定終止應(yīng)用程序運(yùn)行。
import sys; x = 'foo'; sys.stdout.write(x + '\n')
一組單獨(dú)的語句,它們使單一代碼塊化在Python稱為套件。復(fù)雜的語句,如 if, while, def, 和 class 需要一個(gè)標(biāo)頭行和一個(gè)套件。
if expression : suite elif expression : suite else : suite
$ python -h usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -c cmd : program passed in as string (terminates option list) -d : debug output from parser (also PYTHONDEBUG=x) -E : ignore environment variables (such as PYTHONPATH) -h : print this help message and exit [ etc. ]
您也可以在這樣的,它應(yīng)該接受各種選擇的方式編寫腳本。命令行參數(shù)是一個(gè)高級(jí)的主題,當(dāng)你已經(jīng)學(xué)習(xí)了Python概念后,其余部分的內(nèi)容我們將在接下來的章節(jié)中學(xué)習(xí)。