4.1 运算符
4.1.1 运算符概念
- 运算符是对一个以上的操作对象进行运算
- 1+2 1和2是操作对象 +是运算符
4.1.2 运算符分类
- 算术运算符
- 赋值运算符
- 比较运算符(关系)
- 逻辑运算符
- 条件运算符(三元)
4.2 算术运算符
- +、-、*、/、//取整、%取余、**x数字的x次幂
# 算术运算符
# +
x = 1 + 2
print('x =',x)
x = 'hello'+' '+'world'
print('x =',x)
# -
x = 2-1
print('x =',x)
x = 2-True
print('x =',x)
# x='abc'-'c' # TypeError: unsupported operand type(s) for -: 'str' and 'str'
# *
x=2*3
print('x =',x)
# 将字符串与数字y相乘,将字符复制y次
x='a'*3
print('x =',x)
# x='a'*'b' # TypeError: can't multiply sequence by non-int of type 'str'
# /
x=6/3
# x=6/0 # (除数不能为0)ZeroDivisionError: division by zero
#x='a'/'b' # TypeError: unsupported operand type(s) for /: 'str' and 'str'
print(x)
# // % **
x=7/3 # 2.3333333333333335
x=7//3 # 2
x=