标签:python
算数运算符
运算符 | 描述符 |
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 模--取余 |
** | 指数 |
// | 地板除 |
#!/usr/bin/env python2 # *.* coding:utf-8 *.* # @Time : 2017-10-19 17:07 # @Author : chengxiaomin # @File : 003.py a = 2 b = 3 c = 9 d = 10.0 print (a + b) print (a * b) print (c / b) print (a ** b) print (c % b) print (d / b) print (d // b) #输出结果 5 6 3 8 0 3.33333333333 3.0
关系运算符
== | 检查两个操作数是否相等,若相等则为真 | Ture or False |
!= | 检查两个操作数是否不相等,若不相等则为真 | Ture or False |
> | 比较大小,若成立为真,反之为假 | Ture or False |
< | 比较大小,若成立为真,反之为假 | Ture or False |
>= | 若条件满足,为真,反之为假 | Ture or False |
<= | 若条件满足,为真,反之为假 | Ture or False |
#!/usr/bin/env python2 # *.* coding:utf-8 *.* # @Time : 2017-10-19 17:11 # @Author : chengxiaomin # @File : 004.py a = 3 b = 4 print(a > b) print(a < b) print(a == b) print(a >= b) print(a != b) 输出结果: False True False False True
赋值运算符
运算符 | 描述符 |
= | 直接赋值 |
+= | a +=b 等同于a=a+b |
-= | a -=b 等同于a=a-b |
*= | a *=b 等同于a=a*b |
/= | a /=b 等同于a=a/b |
%= | a %=b 等同于a=a%b |
**= | a **=b 等同于a=a**b |
#!/usr/bin/env python2 # *.* coding:utf-8 *.* # @Time : 2017-10-19 17:20 # @Author : chengxiaomin # @File : 005.py a = 3 b = 2 c = 4 e = 5 a+=b c-=b e*=b print (a) print (c) print (e) 输出结果: 5 2 10
逻辑运算符
运算符 | 描述符 |
and | and两边条件都为True,则结果为真 |
or | or两边只需满足一边为True,则结果才为真 |
not | 两边条件都为False时,结果为真 |
#!/usr/bin/env python2 # *.* coding:utf-8 *.* # @Time : 2017-10-19 17:31 # @Author : chengxiaomin # @File : 006.py a = 5 b = 2 if a > 0 and b > 0: print ("True") if a < 0 or b > 1: print ("True") 输出结果: True True
标签:python
原文地址:http://xiexiaoying.blog.51cto.com/3996899/1974337