python运算符
一、算数运算符 1、加法运算符 + print(5 + 3) # + 还可以用作字符串的连接运算符 print('Hello' + 'World') 输出结果: 8 HelloWorld 2、减法运算符 - print(5 - 3) # - 还可以用作求负的运算符 x = 5 print(-x) 输出结果: 2 -5 3、乘法运算符 * print(5 * 3) # * 还可以用作字符串连接运算符,表示 n 个字符串连接 s = 'xyz ' print(s * 5) 输出结果: 15 xyz xyz xyz xyz xyz 4、除法运算符 / 和 // / 表示普通除法,即除不尽时,会产生小数部分;而 // 表示整除,即结果只有整数部分,小数部分将会被舍弃。不允许使用 0 作为除数,否则将会引发 ZeroDivisionError 错误。 a = 15.3 b = 4.2 c1 = a / b c2 = a // b print(c1, type(c1)) print(c2, type(c2)) 输出结果: 3.642857142857143 <class 'float'> 3.0 <class 'float'> 5、求余运算符 % print(5 % 3) print(5.2 % 3.1) print(-5.2 % -3.1) print(5.2 % -1.5) print