python-运算符

一、算术运算符:+ - * / %
1、+可完成数字的相加和字符串的拼接
a='hello'
b='四川'
c=2
d=1.25
print(a+b)
print(c+d)

2、可完成数字的相乘和字符串的多次拼接
a='hello'
b='四川'
c=2
d=1.25
print(c
d)
print(a2)
print('
'5+a+''*5)

3、取余运算% 一般用来判断是否可整除,或判断是奇数还是偶数
print(6%2)
print(6%4)

二、赋值运算符:= += -= = /=
a=5
a+=1 #等同于a=a+1
b=6
b-=1 #等同于b=b-1
c=2
c
=2 #等同于c=c*2
d=8
d/=4 #等同于d=d/4
print(a)
print(b)
print(c)
print(d)

三、比较运算符:<= >= < > != ==
返回值的类型为:布尔值 True False
a=2
b=4
print(a print(a>b)
print(a!=b)
print(a==b)
print(a<=b)
print(a>=b)

逻辑运算符:and or not
返回值的类型为:布尔值 True False
age=18
sex='boy'
and为左右2边都成立才为true,反之
print(age>20 and sex=='boy')
print(age<20 and sex=='boy')
or为左右2边,只要有一边成立都为true,反之
print(age>20 or sex=='boy')

成员运算符 in not in
返回值的类型为:布尔值 True False
判断某个元素是否存在数据中
a='hello'
b=[1,2,3,4,'python']
c={'class':'三班','name':'小钟'}
print('e' in a)
print(5 not in b)
print('n' in b[-1])
print('name' in c)
print('小钟' in c.values())

你可能感兴趣的:(python-运算符)