1.variables

變數

【資料型態】

Python內建型態的資料型態有以下幾種:

數值型態(Numeric type) - int, long, float, bool, complex

字串型態(String type)

容器型態(Container type) - list, set, dict, tuple


【命名規則】

名稱只能由大小寫字母、_底線、中文、數字組成,英文字母大小寫式為不同字元。

第一個字母必須為:大小寫字母、_底線、中文。

變數名稱不能與Python內建的保留字相同,例如:and or not def print ...。

【保留字】

acos and array asin assert atan

break class close continue cos Data def del

e elif else except exec exp fabs float finally floor for from

global if import in input int is lambda log log10 not open or

pass pi print raise range return sin sqrt tan try type while write zeros

資料型態】

數值、布林、字串。

數值:整數int、浮點數float。

布林bool:True、False。(int(True)=1、int(False)=0)

字串:可以使用單引號或雙引號,字串要內含引號時,可以使用另一個引號來給值。

Ex: str = "林大明的報告內有'單引號" 或 str = '林大明的報告內有"雙引號'

【資料轉換】

int():轉為整數

str():轉為字串

bool():轉為布林值

float():轉為浮點數

※要轉換的內容不可以是串列list※

Python

【跳脫字元表】

【兩數交換的方法】

若a=10,b=20則:

《引入第三個變數》

c=a #c=10,a=10,b=20

a=b #a=20,b=20,c=10

b=c #b=10,a=20

《加減法》

a=a+b #a=30,b=20

b=a-b #b=10,a=30

a=a-b #a=20,b=10

《Python指令法》

a,b = b,a