国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python 運算符

瀏覽:92日期:2022-08-07 16:35:41

什么是運算符?

本章節(jié)主要說明Python的運算符。舉個簡單的例子 4 +5 = 9 。 例子中,4和5被稱為操作數(shù),"+"號為運算符。

Python語言支持以下類型的運算符:

算術(shù)運算符 比較(關(guān)系)運算符 賦值運算符 邏輯運算符 位運算符 成員運算符 身份運算符 運算符優(yōu)先級

接下來讓我們一個個來學習Python的運算符。

Python算術(shù)運算符

以下假設(shè)變量a為10,變量b為20:

運算符 描述 實例+ 加 - 兩個對象相加 a + b 輸出結(jié)果 30- 減 - 得到負數(shù)或是一個數(shù)減去另一個數(shù) a - b 輸出結(jié)果 -10* 乘 - 兩個數(shù)相乘或是返回一個被重復(fù)若干次的字符串 a * b 輸出結(jié)果 200/ 除 - x除以y b / a 輸出結(jié)果 2% 取模 - 返回除法的余數(shù) b % a 輸出結(jié)果 0** 冪 - 返回x的y次冪 a**b 為10的20次方, 輸出結(jié)果 100000000000000000000// 取整除 - 返回商的整數(shù)部分 9//2 輸出結(jié)果 4 , 9.0//2.0 輸出結(jié)果 4.0

以下實例演示了Python所有算術(shù)運算符的操作:

#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", cc = a - bprint "Line 2 - Value of c is ", c c = a * bprint "Line 3 - Value of c is ", c c = a / bprint "Line 4 - Value of c is ", c c = a % bprint "Line 5 - Value of c is ", ca = 2b = 3c = a**b print "Line 6 - Value of c is ", ca = 10b = 5c = a//b print "Line 7 - Value of c is ", c

嘗試一下 »

以上實例輸出結(jié)果:

Line 1 - Value of c is 31Line 2 - Value of c is 11Line 3 - Value of c is 210Line 4 - Value of c is 2Line 5 - Value of c is 1Line 6 - Value of c is 8Line 7 - Value of c is 2Python比較運算符

以下假設(shè)變量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/pythona = 21b = 10c = 0if ( a == b ): print "Line 1 - a is equal to b"else: print "Line 1 - a is not equal to b"if ( a != b ): print "Line 2 - a is not equal to b"else: print "Line 2 - a is equal to b"if ( a <> b ): print "Line 3 - a is not equal to b"else: print "Line 3 - a is equal to b"if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b"if ( a > b ): print "Line 5 - a is greater than b"else: print "Line 5 - a is not greater than b"a = 5;b = 20;if ( a <= b ): print "Line 6 - a is either less than or equal to b"else: print "Line 6 - a is neither less than nor equal to b"if ( b >= a ): print "Line 7 - b is either greater than or equal to b"else: print "Line 7 - b is neither greater than nor equal to b"

以上實例輸出結(jié)果:

Line 1 - a is not equal to bLine 2 - a is not equal to bLine 3 - a is not equal to bLine 4 - a is not less than bLine 5 - a is greater than bLine 6 - a is either less than or equal to bLine 7 - b is either greater than or equal to b Python賦值運算符

以下假設(shè)變量a為10,變量b為20:

運算符 描述 實例= 簡單的賦值運算符 c = a + b 將 a + b 的運算結(jié)果賦值為 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/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", cc += aprint "Line 2 - Value of c is ", c c *= aprint "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2c %= aprint "Line 5 - Value of c is ", cc **= aprint "Line 6 - Value of c is ", cc //= aprint "Line 7 - Value of c is ", c

以上實例輸出結(jié)果:

Line 1 - Value of c is 31Line 2 - Value of c is 52Line 3 - Value of c is 1092Line 4 - Value of c is 52Line 5 - Value of c is 2Line 6 - Value of c is 2097152Line 7 - Value of c is 99864Python位運算符

按位運算符是把數(shù)字看作二進制來進行計算的。Python中的按位運算法則如下:

運算符 描述 實例& 按位與運算符 (a & b) 輸出結(jié)果 12 ,二進制解釋: 0000 1100| 按位或運算符 (a | b) 輸出結(jié)果 61 ,二進制解釋: 0011 1101^ 按位異或運算符 (a ^ b) 輸出結(jié)果 49 ,二進制解釋: 0011 0001~ 按位取反運算符 (~a ) 輸出結(jié)果 -61 ,二進制解釋: 1100 0011, 在一個有符號二進制數(shù)的補碼形式。<< 左移動運算符 a << 2 輸出結(jié)果 240 ,二進制解釋: 1111 0000>> 右移動運算符 a >> 2 輸出結(jié)果 15 ,二進制解釋: 0000 1111

以下實例演示了Python所有位運算符的操作:

#!/usr/bin/pythona = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0c = a & b;# 12 = 0000 1100print "Line 1 - Value of c is ", cc = a | b;# 61 = 0011 1101 print "Line 2 - Value of c is ", cc = a ^ b;# 49 = 0011 0001print "Line 3 - Value of c is ", cc = ~a; # -61 = 1100 0011print "Line 4 - Value of c is ", cc = a << 2; # 240 = 1111 0000print "Line 5 - Value of c is ", cc = a >> 2; # 15 = 0000 1111print "Line 6 - Value of c is ", c

以上實例輸出結(jié)果:

Line 1 - Value of c is 12Line 2 - Value of c is 61Line 3 - Value of c is 49Line 4 - Value of c is -61Line 5 - Value of c is 240Line 6 - Value of c is 15Python邏輯運算符

Python語言支持邏輯運算符,以下假設(shè)變量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/pythona = 10b = 20c = 0if ( a and b ): print "Line 1 - a and b are true"else: print "Line 1 - Either a is not true or b is not true"if ( a or b ): print "Line 2 - Either a is true or b is true or both are true"else: print "Line 2 - Neither a is true nor b is true"a = 0if ( a and b ): print "Line 3 - a and b are true"else: print "Line 3 - Either a is not true or b is not true"if ( a or b ): print "Line 4 - Either a is true or b is true or both are true"else: print "Line 4 - Neither a is true nor b is true"if not( a and b ): print "Line 5 - Either a is not true or b is not true or both are not true"else: print "Line 5 - a and b are true"

以上實例輸出結(jié)果:

Line 1 - a and b are trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - Either a is not true or b is not true or both are not truePython成員運算符

除了以上的一些運算符之外,Python還支持成員運算符,測試實例中包含了一系列的成員,包括字符串,列表或元組。

運算符 描述 實例in 如果在指定的序列中找到值返回True,否則返回False。 x 在 y序列中 , 如果x在y序列中返回True。not in 如果在指定的序列中沒有找到值返回True,否則返回False。 x 不在 y序列中 , 如果x不在y序列中返回True。

以下實例演示了Python所有成員運算符的操作:

#!/usr/bin/pythona = 10b = 20list = [1, 2, 3, 4, 5 ];if ( a in list ): print "Line 1 - a is available in the given list"else: print "Line 1 - a is not available in the given list"if ( b not in list ): print "Line 2 - b is not available in the given list"else: print "Line 2 - b is available in the given list"a = 2if ( a in list ): print "Line 3 - a is available in the given list"else: print "Line 3 - a is not available in the given list"

以上實例輸出結(jié)果:

Line 1 - a is not available in the given listLine 2 - b is not available in the given listLine 3 - a is available in the given listPython身份運算符

身份運算符用于比較兩個對象的存儲單元

運算符 描述 實例is is是判斷兩個標識符是不是引用自一個對象 x is y, 如果 id(x) 等于 id(y) , is 返回結(jié)果 1is not is not是判斷兩個標識符是不是引用自不同對象 x is not y, 如果 id(x) 不等于 id(y). is not 返回結(jié)果 1

以下實例演示了Python所有身份運算符的操作:

#!/usr/bin/pythona = 20b = 20if ( a is b ): print "Line 1 - a and b have same identity"else: print "Line 1 - a and b do not have same identity"if ( id(a) == id(b) ): print "Line 2 - a and b have same identity"else: print "Line 2 - a and b do not have same identity"b = 30if ( a is b ): print "Line 3 - a and b have same identity"else: print "Line 3 - a and b do not have same identity"if ( a is not b ): print "Line 4 - a and b do not have same identity"else: print "Line 4 - a and b have same identity"

以上實例輸出結(jié)果:

Line 1 - a and b have same identityLine 2 - a and b have same identityLine 3 - a and b do not have same identityLine 4 - a and b do not have same identity Python運算符優(yōu)先級

以下表格列出了從最高到最低優(yōu)先級的所有運算符:

運算符 描述** 指數(shù) (最高優(yōu)先級)~ + - 按位翻轉(zhuǎn), 一元加號和減號 (最后兩個的方法名為 +@ 和 -@)* / % // 乘,除,取模和取整除+ - 加法減法>> << 右移,左移運算符& 位 'AND'^ | 位運算符<= < > >= 比較運算符<> == != 等于運算符= %= /= //= -= += *= **= 賦值運算符is is not 身份運算符in not in 成員運算符not or and 邏輯運算符

以下實例演示了Python所有運算符優(yōu)先級的操作:

#!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (a + b) * c / d #( 30 * 15 ) / 5print "Value of (a + b) * c / d is ", ee = ((a + b) * c) / d # (30 * 15 ) / 5print "Value of ((a + b) * c) / d is ", ee = (a + b) * (c / d); # (30) * (15/5)print "Value of (a + b) * (c / d) is ", ee = a + (b * c) / d; # 20 + (150/5)print "Value of a + (b * c) / d is ", e

以上實例輸出結(jié)果:

Value of (a + b) * c / d is 90Value of ((a + b) * c) / d is 90Value of (a + b) * (c / d) is 90Value of a + (b * c) / d is 50
標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 午夜福利国产一级毛片 | 美国一级毛片完整高清 | 又黄又爽又刺激的视频 | 99久久综合国产精品免费 | 国产99久久 | 日韩色视频一区二区三区亚洲 | 成人欧美在线观看 | 日韩欧美一中字暮 | 写真片福利视频在线播放 | 国产高清成人 | 国产高清亚洲精品26u | 亚洲欧洲一级 | 日韩美女免费线视频 | 在线免费观看亚洲视频 | 亚洲精品美女国产一区 | 成人国产精品高清在线观看 | 91精品成人免费国产 | 日本国产最新一区二区三区 | 欧美一级毛片免费播放aa | 国产精品久久久久久福利漫画 | 亚洲精品欧洲久久婷婷99 | 欧美综合一区 | 国产精品久久久久久一级毛片 | 欧美日韩综合精品一区二区三区 | 亚洲视频在线播放 | 99久久精品国产一区二区成人 | 亚洲爱爱天堂 | 欧美在线 | 欧美 | 成在线人免费视频 | 日韩精品中文字幕在线 | 色综合a怡红院怡红院首页 色综合精品久久久久久久 色综合九九 | 国内精品久久国产大陆 | 久久久www免费看片 久久久www免费人成看片 | 欧美成 人h版在线观看 | 久久精品国产免费中文 | 国产成人美女福利在线观看 | 成人精品视频在线观看 | 在线看片 在线播放 | 玖玖玖精品视频免费播放 | 好湿好紧好痛a级是免费视频 | 国内精品1区1区3区4区 |