Python基本运算符

一.算数运算符

1.加减乘除

print(10 + 3)13
print(10 - 3)7
print(10 * 3)30
print(10 / 3)3.333…(到达一定精度后停止)

2.取整除 //

  • 相除后得到的返回值只保留整数部分,不是四舍五入,直接抛弃小数部分
print(10 / 3)3.333333…
print(10//3)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**

2.交叉赋值

我们定义两个变量m与n

m=10
n=20

如果我们想将m与n的值交换过来,可以这么做

temp=m
m=n
n=temp
print(n,m)
(10, 20)

交叉赋值指的是一行代码可以搞定这件事

m=10
n=20
m,n=n,m # 交叉赋值
m,n
(20, 10)

4.解压运算符

如果我们想把列表中的多个值取出来依次赋值给多个变量名,可以这么做

>>> 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=nums
Traceback (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=nums
Traceback (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,*_ = salary
print(m1,m2,m3)  #1.1,2.2,3.3
print(_)         #4.4,5.5,6.6,44

取前面一个和后面两个
m1,*_,m7,m8 = salary
print(m1,m7,m8)  #1.1,6.6,44
print(_)         #2.2,3.3,4.4,5.5

可以自由搭配

1.not 逻辑非(取反)

  • 把紧跟其后的那个条件的布尔值取反
print(not 10 == 10) #False
print(not 10 < 0)   #True
print(not True)     #False
print(not False)    #True
print(not None)     #True
.........

2.and 逻辑与

  • and用来连接左右两个条件,两个条件同时为True,最终结果才为True
  • 两个条件有一个为False,结果一定为False.
#       True     True
print(True and 10 > 3)

#       True     True    True  False
print(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左边的值为Flaseand右边的值将不会执行,返回结果就是False(偷懒原则),如果为True则继续判断后面的条件
  • 只要or左边的值为Trueor右边的值将不会执行,返回结果就是True(偷懒原则),如果为Flase则继续判断后面的条件
print((1 > 2 and 2 < 3) or (4 and not 1 != 3) or (3 and []))  #[]
#            False                 False             []

5.或,与,非的优先级

优先级not>and>or
#1、三者的优先级关系:not>and>or,同一优先级默认从左往右计算。
>>> 3>4 and 4>3 or 1==3 and 'x' == 'x' or 3 >3
False

#2、最好使用括号来区别优先级,其实意义与上面的一样
'''
原理为:
(1) not的优先级最高,就是把紧跟其后的那个条件结果取反,所以not与紧跟其后的条件不可分割

(2) 如果语句中全部是用and连接,或者全部用or连接,那么按照从左到右的顺序依次计算即可

(3) 如果语句中既有and也有or,那么先用括号把and的左右两个条件给括起来,然后再进行运算
'''
>>> (3>4 and 4>3) or (1==3 and 'x' == 'x') or 3 >3
False 

#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 3
1
>>> 1 and 3
3
>>> 0 and 2 and 1
0
>>> 0 and 2 or 1
1
>>> 0 and 2 or 1 or 4
1
>>> 0 or False and 1
False 

五.成员运算

适用于字符串时,判断子字符串是否在大字符串中(PS:字符串、列表、元组、集合都支持成员运算)

1.in 某一个对象包含于另外一个对象则返回True

?判断子字符串是否在大字符串中
print("shawn" in "hello shawn") # True
print("sha" in "hello shawn")       # True
print("he sh" in "hello shawn")     # False

?判断key是否存在于字典中(注意:in判断的条件是字典的key)
print(111 in {"k1": 111, 'k2': 222})    # False
print("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

六.身份运算

is : 判断左右两边的 id 是否相等,两个对象的ID相同则返回True

?两个不同的内存地址关联不同的变量名,只是存的值一样
li1 = [111, 222, 333]
li2 = [111, 222, 333]
print(id(li1))     #2160395309640
print(id(li2))     #2160395310152
print(li1 is li2)  #False

?两个变量名关联同一个内存地址
x = 10
y = x
print(x)       #140725148348960
print(y)       #140725148348960 
print(x is y)  #True
is not`:两个对象的ID不同时`is not` 会返回 `Ture
m="xxx"
n="yyy"
print(id(m),id(n))     #2439526809264 2439531202736
print(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 # 等号比较的是value
True
>>> type(x),type(y) # 值相同type肯定相同
(<class 'str'>, <class 'str'>)
>>> x is y # is比较的是id,x与y的值相等但id可以不同
False
  • 16
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾维斯Echo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值