1.算术运算符
+ :加算运算符
int类型相加
>>> 12+13 25
str类型相加
>>> 'zhang'+'san''zhangsan'
float类型相加
>>> 12.1+12.3 24.4
float类型相加有精度误差
可使用round()内置函数精确小数位数 并且数据类型没变
>>> 0.2+0.1 0.30000000000000004
>>> a=0.1+0.2 >>> (round(a,20.3 >>> 0.30000000000000004 >>> print(type(round(a,2)))
<class 'float'>
float与int类型相加
>>> 1+3.0 4.0
int类型与str类型相加
int类型与float类型不能与str类型 做运算
>>> 'zhang'+1Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: must be str, not int
>>> 'zhang'+1.5Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: must be str, not float
正数与负数相加
>>> -1+3 2 >>> -1+-2 -3 >>> 3+-1 2
- 减算运算
int类型相减
>>> 12-18 -6
str类型相减
str类型不能做减法运算
>>> 'zhang'-'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for -: 'str' and 'str'
float类型相减
注意精度误差,float类型的精度误差是每一种语言都有的
当值有精度误差时可使用round()内置函数精确小数位数 并且数据类型没变
>>> 25.3-1.3 24.0
>>> 2.0-1.8 0.19999999999999996
>>> a=2.0-1.8 >>> print(round(a,2))0.2
>>> print(type(round(a,2)))<class 'float'>
int类型与float类型相减
>>> 25-1.3 23.7
正数与负数相减
>>> 25--1.3 26.3
>>> -25--1.3 -23.7
*乘法运算
int类型相乘
>>> 3*3 9
str类型相乘
>>> 'zhang'*'san'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: can't multiply sequence by non-int of type 'str'
float类型相乘
注意float精度误差
>>> 2.0*3.5 7.0
int类型与float类型相乘
>>> 2*3.5 7.0
int类型与str类型相乘
注意:int类型与str类型相乘结果是str
>>> 2*'3.5''3.53.5'>>> 2*'zhang''zhangzhang'
正数与负数相乘
>>> 2*-2 -4
>>> -2*-2 4
/ 除法运算
int类型相除
python3x int类型相除结果为float类型
>>> 6/3 2.0
python2x int类型相除结果为int类型 只取整数
>>> 6 / 3 2
>>> 7 / 3 2
float类型相除
>>> 6.5/0.5 13.0
str类型相除
注意:str类型不能与str类型相除
>>> 'zhang'/'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for /: 'str' and 'str'
int类型与float类型相除
>>> 6/1.5 4.0
int类型与str类型相除
注意:int类型不能与str类型相除
>>> 6/'zhang'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for /: 'int' and 'str'
正数与负数相除
注意:python3x中结果为float类型 python2x中结果为int类型 只取整数
>>> 6/-3 -2.0 >>> -6/-3 2.0
** 乘方运算(幂运算) a**b 结果为a的b次方
int类型乘方运算
>>> 2**3 #2的3次方 8
float类型乘方运算
>>> 1.5**1.5 #1.5的1.5次方 1.8371173070873836
str类型乘方运算
注意 :str不能与str做乘方运算
>>> 'zhang'**'san'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'
int类型与float类型乘方运算
>>> 2**1.5 2.8284271247461903
int类型str类型乘方运算
注意:int类型不能与str类型做乘方运算 但是可以做乘法运算 得出的结果是str
>>> 'zhang'**2Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
正数与负数的乘方运算
>>> 2**-1.5 0.3535533905932738
>>> 2**-1.5 0.3535533905932738 >>> -2**-1.5 -0.3535533905932738 >>> -2**1.5 -2.8284271247461903 >>> -2**-2 -0.25
% 取模运算 a%b =a-(b*c) c是商值
int类型中 a%b=a-(b*c)
c=a/b c为正数时,商值c的取值规则向0取值
int类型取模运算
>>> 5%3 5/3 商值1或2 1<2 商值取1 2 >>> 6%3 商值只能取2 0
float类型取模运算
>>> 5.0%1.5 5.0/1.5商值为3或4 3<4 商值取3 0.5
str类型取模运算
注意:str类型不能进行取模运算
>>> 'zhang'%'z'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: not all arguments converted during string formatting
int类型与float类型取模运算
>>> 5%1.5 0.5 >>> 5%3.0 2.0
int类型与str类型取模运算
注意:int类型不能与str类型做取模运算
>>> 5%'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for %: 'int' and 'str'>>> 5%'1'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for %: 'int' and 'str'
正数与负数取模运算
c=a/b c为负数时, 商值c的取值规则向负无穷取值
c=a/b c为正数时,商值c的取值规则向0取值
>>> 5%-3 5/-3商值为-1或-2 -2<-1 商值取-2 a%b=a-(b*c)=5-(-3*-2)=5-6=-1 -1
>>> -5%-3 -5/-3商值为1或2 1<2 商职取1 a%b=a-(b*c)=-5-(-3*1)=-5-(-3)=-2 -2
>>> -5%3 -5/3商值为-1或-2 -2<-1 商职取-2 -5-(3*-2)=-5-(-6)=1 1
//整除法 a//b=c c是商值 c=a/b
int类型整除
c=a/b c为正数时 商值c的取值规则向0取值
c=a/b c为负数时 商值c的取值规则向负无穷取值
>>> 5//3 5/3商值为1或2 1<2 商值取1 #向0取值 1
>>> 5//-3 5/-3商值为-1或-2 -2<-1 商值取-2 #向负无穷取值 -2
>>> -5//3 -5/3商值为-1或-2 -2<-1 商值取-2 #向负无穷取值 -2
>>> -5//-3 -5/-3商值为1或2 1<2 商值取1 #向0取值 1
float类型整除
>>> 5.5//1.5 3.0
str类型整除
注意:str类型不能做整除运算
>>> 'zhang'//'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for //: 'str' and 'str'>>> '5'//'3'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for //: 'str' and 'str'
int类型与float类型整除
>>> 5//1.6 3.0
>>> 5.5//2 2.0
上一篇: jmeter中对请求参数进行md5加密
下一篇: 软件测试之手工测试人员如何转测试开发?