第1章python 基礎
python 3.3
.2(v3.
3.2:d047928ae3f6, may 16
2013,00
:06:53
)[msc v.
1600
64 bit
(amd64)
] on win32
,"credits"
or"license()"
for more information.
>>
>2+
24>>
>
>>
>
int(
4.555
)>>
>
float(4
)>>
>
str(
1)
>>
>
'alice'
+'bob'
'alicebob'
*操作符用於乙個字串值和乙個整型值時,它變成了「字串複製」操作符。
>>
>
'alice'*5
'alicealicealicealicealice'
賦值語句
>>
> spam =
40>>
> spam
40>>
> eggs =
2>>
> spam + eggs
42
變數名
你可以給變數取任何名字,只要它遵守以下 3 條規則:
1.只能是乙個詞。
2.只能包含字母、數字和下劃線。
3.不能以數字開頭。
本書的變數名使用了駝峰形式,沒有用下劃線。好的變數名描述了它包含的資料。
第2章控制流
==等於
!=不等於
《小於>大於
<=小於等於
>=大於等於
>>
>2+
2==4and
not2+2
==5and2*2
==2+2
true
while-else語句
count =
0while count <=5:
count +=
1if count ==3:
break
print
("loop"
,count)
else
:print
("迴圈正常執行完啦"
)print
("-----out of while loop ------"
)#當while中有break語句打斷時,else不會執行。
#當while中沒有被break語句打斷時,else就會執行。
import random, sys, os, math
用 sys.exit()提前結束程式。
第3章函式
def 語句和引數:
def
hello
(name)
:print
('hello '
+ name)
hello(
'alice'
)hello(
'bob'
)
返回值和 return 語句:
return 關鍵字。
函式應該返回的值或表示式。
區域性和全域性作用域:
全域性作用域中的**不能使用任何區域性變數;
但是,區域性作用域可以訪問全域性變數;
乙個函式的區域性作用域中的**,不能使用其他區域性作用域中的變數。
如果在不同的作用域中,你可以用相同的名字命名不同的變數。也就是說,可
以有乙個名為 spam 的區域性變數,和乙個名為 spam 的全域性變數。
global 語句
def
spam()
:global eggs
eggs =
'spam'
eggs =
'gobal'
spam(
)print
(eggs)
def
spam()
:global eggs
eggs =
'spam'
# this is the global
defbacon()
: eggs =
'bacon'
# this is a local
defham()
:print
(eggs)
# this is the global
eggs =
42# this is the global
spam(
)print
(eggs
異常處理
def
spam
(divideby)
:try
:return
42/ divideby
except zerodivisionerror:
print
('error: invalid argument.'
)print
(spam(2)
)print
(spam(12)
)print
(spam(0)
)print
(spam(1)
)
def
spam
(divideby)
:return
42/ divideby
try:
print
(spam(2)
)print
(spam(12)
)print
(spam(0)
)print
(spam(1)
)except zerodivisionerror:
print
('error: invalid argument.'
)
《python程式設計快速上手》第9章 作業9 8 3
本來想偷懶不做這題,感覺好麻煩,就上網找找答案。結果沒有滿意的,還是自己寫乙個吧。相較於一位網上同學的答案,此 亮點在 03d 和zip 函式的使用,還有任務拆分清晰。做了個把小時的,還算滿意,就作為第一篇部落格,激勵下自己繼續努力。如有錯誤,請指出以相互學習,多謝!python3 complete...
python程式設計快速上手 第11章實踐專案答案
11.3 2048 2048是乙個簡單的遊戲,通過箭頭向上 下 左 右滑動,讓滑塊合併。實際上,你可以通過一遍遍的重複 上下左右 模式,獲得相當高的分數。編寫乙個程式,開啟上的遊戲,不斷傳送上下左右按鍵,自動玩遊戲 from selenium import webdriver from seleni...
python程式設計快速上手 第4章實踐專案答案
4.10.1逗號 假定有下面這樣的列表 def joinspam spam i len spam 1 spam i and str spam i str1 print str1.join spam 4.10.2 字元圖網格 假定有乙個巢狀列表,記憶體列表的每乙個值都是包含乙個字元的字串,像這樣 gr...