运算

  1 什么是操作符?
  2 简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符。 Python语言支持操作者有以下几种类型。
  3 算术运算符
  4 比较(即关系)运算符
  5 赋值运算符
  6 逻辑运算符
  7 位运算符
  8 会员操作符
  9 标识操作符
 10 让我们逐一看看所有的运算符。
 11 Python算术运算符:
 12 假设变量a持有10和变量b持有20,则: 
 13 ​操作符    描述符 例子
 14 +   加法 - 对操作符的两侧增加值 a + b = 30
 15 -   减法 - 减去从左侧操作数右侧操作数  a - b = -10
 16 *   乘法 - 相乘的运算符两侧的值 a * b = 200
 17 /   除 - 由右侧操作数除以左侧操作数   b / a = 2
 18 %   模 - 由右侧操作数和余返回除以左侧操作数   b % a = 0
 19 **  指数- 执行对操作指数(幂)的计算   a**b = 10 的幂 20
 20 //  地板除 - 操作数的除法,其中结果是将小数点后的位数被除去的商。    9//2 =  4 而 9.0//2.0 = 4.0
 21 #!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", c
 22 
 23 c = a - bprint "Line 2 - Value of c is ", c 
 24 
 25 c = a * bprint "Line 3 - Value of c is ", c 
 26 
 27 c = a / bprint "Line 4 - Value of c is ", c 
 28 
 29 c = a % bprint "Line 5 - Value of c is ", c
 30 
 31 a = 2b = 3c = a**b 
 32 print "Line 6 - Value of c is ", c
 33 
 34 a = 10b = 5c = a//b print "Line 7 - Value of c is ", c
 35 
 36 执行程序结果
 37 Line 1 - Value of c is 31
 38 Line 2 - Value of c is 11
 39 Line 3 - Value of c is 210
 40 Line 4 - Value of c is 2
 41 Line 5 - Value of c is 1
 42 Line 6 - Value of c is 8
 43 Line 7 - Value of c is 2
 44 Python的比较操作符:
 45 假设变量a持有10和变量b持有20,则:
 46 运算符 描述  示例
 47 ==  检查,两个操作数的值是否相等,如果是则条件变为真。   (a == b) 不为 true.
 48 !=  检查两个操作数的值是否相等,如果值不相等,则条件变为真。    (a != b) 为 true.
 49 <>  检查两个操作数的值是否相等,如果值不相等,则条件变为真。    (a <> b) 为 true。这个类似于 != 运算符
 50 >   检查左操作数的值是否大于右操作数的值,如果是,则条件成立。   (a > b) 不为 true.
 51 <   检查左操作数的值是否小于右操作数的值,如果是,则条件成立。   (a < b) 为 true.
 52 >=  检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。    (a >= b) 不为 true.
 53 <=  检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。    (a <= b) 为 true.
 54 #!/usr/bin/pythona = 21b = 10c = 0if ( a == b ):
 55    print "Line 1 - a is equal to b"else:
 56    print "Line 1 - a is not equal to b"if ( a != b ):
 57    print "Line 2 - a is not equal to b"else:
 58    print "Line 2 - a is equal to b"if ( a <> b ):
 59    print "Line 3 - a is not equal to b"else:
 60    print "Line 3 - a is equal to b"if ( a < b ):
 61    print "Line 4 - a is less than b" else:
 62    print "Line 4 - a is not less than b"if ( a > b ):
 63    print "Line 5 - a is greater than b"else:
 64    print "Line 5 - a is not greater than b"a = 5;b = 20;if ( a <= b ):
 65    print "Line 6 - a is either less than or equal to  b"else:
 66    print "Line 6 - a is neither less than nor equal to  b"if ( b >= a ):
 67    print "Line 7 - b is either greater than  or equal to b"else:
 68    print "Line 7 - b is neither greater than  nor equal to b"
 69 当执行上面的程序它会产生以下结果:
 70 
 71 Line 1 - a is not equal to b
 72 Line 2 - a is not equal to b
 73 Line 3 - a is not equal to b
 74 Line 4 - a is not less than b
 75 Line 5 - a is greater than b
 76 Line 6 - a is either less than or equal to b
 77 Line 7 - b is either greater than or equal to b
 78 Python赋值运算符:
 79 假设变量持有10和变量b持有20,则:
 80 运算符 描述  示例
 81 =   简单的赋值运算符,赋值从右侧操作数左侧操作数  c = a + b将指定的值 a + b 到  c
 82 +=  加法AND赋值操作符,它增加了右操作数左操作数和结果赋给左操作数    c += a 相当于 c = c + a
 83 -=  减AND赋值操作符,它减去右边的操作数从左边操作数,并将结果赋给左操作数    c -= a 相当于 c = c - a
 84 *=  乘法AND赋值操作符,它乘以右边的操作数与左操作数,并将结果赋给左操作数    c *= a 相当于 c = c * a
 85 /=  除法AND赋值操作符,它把左操作数与正确的操作数,并将结果赋给左操作数 c /= a 相当于= c / a
 86 %=  模量AND赋值操作符,它需要使用两个操作数的模量和分配结果左操作数   c %= a is equivalent to c = c % a
 87 **= 指数AND赋值运算符,执行指数(功率)计算操作符和赋值给左操作数    c **= a 相当于 c = c ** a
 88 //= 地板除,并分配一个值,执行地板除对操作和赋值给左操作数 c //= a 相当于 c = c // a
 89 #!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", c
 90 
 91 c += aprint "Line 2 - Value of c is ", c 
 92 
 93 c *= aprint "Line 3 - Value of c is ", c 
 94 
 95 c /= a 
 96 print "Line 4 - Value of c is ", c 
 97 
 98 c  = 2c %= aprint "Line 5 - Value of c is ", c
 99 
100 c **= aprint "Line 6 - Value of c is ", c
101 
102 c //= aprint "Line 7 - Value of c is ", c
103 当执行上面的程序,它会产生以下结果:
104 
105 Line 1 - Value of c is 31
106 Line 2 - Value of c is 52
107 Line 3 - Value of c is 1092
108 Line 4 - Value of c is 52
109 Line 5 - Value of c is 2
110 Line 6 - Value of c is 2097152
111 Line 7 - Value of c is 99864
112 Python位运算符:
113 位运算符作用于位和位操作执行位。假设,如果a =60;且b =13;现在以二进制格式它们将如下:
114 a = 0011 1100
115 b = 0000 1101
116 -----------------
117 a&b = 0000 1100
118 a|b = 0011 1101
119 a^b = 0011 0001
120 ~a  = 1100 0011
121 Python语言支持下位运算符
122 操作符 描述  示例
123 &   二进制和复制操作了一下,结果,如果它存在于两个操作数。 (a & b) = 12 即 0000 1100
124 |   二进制或复制操作了一个比特,如果它存在一个操作数中。  (a | b) = 61 即 0011 1101
125 ^   二进制异或运算符的副本,如果它被设置在一个操作数而不是两个比特。    (a ^ b) =  49 即  0011 0001
126 ~   二进制的补运算符是一元的,并有“翻转”位的效果。    (~a ) =  -61 即 1100 0011以2的补码形式由于带符号二进制数。
127 <<  二进位向左移位运算符。左操作数的值左移由右操作数指定的位数。  a << 2 = 240 即 1111 0000
128 >>  二进位向右移位运算符。左操作数的值是由右操作数指定的位数向右移动。   a >> 2 = 15 即 0000 1111
129 #!/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 ", c
130 
131 c = a | b;        # 61 = 0011 1101 print "Line 2 - Value of c is ", c
132 
133 c = a ^ b;        # 49 = 0011 0001print "Line 3 - Value of c is ", c
134 
135 c = ~a;           # -61 = 1100 0011print "Line 4 - Value of c is ", c
136 
137 c = a << 2;       # 240 = 1111 0000print "Line 5 - Value of c is ", c
138 
139 c = a >> 2;       # 15 = 0000 1111print "Line 6 - Value of c is ", c
140 当执行上面的程序它会产生以下结果:
141 
142 Line 1 - Value of c is 12
143 Line 2 - Value of c is 61
144 Line 3 - Value of c is 49
145 Line 4 - Value of c is -61
146 Line 5 - Value of c is 240
147 Line 6 - Value of c is 15
148 Python逻辑运算符:
149 Python语言支持以下逻辑运算符。假设变量a持有10和变量b持有20则:
150 运算符 描述  示例
151 and 所谓逻辑与运算符。如果两个操作数都是真的,那么则条件成立。   (a and b) 为 true.
152 or  所谓逻辑OR运算符。如果有两个操作数都是非零然后再条件变为真。 (a or b) 为 true.
153 not 所谓逻辑非运算符。用于反转操作数的逻辑状态。如果一个条件为真,则逻辑非运算符将返回false。 not(a and b) 为 false.
154 #!/usr/bin/pythona = 10b = 20c = 0if ( a and b ):
155    print "Line 1 - a and b are true"else:
156    print "Line 1 - Either a is not true or b is not true"if ( a or b ):
157    print "Line 2 - Either a is true or b is true or both are true"else:
158    print "Line 2 - Neither a is true nor b is true"a = 0if ( a and b ):
159    print "Line 3 - a and b are true"else:
160    print "Line 3 - Either a is not true or b is not true"if ( a or b ):
161    print "Line 4 - Either a is true or b is true or both are true"else:
162    print "Line 4 - Neither a is true nor b is true"if not( a and b ):
163    print "Line 5 - Either a is not true or b is not true"else:
164    print "Line 5 - a and b are true"
165 当执行上面的程序它会产生以下结果:
166 
167 Line 1 - a and b are true
168 Line 2 - Either a is true or b is true or both are true
169 Line 3 - Either a is not true or b is not true
170 Line 4 - Either a is true or b is true or both are true
171 Line 5 - Either a is not true or b is not true
172 Python成员运算符:
173 除了前面讨论的运算符,Python成员运算符,在一个序列中成员资格的测试,如字符串,列表或元组。有两个成员运算符解释如下:
174 操作符 描述  示例
175 in  计算结果为true,如果它在指定找到变量的顺序,否则false。    x在y中,在这里产生一个1,如果x是序列y的成员。
176 not in  计算结果为true,如果它不找到在指定的变量顺序,否则为false。  x不在y中,这里产生结果不为1,如果x不是序列y的成员。
177 #!/usr/bin/pythona = 10b = 20list = [1, 2, 3, 4, 5 ];if ( a in list ):
178    print "Line 1 - a is available in the given list"else:
179    print "Line 1 - a is not available in the given list"if ( b not in list ):
180    print "Line 2 - b is not available in the given list"else:
181    print "Line 2 - b is available in the given list"a = 2if ( a in list ):
182    print "Line 3 - a is available in the given list"else:
183    print "Line 3 - a is not available in the given list"
184 当执行上面的程序它会产生以下结果:
185 
186 Line 1 - a is not available in the given list
187 Line 2 - b is not available in the given list
188 Line 3 - a is available in the given list
189 Python标识运算符:
190 标识符比较两个对象的内存位置。两个运算符标识解释如下:
191 运算符 描述  例子
192 is  计算结果为true,如果操作符两侧的变量指向相同的对象,否则为false。   x是y,这里结果是1,如果id(x)的值为id(y)。
193 is not  计算结果为false,如果两侧的变量操作符指向相同的对象,否则为true。   x不为y,这里结果不是1,当id(x)不等于id(y)。
194 #!/usr/bin/pythona = 20b = 20if ( a is b ):
195    print "Line 1 - a and b have same identity"else:
196    print "Line 1 - a and b do not have same identity"if ( id(a) == id(b) ):
197    print "Line 2 - a and b have same identity"else:
198    print "Line 2 - a and b do not have same identity"b = 30if ( a is b ):
199    print "Line 3 - a and b have same identity"else:
200    print "Line 3 - a and b do not have same identity"if ( a is not b ):
201    print "Line 4 - a and b do not have same identity"else:
202    print "Line 4 - a and b have same identity"
203 当执行上面的程序它会产生以下结果:
204 
205 Line 1 - a and b have same identity
206 Line 2 - a and b have same identity
207 Line 3 - a and b do not have same identity
208 Line 4 - a and b do not have same identity
209 Python运算符优先级
210 下表列出了所有运算符从最高优先级到最低。
211 
212 运算符 描述
213 **  幂(提高到指数)
214 ~ + -   补码,一元加号和减号(方法名的最后两个+@和 - @)
215 * / % //    乘,除,取模和地板除
216 + - 加法和减法
217 >> <<   左,右按位转移
218 &   位'AND'
219 ^ | 按位异'或`'和定期`或'
220 <= < > >=   比较运算符
221 <> == !=    等式运算符
222 = %= /= //= -= += *= **=    赋值运算符
223 is is not   标识运算符
224 in not in   成员运算符
225 not or and  逻辑运算符
226 #!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (a + b) * c / d       #( 30 * 15 ) / 5print "Value of (a + b) * c / d is ",  e
227 
228 e = ((a + b) * c) / d     # (30 * 15 ) / 5print "Value of ((a + b) * c) / d is ",  e
229 
230 e = (a + b) * (c / d);    # (30) * (15/5)print "Value of (a + b) * (c / d) is ",  e
231 
232 e = a + (b * c) / d;      #  20 + (150/5)print "Value of a + (b * c) / d is ",  e
233 当执行上面的程序,它会产生以下结果:
234 
235 Value of (a + b) * c / d is 90
236 Value of ((a + b) * c) / d is 90
237 Value of (a + b) * (c / d) is 90
238 Value of a + (b * c) / d is 50

 

posted @ 2016-10-12 17:06  Callum  阅读(329)  评论(0编辑  收藏  举报