算数运算符
python算数运算符有七种,{ + , - , * , / , % , // , ** }
+ 表示求和
a = 10
b = 20
c = a + b
print(c)
- 表示相减
a = 10
b = 20
c = b - a
print(c)
*表示相乘
a = 10
b = 20
c = a * b
print(c)
/表示相除
a = 10
b = 20
c = a / b
print(c)
%表示求余数 (通常用来判定奇数偶数或者倍数)
a = 15
b = 7
c = 15 % 7
print(c)
//表示整除 (返回整数)
a = 15
b = 7
c = 15 // 7
print(c)
**表示次方 (优先级最高)
注:m**n
n个m想乘
a = 3
b = 5
c = 3 ** 5
print(c)
赋值运算符
python的赋值运算符有{ = , += , -= , *= , /= , %= , //= , **= }
=表示赋值 (声明一个变量a值为10,把值10赋予变量a)
a = 10
print(a)
+=表示相加在赋值
a = 10
a = a + 10
print(a)
a = 10
a += 10
print(a)
-=表示相减再赋值
a = 10
a = a - 10
print(a)
a = 10
a -= 10
print(a)
*=表示相乘再赋值
a = 10
a = a * 10
print(a)
a = 10
a *= 10
print(a)
/=表示相除再赋值
a = 10
a = a / 10
print(a)
a = 10
a /= 10
print(a)
%=表示求余数再赋值
a = 15
b = 7
a = a % b
print(a)
a = 15
b = 7
a %= b
print(a)
//=表示求整数再赋值
a = 10
a = a // 10
print(a)
a = 10
a //= 10
print(a)
**=表示次方再赋值
a = 10
a = a ** 3
print(a)
a = 10
a **= 3
print(a)
比较运算符
python的比较运算符有{ == , != , > , < , >= , <= }
注:比较的结果是布尔值 字符串在比较时逐个比较字符的大小
True
False
==表示等于
a, b = 10, 20
print(a == b)
!=表示不等于
a, b = 10, 20
print(a != b)
>表示大于
a, b = 10, 20
print(a > b)
<表示小于
a, b = 10, 20
print(a < b)
>=表示大于等于
a, b = 10, 20
print(a >= b)
<=表示小于等于
a, b = 10, 20
print(a <= b)
字符串比较
a, b = 10, "20"
print(a < int(b))
a, b = "x", "xyz"
print(a > b)
逻辑运算符
python的逻辑运算符{ and , or , not}
注:只输出假,同真同假输出左边
and表示并且
print(10 and 20 )
print(0 and 10 )
print("" and "hello")
print(10 and 0)
print( None and False)
注:只输出真,同真同假输出左边
or表示或者
print(0 or "hi")
print(None or 0)
print("hi" or 0)
print("hi " or"hello")
and的优先级高于or
r2 = True or "hello" and 100
print(r2)
not表示求相反
print(not "hi")
print(not None)
print(not 10)
print(not not not "hello")
位运算符
python位运算符有{ & , | , ^ }
注:使用二进制
&叫按位与 两个都是1结果才是1
|叫按位或 两个都是0结果才是0
^叫按位异或 相同为0 不同为1
# &都是1才是1
a = 0b0110
b = 0b0101
print(a & b)
c = 0b0100
print(c)
# |有1 就1
print(a | b )
c = 0b0111
# ^相同就0
print(a ^ b)
c = 0b0011
成员运算符
python的成员运算符有{ in , not in }
注:先判断是否是内部元素
in的后面需要由多个元素构成
字符串
in表示在其中
print("c" in "china","1" in "100")
print(100 in {100,200,300})
# 字符串中单词需要相连
print("ho "in "hello")
not in 表示求相反
print(100 not in {100,200,300})
标签:表示,10,20,python,运算符,print,赋值
From: https://blog.csdn.net/vloggyh/article/details/140160013