Python 基礎語法 1

2021-07-02 17:19:42 字數 4831 閱讀 6133

通過指令碼引數呼叫直譯器開始執行指令碼,直到指令碼執行完畢。當指令碼執行完成後,直譯器不再有效。

讓我們寫乙個簡單的python指令碼程式。所有python檔案將以.py為副檔名。將以下的源**拷貝至test.py檔案中。

print

"hello, python!"

;

這裡,假設你已經設定了python直譯器path變數。使用以下命令執行程式:

$ python test

.py

輸出結果:

hello

,python

!

讓我們嘗試另一種方式來執行python指令碼。修改test.py檔案,如下所示:

#!/usr/bin/python

print

"hello, python!"

;

這裡,假定您的python直譯器在/usr/bin目錄中,使用以下命令執行指令碼:

$ chmod 

+x test

.py

# 指令碼檔案新增可執行許可權$./

test

.py

輸出結果:

hello

,python

!

在python裡,識別符號有字母、數字、下劃線組成。

在python中,所有識別符號可以包括英文、數字以及下劃線(_),但不能以數字開頭。

python中的識別符號是區分大小寫的。

以下劃線開頭的識別符號是有特殊意義的。以單下劃線開頭(_foo)的代表不能直接訪問的類屬性,需通過類提供的介面進行訪問,不能用"from *** import *"而匯入;

以雙下劃線開頭的(__foo)代表類的私有成員;以雙下劃線開頭和結尾的(__foo__)代表python裡特殊方法專用的標識,如__init__()代表類的建構函式。

下面的列表顯示了在python中的保留字。這些保留字不能用作常數或變數,或任何其他識別符號名稱。

所有python的關鍵字只包含小寫字母。

andexec

notassert

finally

orbreak

forpass

class

from

print

continue

global

raise

defif

return

delimport

tryelif

inwhile

else

iswith

except

lambda

yield

學習python與其他語言最大的區別就是,python的**塊不使用大括號({})來控制類,函式以及其他邏輯判斷。python最具特色的就是用縮進來寫模組。

縮排的空白數量是可變的,但是所有**塊語句必須包含相同的縮排空白數量,這個必須嚴格執行。如下所示:

if

true

:print

"true"

else

:print

"false"

以下**將會執行錯誤:

iftrue

:print

"answer"

print

"true"

else

:print

"answer"

print

"false"

因此,在python的**塊中必須使用相同數目的行首縮排空格數。

以下例項包含了相同數目的行首縮排**語句塊的例子:

#!/usr/bin/python

import

systry

:# 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

=raw_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語句中一般以新行作為為語句的結束符。

但是我們可以使用斜槓( \)將一行的語句分為多行顯示,如下所示:

total 

=item_one +\

item_two +\

item_three

語句中包含, {} 或 () 括號就不需要使用多行連線符。如下例項:

days =[

'monday'

,'tuesday'

,'wednesday'

,'thursday'

,'friday'

]

python 接收單引號(' ),雙引號(" ),三引號(''' """) 來表示字串,引號的開始與結束必須的相同型別的。

其中三引號可以由多行組成,編寫多行文字的快捷語法,常用語文件字串,在檔案的特定地點,被當做注釋。

word 

='word'

sentence

="this is a sentence."

paragraph

="""this is a paragraph. it is

made up of multiple lines and sentences."""

python中單行注釋採用 # 開頭。

python沒有塊注釋,所以現在推薦的多行注釋也是採用的 #比如:

#!/usr/bin/python

# 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.

函式之間或類的方法之間用空行分隔,表示一段新的**的開始。類和函式入口之間也用一行空行分隔,以突出函式入口的開始。

空行與**縮排不同,空行並不是python語法的一部分。書寫時不插入空行,python直譯器執行也不會出錯。但是空行的作用在於分隔兩段不同功能或含義的**,便於日後**的維護或重構。

記住:空行也是程式**的一部分。

下面的程式在按回車鍵後就會等待使用者輸入:

#!/usr/bin/python

raw_input

("\n\npress the enter key to exit."

)

以上**中 ,"\n\n"在結果輸出前會輸出兩個新的空行。一旦使用者按下鍵時,程式將退出。

python可以在同一行中使用多條語句,語句之間使用分號(;)分割,以下是乙個簡單的例項:

import

sys;x =

'foo'

;sys

.stdout

.write(x

+'\n'

)

縮排相同的一組語句構成乙個**塊,我們稱之**組。

像if、while、def和class這樣的復合語句,首行以關鍵字開始,以冒號( : )結束,該行之後的一行或多行**構成**組。

我們將首行及後面的**組稱為乙個子句(clause)。

如下例項:

if

expression

:suite

elif

expression

:suite

else

:suite

Python基礎 1(語法基礎)

1 資料交換 c語言中 temp x x y y temp python中 x,y y,x 2 典型的python檔案結構 1 usr bin env python 1 起始行 23 this is a test module 2 模組文件45 import sys 3 模組匯入 6importos...

Python基礎語法1

常量與變數 常量 需要用物件的方法來建立乙個模組 class const object class consterror typeerror pass def setattr self,name,value if self.dict has key name raise self.consterro...

Python語法基礎(1)

識別符號 識別符號就是變數 常量 函式 屬性 類 模組和包等有程式設計師指定的名字。構成標識的字元具有一定的規範,規則如下 1 區分大小寫,myname與myname是兩個不同的識別符號 2 首字元可以是下畫線 或字母,但不能用數字 3 除首字元以外其他字元,可以是下畫線 字母和數字 4 關鍵字不能...