在第一章的時候講解了運算操作符和指派操作符,這章來學習下其他常用操作符。
<b>4.1 基本運算符</b>
<b>4.1.1 比較操作符</b>
<b>操作符</b>
<b>描述</b>
<b>示例</b>
==
相等
>>> 1 == 1 true
!=
不相等
>>> 1 != 1 false
>
大于
>>> 2 > 1 true
<
小于
>>> 2 < 1 false
>=
大于等于
>>> 1 >= 1 true
<=
小于等于
>>> 1 <= 1 true
<b>4.1.2 邏輯運算符</b>
邏輯運算符常用于表達式判斷。
and
與
or
或
not
非
示例:
>>> a = "a"
>>> b = "b"
>>> a and b
'b'
>>> a or b
'a'
>>> a = ""
''
and操作符判斷表達式,如果a和b都為真,傳回b的值,否則傳回a的值。
or操作符也是判斷表達式,如果a和b都為真,傳回a的值,否則傳回b的值。
類似于shell裡的&&和||:[ 'a' == 'b' ] && echo no || echo yes
>>> if not a:
... print "yes"
... else:
... print "no"
...
yes
... else:
no
not操作符用于布爾值(true和false)判斷不為真,與if語句連用。上面是不為真用not,那為真時怎麼弄呢?
>>> if a:
<b>4.1.3 成員運算符</b>
in
在對象裡
not in
不在對象裡
>>> 'a' in 'abc'
true
>>> 'd' in 'abc'
false
>>> lst = ['a','b','c']
>>> 'a' in lst
>>> 'd' in lst
>>> 'a' not in 'abc'
>>> 'd' not in 'abc'
>>> 'd' not in lst
<b>4.1.4 辨別運算符</b>
is
記憶體位址相等
is not
記憶體位址不相等
>>> a = []
>>> b = []
>>> id(a)
139741563903296
>>> id(b)
139741563902144
>>> a is b
>>> a is not b
這裡用到了id()函數,用于擷取對象在記憶體的位址。
<b>4.2 條件判斷</b>
4.2.1 單分支
>>> a = 20
>>> if a < 18:
有時候一個簡單的判斷語句,感覺這樣寫麻煩,有沒有一條指令搞定的。
有的,簡寫if語句:
>>> result = ("yes" if a == 20 else "no")
>>> result
'yes'
>>> type(result)
<type 'str'>
# 有時會看到别人代碼用中括号,意思把結果存儲為一個清單
>>> result = ["yes" if a == 20 else "no"]
['yes']
<type 'list'>
4.2.2 多分支
>>> if a < 18:
... elif a == 20:
... print "other"
4.2.3 pass語句
>>> a = 20
... pass
pass語句作用是不執行目前代碼塊,與shell中的冒号做作用一樣。
<b>部落格位址:http://lizhenliang.blog.51cto.com and https://yq.aliyun.com/u/lizhenliang</b>
qq群:323779636(shell/python運維開發群)
<b>4.3 循環語句</b>
<b> 4.3.1 for</b>
1)疊代對象
周遊字元串,每個字元當做單個周遊:
>>> for i in "abc":
... print i
a
b
c
使用range()函數生成一個數字序列清單,并周遊:
>>> for i in range(1,5):
1
2
3
4
回顧下第三章講的周遊字典:
>>> d = {'a':1, 'b':2, 'c':3}
>>> for i in d.iteritems():
... print "%s:%s" %(i[0],i[1])
a:1
c:3
b:2
2)嵌套循環
逐個循環判斷外層清單裡元素是否存在内層清單:
>>> for i in range(1,6):
... for x in range(3,8):
... if i == x:
... print i
5
3)簡寫語句
簡寫for語句:
>>> result = (x for x in range(5))
<generator object <genexpr> at 0x030a4fd0>
<type 'generator'>
說明:在這裡用小括号,會生成一個生成器,在這裡知道下就可以了,不過多講解,後面會專門生成器用途。
# 同樣用中括号會以清單存儲
>>> result = [ x for x in range(5)]
[0, 1, 2, 3, 4]
for和if語句寫一行:
>>> result = [ x for x in range(5) if x % 2 == 0]
[0, 2, 4]
<b> 4.3.2 while</b>
文法:
while 表達式:
執行語句...
1)輸出序列
當條件滿足時,停止循環:
>>> while count < 5:
... print count
... count += 1
2)死循環
>>> import time
>>> i = 1
>>> while true:
... print i
... i += 1
... time.sleep(0.5)
...... # 會一直循環,直到海枯石爛,天荒地老...
注意:當表達式值為true或者非零時,都會一直循環。
<b>4.3.3 continue和break語句</b>
continue當滿足條件時,跳出本次循環。
break當滿足條件時,跳出所有循環。
for和while用法一樣。
1)基本使用
滿足條件跳出目前循環:
#!/usr/bin/env python
for i in range(1,6):
if i == 3:
continue
else:
print i
# python test.py
count = 0
while count < 5:
count += 1
if count == 3:
print count
滿足條件終止循環:
break
2)輸入錯誤次數超過三次退出
例如:提示使用者輸入名字,如果名字是xiaoming輸入正确退出,否則一直提示重新輸入,直到三次退出。
while 1:
if count < 3:
name = raw_input("please input your name: ").strip() # .strip()去除首尾空格
if len(name) == 0:
print "input can not be empty!"
count += 1
continue
elif name == "xiaoming":
print "ok."
break
else:
print "name input error, please input again!"
print "error three times, exit!"
<b> 4.3.4 else語句</b>
else語句會在循環正常執行完才執行。在for循環用法也一樣。
>>> count = 0
... print count
... count += 1
... print "end"
end
>>> count = 0
... break