Python基础教程(三):运算符、条件语句
Python 運算符
什么是運算符?
本章節主要說明Python的運算符。舉個簡單的例子 4 +5 = 9 。 例子中,4和5被稱為操作數,"+"號為運算符。
Python語言支持以下類型的運算符:
- 算術運算符
- 比較(關系)運算符
- 賦值運算符
- 邏輯運算符
- 位運算符
- 成員運算符
- 身份運算符
- 運算符優先級
接下來讓我們一個個來學習Python的運算符。
Python算術運算符
以下假設變量a為10,變量b為20:
| 運算符 | 描述 | 實例 |
| + | 加 - 兩個對象相加 | a + b 輸出結果 30 |
| - | 減 - 得到負數或是一個數減去另一個數 | a - b 輸出結果 -10 |
| * | 乘 - 兩個數相乘或是返回一個被重復若干次的字符串 | a * b 輸出結果 200 |
| / | 除 - x除以y | b / a 輸出結果 2 |
| % | 取模 - 返回除法的余數 | b % a 輸出結果 0 |
| ** | 冪 - 返回x的y次冪 | a**b 為10的20次方, 輸出結果 100000000000000000000 |
| // | 取整除 - 返回商的整數部分 | 9//2 輸出結果 4 , 9.0//2.0 輸出結果 4.0 |
以下實例演示了Python所有算術運算符的操作:
#!/usr/bin/python
?
a = 21
b = 10
c = 0
?
c = a + b
print "Line 1 - Value of c is ", c
?
c = a - b
print "Line 2 - Value of c is ", c
?
c = a * b
print "Line 3 - Value of c is ", c
?
c = a / b
print "Line 4 - Value of c is ", c
?
c = a % b
print "Line 5 - Value of c is ", c
?
a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c
?
a = 10
b = 5
c = a//b
print "Line 7 - Value of c is ", c
以上實例輸出結果:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2
Python比較運算符
以下假設變量a為10,變量b為20:
| 運算符 | 描述 | 實例 |
| == | 等于 - 比較對象是否相等 | (a == b) 返回 False。 |
| != | 不等于 - 比較兩個對象是否不相等 | (a != b) 返回 true. |
| <>? | 不等于 - 比較兩個對象是否不相等 | (a <> b) 返回 true。這個運算符類似 != 。 |
| >? | 大于 - 返回x是否大于y | (a > b) 返回 False。 |
| <? | 小于 - 返回x是否小于y。所有比較運算符返回1表示真,返回0表示假。這分別與特殊的變量True和False等價。注意,這些變量名的大寫。 | (a < b) 返回 true。 |
| >= | 大于等于 - 返回x是否大于等于y。 | (a >= b) 返回 False。 |
| <= | 小于等于 - 返回x是否小于等于y。 | (a <= b) 返回 true。 |
以下實例演示了Python所有比較運算符的操作:
#!/usr/bin/python
?
a = 21
b = 10
c = 0
?
if ( a == b ):
?? print "Line1 - a is equal to b"
else:
?? print "Line1 - a is not equal to b"
?
if ( a != b ):
?? print "Line2 - a is not equal to b"
else:
?? print "Line2 - a is equal to b"
?
if ( a <> b ):
?? print "Line3 - a is not equal to b"
else:
?? print "Line3 - a is equal to b"
?
if ( a < b ):
?? print "Line4 - a is less than b"
else:
?? print "Line4 - a is not less than b"
?
if ( a > b ):
?? print "Line5 - a is greater than b"
else:
?? print "Line5 - a is not greater than b"
?
a = 5;
b = 20;
if ( a <= b ):
?? print "Line6 - a is either less than or equal to?b"
else:
?? print "Line6 - a is neither less than nor equal to?b"
?
if ( b >= a ):
?? print "Line7 - b is either greater than? or equal tob"
else:
?? print "Line7 - b is neither greater than? nor equalto b"
以上實例輸出結果:
Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b
Python賦值運算符
以下假設變量a為10,變量b為20:
| 運算符 | 描述 | 實例 |
| = | 簡單的賦值運算符 | c = a + b 將 a + b 的運算結果賦值為 c |
| += | 加法賦值運算符 | c += a 等效于 c = c + a |
| -= | 減法賦值運算符 | c -= a 等效于 c = c - a |
| *= | 乘法賦值運算符 | c *= a 等效于 c = c * a |
| /= | 除法賦值運算符 | c /= a 等效于 c = c / a |
| %= | 取模賦值運算符 | c %= a 等效于 c = c % a |
| **= | 冪賦值運算符 | c **= a 等效于 c = c ** a |
| //= | 取整除賦值運算符 | c //= a 等效于 c = c // a |
以下實例演示了Python所有賦值運算符的操作:
#!/usr/bin/python
?
a = 21
b = 10
c = 0
?
c = a + b
print "Line 1 - Value of c is ", c
?
c += a
print "Line 2 - Value of c is ", c
?
c *= a
print "Line 3 - Value of c is ", c
?
c /= a
print "Line 4 - Value of c is ", c
?
c? = 2
c %= a
print "Line 5 - Value of c is ", c
?
c **= a
print "Line 6 - Value of c is ", c
?
c //= a
print "Line 7 - Value of c is ", c
以上實例輸出結果:
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
Python位運算符
按位運算符是把數字看作二進制來進行計算的。Python中的按位運算法則如下:
| 運算符 | 描述 | 實例 |
| & | 按位與運算符 | (a & b) 輸出結果 12 ,二進制解釋: 0000 1100 |
| | | 按位或運算符 | (a | b) 輸出結果 61 ,二進制解釋: 0011 1101 |
| ^ | 按位異或運算符 | (a ^ b) 輸出結果 49 ,二進制解釋: 0011 0001 |
| ~ | 按位取反運算符 | (~a ) 輸出結果 -61 ,二進制解釋: 1100 0011, 在一個有符號二進制數的補碼形式。 |
| <<? | 左移動運算符 | a << 2 輸出結果 240 ,二進制解釋: 1111 0000 |
| >>? | 右移動運算符 | a >> 2 輸出結果 15 ,二進制解釋: 0000 1111 |
以下實例演示了Python所有位運算符的操作:
#!/usr/bin/python
?
a = 60??????????? #60 = 0011 1100
b = 13??????????? #13 = 0000 1101
c = 0
?
c = a & b;???????# 12 = 0000 1100
print "Line 1 - Value of c is ", c
?
c = a | b;??????? #61 = 0011 1101
print "Line 2 - Value of c is ", c
?
c = a ^ b;??????? #49 = 0011 0001
print "Line 3 - Value of c is ", c
?
c = ~a;????????? ?# -61 = 1100 0011
print "Line 4 - Value of c is ", c
?
c = a << 2;??????# 240 = 1111 0000
print "Line 5 - Value of c is ", c
?
c = a >> 2;??????# 15 = 0000 1111
print "Line 6 - Value of c is ", c
以上實例輸出結果:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
Python邏輯運算符
Python語言支持邏輯運算符,以下假設變量a為10,變量b為20:
| 運算符 | 描述 | 實例 |
| and | 布爾"與" - 如果x為False,x and y返回False,否則它返回y的計算值。 | (a and b) 返回 true。 |
| or | 布爾"或" - 如果x是True,它返回True,否則它返回y的計算值。 | (a or b) 返回 true。 |
| not | 布爾"非" - 如果x為True,返回False。如果x為False,它返回True。 | not(a and b) 返回 false。 |
以下實例演示了Python所有邏輯運算符的操作:
#!/usr/bin/python
?
a = 10
b = 20
c = 0
?
if ( a and b ):
?? print "Line1 - a and b are true"
else:
?? print "Line1 - Either a is not true or b is not true"
?
if ( a or b ):
?? print "Line2 - Either a is true or b is true or both are true"
else:
?? print "Line2 - Neither a is true nor b is true"
?
?
a = 0
if ( a and b ):
?? print "Line3 - a and b are true"
else:
?? print "Line3 - Either a is not true or b is not true"
?
if ( a or b ):
?? print "Line4 - Either a is true or b is true or both are true"
else:
?? print "Line4 - Neither a is true nor b is true"
?
if not( a and b ):
?? print "Line5 - Either a is not true or b is? nottrue or both are not true"
else:
?? print "Line5 - a and b are true"
以上實例輸出結果:
Line 1 - a and b are true
Line 2 - Either a is true or b is true or both are true
Line 3 - Either a is not true or b is not true
Line 4 - Either a is true or b is true or both are true
Line 5 - Either a is not true or b is? not true or both are not true
Python成員運算符
除了以上的一些運算符之外,Python還支持成員運算符,測試實例中包含了一系列的成員,包括字符串,列表或元組。
| 運算符 | 描述 | 實例 |
| in | 如果在指定的序列中找到值返回True,否則返回False。 | x 在 y序列中 , 如果x在y序列中返回True。 |
| not in | 如果在指定的序列中沒有找到值返回True,否則返回False。 | x 不在 y序列中 , 如果x不在y序列中返回True。 |
以下實例演示了Python所有成員運算符的操作:
#!/usr/bin/python
?
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
?
if ( a in list ):
?? print "Line1 - a is available in the given list"
else:
?? print "Line1 - a is not available in the given list"
?
if ( b not in list ):
?? print "Line2 - b is not available in the given list"
else:
?? print "Line2 - b is available in the given list"
?
a = 2
if ( a in list ):
?? print "Line3 - a is available in the given list"
else:
?? print "Line3 - a is not available in the given list"
以上實例輸出結果:
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list
Python身份運算符
身份運算符用于比較兩個對象的存儲單元
| 運算符 | 描述 | 實例 |
| is | is是判斷兩個標識符是不是引用自一個對象 | x is y, 如果 id(x) 等于 id(y) , is 返回結果 1 |
| is not | is not是判斷兩個標識符是不是引用自不同對象 | x is not y, 如果 id(x) 不等于 id(y). is not 返回結果 1 |
以下實例演示了Python所有身份運算符的操作:
#!/usr/bin/python
?
a = 20
b = 20
?
if ( a is b ):
?? print "Line1 - a and b have same identity"
else:
?? print "Line1 - a and b do not have same identity"
?
if ( id(a) == id(b) ):
?? print "Line2 - a and b have same identity"
else:
?? print "Line2 - a and b do not have same identity"
?
b = 30
if ( a is b ):
?? print "Line3 - a and b have same identity"
else:
?? print "Line3 - a and b do not have same identity"
?
if ( a is not b ):
?? print "Line4 - a and b do not have same identity"
else:
?? print "Line4 - a and b have same identity"
以上實例輸出結果:
Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity
Python運算符優先級
以下表格列出了從最高到最低優先級的所有運算符:
| 運算符 | 描述 |
| ** | 指數 (最高優先級) |
| ~ + - | 按位翻轉, 一元加號和減號 (最后兩個的方法名為 +@ 和 -@) |
| * / % // | 乘,除,取模和取整除 |
| + - | 加法減法 |
| >> << | 右移,左移運算符 |
| & | 位 'AND' |
| ^ | | 位運算符 |
| <= < > >= | 比較運算符 |
| <> == != | 等于運算符 |
| = %= /= //= -= += *= **= | 賦值運算符 |
| is is not | 身份運算符 |
| in not in | 成員運算符 |
| not or and | 邏輯運算符 |
以下實例演示了Python所有運算符優先級的操作:
#!/usr/bin/python
?
a = 20
b = 10
c = 15
d = 5
e = 0
?
e = (a + b) * c / d??????#( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ",? e
?
e = ((a + b) * c) / d????# (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ",? e
?
e = (a + b) * (c / d);???# (30) * (15/5)
print "Value of (a + b) * (c / d) is ",? e
?
e = a + (b * c) / d;?????#? 20 + (150/5)
print "Value of a + (b * c) / d is ",? e
以上實例輸出結果:
Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50
?
?
Python 條件語句
Python條件語句是通過一條或多條語句的執行結果(True或者False)來決定執行的代碼塊。
可以通過下圖來簡單了解條件語句的執行過程:
Python程序語言指定任何非0和非空(null)值為true,0 或者 null為false。
Python 編程中 if 語句用于控制程序的執行,基本形式為:
if 判斷條件: ??? 執行語句…… else: ??? 執行語句……其中"判斷條件"成立時(非零),則執行后面的語句,而執行內容可以多行,以縮進來區分表示同一范圍。
else 為可選語句,當需要在條件不成立時執行內容則可以執行相關語句,具體例子如下:
#!/usr/bin/python # -*- coding: UTF-8 -*- ? # 例1:if 基本用法 ? flag = False name = 'luren' if name == 'python':???????? # 判斷變量否為'python' ??? flag = True????????? # 條件成立時設置標志為真 ??? print 'welcome boss'??? # 并輸出歡迎信息 else: ??? print name????????????? # 條件不成立時輸出變量名稱輸出結果為:
>>> luren????????????????????? # 輸出結果if 語句的判斷條件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)來表示其關系。
當判斷條件為多個值是,可以使用以下形式:
if 判斷條件1: ??? 執行語句1…… elif 判斷條件2: ??? 執行語句2…… elif 判斷條件3: ??? 執行語句3…… else: ??? 執行語句4……實例如下:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 例2:elif用法 ? num = 5???? if num == 3:??????????? # 判斷num的值 ??? print 'boss'??????? elif num == 2: ??? print 'user' elif num == 1: ??? print 'worker' elif num < 0:?????????? # 值小于零時輸出 ??? print 'error' else: ??? print 'roadman'???? # 條件均不成立時輸出輸出結果為:
>>> roadman??????????? # 輸出結果由于 python 并不支持 switch 語句,所以多個條件判斷,只能用 elif 來實現,如果判斷需要多個條件需同時判斷時,可以使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的情況下,判斷條件才成功。
#!/usr/bin/python # -*- coding: UTF-8 -*- ? # 例3:if語句多個條件 ? num = 9 if num >= 0 and num <= 10:??? # 判斷值是否在0~10之間 ??? print 'hello' >>> hello????????????? # 輸出結果 ? num = 10 if num < 0 or num > 10:??? # 判斷值是否在小于0或大于10 ??? print 'hello' else: ??????? print 'undefine' >>> undefine?????????? # 輸出結果 ? num = 8 # 判斷值是否在0~5或者10~15之間 if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):??? ????print 'hello' else: ??? print 'undefine' >>> undefine?????????? # 輸出結果當if有多個條件時可使用括號來區分判斷的先后順序,括號中的判斷優先執行,此外 and 和 or 的優先級低于>(大于)、<(小于)等判斷符號,即大于和小于在沒有括號的情況下會比與或要優先判斷。
簡單的語句組
你也可以在同一行的位置上使用if條件判斷語句,如下實例:
#!/usr/bin/python # -*- coding: UTF-8 -*- var = 100 ? if ( var? == 100 ) : print "變量 var 的值為100" ? print "Good bye!"以上代碼執行輸出結果如下:
變量 var 的值為100 Good bye!出處:http://www.runoob.com/python/python-tutorial.html總結
以上是生活随笔為你收集整理的Python基础教程(三):运算符、条件语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python基础教程(二):基础语法、变
- 下一篇: Python基础教程(四):循环语句