語法
if語句
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
可能會有零到多個elif部分,else是可選的。關鍵字‘elif‘是‘else if’的縮寫,可避免過深的縮進。 if ... elif ... elif序列用於替代其它語言中的switch或case語句。python中沒有case語言,可以考慮用字典或者elif語句替代。
深入條件控制
while和if語句中使用的條件可以使用比較,也可包含任意的操作。
比較操作符 in 和 not in判斷值是否包含在序列。操作符 is 和 is not 比較兩個對象是否相同,適用於可變對象。所有的比較操作符具有相同的優先級,低於所有的數值操作符。
比較操作可以串聯。例如 a < b == c 判斷是否 a 小於 b 並且 b 等於 c 。
比較操作可以通過邏輯操作符 and 和 or 組合,比較的結果可以用 not 來取反。這些操作符的優先級低於比較操作符,其中not 具有最高的優先級,or 優先級最低,所以 A and not B or C 等於 (A and (notB)) or C。
邏輯操作符 and 和 or 也稱作短路操作符:執行順序從左向右,一旦結果確定就停止。例如AandBandC中,如果 A 和 C 爲真而 B 爲假,不會解析 C。
可以把比較或其它邏輯表達式的返回值賦給變量,例如:
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
注意Python與C 不同,在表達式內部不能賦值,避免 C 程序中常見的錯誤:該用==時誤用了=操作符。
選擇
用turtle繪製一個圓,從從左邊滾動到右邊,再從右邊滾動到左邊。
代碼:
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
# 技術支持 釘釘羣:21745728(可以加釘釘pythontesting邀請加入)
# qq羣:144081101 591302926 567351477
# CreateDate: 2018-6-12
# bounce.py
# Bounce the turtle.
from turtle import *
def move(distance):
"""Move forward, reversing direction at right side."""
forward(distance)
if xcor() > 320:
setheading(180)
def main():
shape("circle")
penup()
speed(0)
for _ in range(100):
move(10)
exitonclick()
main()
方法 | 功能 |
---|---|
shape(name) | 命名爲name |
speed(value) | 速度設置爲1(慢)到10(最快)之間的value,其中0爲“瞬間 |
xcor() | 返回當前x座標 |
ycor() | 返回當前y座標。 |
position() | 返回當前座標 (x, y) |
heading() | 返回當前方向 |
towards(x, y) | 從當前位置到(x, y)的方向。 |
distance(x, y) | 從當前位置到(x, y)的距離。 |
複雜的選擇
代碼:
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
# 技術支持 釘釘羣:21745728(可以加釘釘pythontesting邀請加入)
# qq羣:144081101 591302926 567351477
# CreateDate: 2018-6-12
# mycircle.py
# Mimic circle() with adaptive radius.
def mycircle(radius):
"""Draw circle as polygon."""
if radius < 20:
sides = 10
elif radius < 100:
sides = 30
else:
sides = 50
polygon(sides, 6.28*radius/sides)
隨機
代碼:
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
# 技術支持 釘釘羣:21745728(可以加釘釘pythontesting邀請加入)
# qq羣:144081101 591302926 567351477
# CreateDate: 2018-6-12
# randomwalk.py
# Draw path of a random walk.
from turtle import *
from random import randrange
def random_move(distance):
"""Take random step on a grid."""
left(randrange(0, 360, 90))
forward(distance)
def main():
speed(0)
while abs(xcor()) < 200 and abs(ycor()) < 200:
random_move(10)
exitonclick()
main()
方法 | 功能 |
---|---|
random() | 隨機值x,0 ≤ x < 1(不一定是整數)。 |
uniform(a, b) | 隨機數n,其中a≤n≤b。 |
randint(a, b) | 隨機整數n,其中a≤n≤b。 |
randrange(start, stop, step) | range(start, stop, step)的隨機整數 |
編碼風格
建議遵守PEP8,高可讀性,部分要點如下:
使用4空格縮進,而非tab。
每行不超過79個字符。
使用空行分隔函數和類,以及函數中的大代碼塊。
可能的話,註釋佔一行
使用文檔字符串
操作符前後有空格,逗號後有空格,但是括號兩側無空格。如: a = f(1, 2) + g(3, 4) 。
統一函數和類命名。類名用首字母大寫的駝峯式,比如CamelCase。函數和方法名用小寫和下劃線組成:lower_case_with_underscores。類中使用self。
國際化時不要使用花哨的編碼。
另autopep8能把代碼調整爲符合pep8,pep8能檢查是否符合pep8,mypy:靜態類型檢查等推薦使用。更多的python規範外部庫
參考資料
- 討論qq羣144081101 591302926 567351477 釘釘免費羣21745728
- 本文最新版本地址
- 本文涉及的python測試開發庫 謝謝點贊!
- 本文相關海量書籍下載
- 本文源碼地址