phython學習筆記

在w3c上學習的筆記:

1.Python的代碼塊不使用大括號({})來控制類,函數以及其他邏輯判斷。python最具特色的就是用縮進來寫模塊。

2.Python語句中一般以新行作爲爲語句的結束符。但是我們可以使用斜槓( \)將一行的語句分爲多行顯示。

例如:

 total = item_one + \ 
        item_two + \
        item_three

3.字符串用單引號雙引號三引號表示,三引號可以中間內容換行,註釋用#開頭,無塊註釋。

4.函數或方法之間用空行表示,表示一個新函數或方法的開始。

5.Python中的變量不需要聲明,變量的賦值操作既是變量聲明和定義的過程。

6.Python允許你同時爲多個變量賦。例如:

a = b = c = 1 # 爲a/b/c賦值1
a, b, c = 1, 2, "john" # 爲a/b/c賦值1,2,john

7.數據類型裏值得注意的是:

<1>列表——集合類的數據類型,用“[]”標識例如:

list = [ 'abcd', 768, 2.36, 33]

print list # 輸出整個列表

print list[0] #    abcd

print list[1:3] # 輸出 768和2.36

print list[2:] # 輸出2.36後所有的內容

print list * 2 # 輸出列表兩次

<2>元祖—— 類似於list的列表,用“()”標識,不同於list的是不可以更新,只讀,複製後不可改變。

<3>python元字典,列表是有序對象集合,字典是無序的對象集合。

區別在於:字典當中的元素是通過鍵來存取的。用“{}”標識。

例如:

test[2] = 'hello'

test1['name'] = charles

test2['id': 23, 'user': 'user01']

print test[2]

print test1['name']

print test2 #完整輸出元字典

print test2.keys() # 輸出所有鍵

print test2.values() # 輸出所有值

7.運算符:

特殊的幾個:冪運算:  **         取整除  //

邏輯運算:

if (a and b) :

   print "a and b"

if (a or b) :

   print "a or b"

成員運算:

if (a in list) :

   print "a in list"

if (a not in list) :

   print "a not in list"

身份運算符:is ,is not 判斷兩個標識符是否引自同一個對象,結果返回1

8.條件語句:

if :

elif :

9.循環語句:for   while   

while……else……

例如:

count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"
無限循環可以使用 ctrl + C  結束




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章