和大多数语言一样,Python也有很多运算符,并且运算符跟其他语言的运算符大同小异接下来一一介绍:
算术运算符:
运算符 | 描述 | 实例 |
+ | 加 - 两个对象相加 | a+b的输出结果是30 |
- | 减 - 得到复数或者一个数减去另外一个数 | a-b的输出结果是10 |
* | 乘 - 两个数相乘或是返回一个被重复若干次的字符串 | a*b的结果是200 |
/ | 除 -x 除以y |
a/b的结果是2 |
% | 取模 - 返回除法的余数 | b/a的结果是0 |
.. | 幂- 返回x的y次幂 |
a**b为10的20次方 |
// | 取整除 - 返回商的整数部分 | 9//2的结果是4,9.0//2.0的结果是4.0 |
比较运算符:
运算符 | 描述 | 实例 |
== | 等于-比较对象是否相等 | (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 |
赋值运算符:
运算符 | 描述 | 实例 |
= |
简单的赋值运算符 | 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 |
逻辑运算符:
运算符 | 描述 | 实例 |
and | 布尔"与"如果x为False,x and y 返回False,否则它返回y的计算值 | (a and b)返 回true |
or | 布尔"或",如果x是True,它返回True,否则它返回y的值 | (a or b)返回true |
not in | 布尔"非",如果x为True,返回False,如果x为False,它发挥True | not(a and b) 返回false |
成员运算:
运算符 | 描述 | 实例 |
in | 如果在指定的序列中找到值返回True,否则返回False | x在y序列中,如果x在y序列中返回True |
not in | 入股在指定的序列中没有找到返回True,否则返回False | x不在y序列中,如果不在y序列中返回True |
身份运算:
运算符 |
描述 | 实例 |
is | is是判断两个标识符是不是引用同一个对象 | x is y,如果id(x)等于id(y),is返回结果1 |
is not | is not 是判断两个标识符是不是引用不同的对象 | x is not y,如果id(x)不等于id(y)返回结果1 |
位运算符:
运算符 | 描述 | 实例 |
& |
按位与运算符 | (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 |
代码如下:
#!/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
运算符优先级:
运算符 | 描述 |
** | 指数(最高优先级) |
~、+、- | 按位翻转,一元加号和减号(最后两个方法名为+@he -@) |
*、/、%、// | 乘、除、取模、取整除 |
+、- | 加法减法 |
>>、<< | 右移、左移运算符 |
& | 位"AND" |
^ | 位运算符 |
<=、 <>、 >= | 比较运算符 |
<> 、== 、!= | 等于运算符 |
=、 %=、 /= 、//= 、-=、 +=、 *=、 **= | 赋值运算符 |
is 、is not | 身份运算符 |
in 、not in | 成员运算符 |
not 、or and | 逻辑运算符 |
本文引用于:http://www.cnblogs.com/wupeiqi/articles/4906230.html
注释:
在Python中给一个变量赋值用等号("="),如果判断两个变量是否相等或者值是否相等用等号("==")
如下示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#!/usr/bin/python
a
=
21
=
=
=
=
=
=
=
=
=
=
=
>给变量赋值
b
=
10
=
=
=
=
=
=
=
=
=
=
=
>给变量赋值
c
=
0
=
=
=
=
=
=
=
=
=
=
=
>给变量赋值
if
( 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"
|
输出结果:
1
2
3
4
5
6
7
|
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
|
本文转自027ryan 51CTO博客,原文链接:http://blog.51cto.com/ucode/1710762
,如需转载请自行联系原作者