一.算数运算符
1.加减乘除
print(10 + 3) | 13 |
---|---|
print(10 – 3) | 7 |
print(10 * 3) | 30 |
print(10 / 3) | 3.333….(到达一定精度后停止) |
//
-
相除后得到的返回值只保留整数部分,不是四舍五入,直接抛弃小数部分
print(10 / 3) | 3.333333…. |
---|---|
print(10//3) | 3 |
%
"10"除于"3"余"1"print(10%3) #1
4.多少次方 *
"10"的"3"次方print(10**3) #1000
> | 大于 |
---|---|
< | 小于 |
= | 等于 |
!= | 不等于 |
<= | 小于等于 |
>= | 大于等于 |
-
一个等于号是赋值操作
-
-
像这样 : age = 18
1.增量赋值
age += 10 | 等同于age = age + 10 |
---|---|
age -= 10 | 等同于age = age – 10 |
age *= 10 | 等同于age = age * 10 |
age /= 10 | 等同于age = age / 10 |
age //= 10 | 等同于age = age // 10 |
age %= 10 | 等同于age = age % 10 |
age **= 10 | 等同于age = age 10** |
m=10n=20
如果我们想将m与n的值交换过来,可以这么做
temp=mm=nn=tempprint(n,m)(10, 20)
交叉赋值指的是一行代码可以搞定这件事
m=10n=20m,n=n,m # 交叉赋值m,n(20, 10)
如果我们想把列表中的多个值取出来依次赋值给多个变量名,可以这么做
>>> nums=[11,22,33,44,55]>>> >>> a=nums[0]>>> b=nums[1]>>> c=nums[2]>>> d=nums[3]>>> e=nums[4]>>> a,b,c,d,e(11, 22, 33, 44, 55)
解压赋值指的是一行代码可以搞定这件事
>>> a,b,c,d,e=nums # nums包含多个值,就好比一个压缩包,解压赋值因此得名>>> a,b,c,d,e(11, 22, 33, 44, 55)
注意,上述解压赋值,等号左边的变量名个数必须与右面包含值的个数相同,否则会报错
#1、变量名少了>>> a,b=numsTraceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: too many values to unpack (expected 2)#2、变量名多了>>> a,b,c,d,e,f=numsTraceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: not enough values to unpack (expected 6, got 5)
但如果我们只想取头尾的几个值,可以用*_匹配
>>> a,b,*_=nums>>> a,b(11, 22)
-
直言是多个指定的数据类型都可以解压
-
可用于字符串, 列表,元组,字典,集合,多用于列表,元组
-
如果用于字典,则解压的结果是 key
-
使用
*_
来将剩余的值存成一个列表(单个_
表示该变量被废弃)
salary = [1.1, 2.2, 3.3, 4.4, 5.5,6.6,44]只想取前三个m1,m2,m3,*_ = salaryprint(m1,m2,m3) #1.1,2.2,3.3print(_) #4.4,5.5,6.6,44取前面一个和后面两个m1,*_,m7,m8 = salaryprint(m1,m7,m8) #1.1,6.6,44print(_) #2.2,3.3,4.4,5.5可以自由搭配
not
-
把紧跟其后的那个条件的布尔值取反
print(not 10 == 10) #Falseprint(not 10 < 0) #Trueprint(not True) #Falseprint(not False) #Trueprint(not None) #True.........
2.and
逻辑与
-
and用来连接左右两个条件,两个条件同时为True,最终结果才为True
-
两个条件有一个为False,结果一定为False.
# True Trueprint(True and 10 > 3)# True True True Falseprint(True and 10 > 3 and 10 and 0) # 0# True True False(and偷懒,计算0就不计算了)print(10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3) # 0
3.or
逻辑或
-
or用来连接左右两个条件,两个条件同时为False,最终结果才为False
-
两个条件有一个为True,结果一定为True
# True(or偷懒,计算到3 > 2就不计算了)print(3 > 2 or 0) # True# True(or偷懒,计算到3 > 4就不计算了)print(3 > 4 or False or 3 != 2 or 3 > 2 or True) # True
4.短路运算(偷懒原则)
-
只要and左边的值为Flase,and右边的值将不会执行,返回结果就是False(偷懒原则),如果为True则继续判断后面的条件
-
只要or左边的值为True,or右边的值将不会执行,返回结果就是True(偷懒原则),如果为Flase则继续判断后面的条件
print((1 > 2 and 2 < 3) or (4 and not 1 != 3) or (3 and [])) #[]# False False []
5.或,与,非的优先级
-
#1、三者的优先级关系:not>and>or,同一优先级默认从左往右计算。>>> 3>4 and 4>3 or 1==3 and 'x' == 'x' or 3 >3False#2、最好使用括号来区别优先级,其实意义与上面的一样'''原理为:(1) not的优先级最高,就是把紧跟其后的那个条件结果取反,所以not与紧跟其后的条件不可分割(2) 如果语句中全部是用and连接,或者全部用or连接,那么按照从左到右的顺序依次计算即可(3) 如果语句中既有and也有or,那么先用括号把and的左右两个条件给括起来,然后再进行运算'''>>> (3>4 and 4>3) or (1==3 and 'x' == 'x') or 3 >3False #3、短路运算:逻辑运算的结果一旦可以确定,那么就以当前处计算到的值作为最终结果返回>>> 10 and 0 or '' and 0 or 'abc' or 'egon' == 'dsb' and 333 or 10 > 4我们用括号来明确一下优先级>>> (10 and 0) or ('' and 0) or 'abc' or ('egon' == 'dsb' and 333) or 10 > 4短路: 0 '' 'abc' 假 假 真返回: 'abc'#4、短路运算面试题:>>> 1 or 31>>> 1 and 33>>> 0 and 2 and 10>>> 0 and 2 or 11>>> 0 and 2 or 1 or 41>>> 0 or False and 1False
五.成员运算
1.in
某一个对象包含于另外一个对象则返回True
?判断子字符串是否在大字符串中print("shawn" in "hello shawn") # Trueprint("sha" in "hello shawn") # Trueprint("he sh" in "hello shawn") # False?判断key是否存在于字典中(注意:in判断的条件是字典的key)print(111 in { "k1": 111, 'k2': 222}) # Falseprint("k1" in { "k1": 111, 'k2': 222}) # True
2.not in
某一个对才能够没包含于另外一个对象则返回True
?用法一:条件1 not in 条件2(推荐使用,因为not in语义更加明确)print("shawn" not in "hello shawn") # False?用法二:not 条件1 in 条件2(先运算条件1 in 条件2再取反)print(not "shawn" in "hello shawn") # False
六.身份运算
: 判断左右两边的 id 是否相等,两个对象的ID相同则返回True
?两个不同的内存地址关联不同的变量名,只是存的值一样li1 = [111, 222, 333]li2 = [111, 222, 333]print(id(li1)) #2160395309640print(id(li2)) #2160395310152print(li1 is li2) #False?两个变量名关联同一个内存地址x = 10y = xprint(x) #140725148348960print(y) #140725148348960 print(x is y) #True
is not
:两个对象的ID不同时is not
会返回 Ture
m="xxx"n="yyy"print(id(m),id(n)) #2439526809264 2439531202736print(m is not n) #True
需要强调的是:==双等号比较的是value是否相等,而is比较的是id是否相等
#1. id相同,内存地址必定相同,意味着type和value必定相同#2. value相同type肯定相同,但id可能不同,如下>>> x='Info Tony:18'>>> y='Info Tony:18'>>> id(x),id(y) # x与y的id不同,但是二者的值相同(4327422640, 4327422256)>>> x == y # 等号比较的是valueTrue>>> type(x),type(y) # 值相同type肯定相同(<class 'str'>, <class 'str'>)>>> x is y # is比较的是id,x与y的值相等但id可以不同False