python运算符号和部分运算符号的优先级


一、基本运算符号

1、数学运算符号

加:+、减:-、乘:*、除:/、赋值符号=、乘方:**、取余数:%、整除://

通常代码中都会简化运算的表达式:

image

2、比较运算符号

大于:>、小于:<、大于等于:>=、小于等于:<=、等于号:==、不等于:!=。

比较运算符号返回的都是布尔值

image

二、常用赋值符号

python中的“=”号跟数学中的含义不同,“=”的称呼是赋值符号,顾名思义就是把值赋予给某个对象。在python中,“age = 19”中就是把右边的数据值赋值给左边的变量。

1、链式赋值

给多个变量名绑定同一数据值

举例:

#看着很累的方法
name='猪头'
name= name1
name1=name2
#简短高效的方法
name=name1=name2='猪头'

2、交叉赋值

把两个变量名互相交换数据值

m=999
n=666

change=m
m=n
n=change
#这是比较繁琐的交叉赋值表达方式

m,n=n,m
#这是更加简洁的交叉赋值表达方式

3、解压赋值

当一个列表内有个数据值需要跟多个变量名绑定时,可以使用解压赋值。

解压赋值的时候正常情况下数据值个数和变量名个数需,否则会报错。

s1 = [1,2,3,4,5]
name,name1,name2,name3,name4 = s1

当数据值较多的时候,而我们又用不到中间的没用数据时,可以用*号收取多余数据值。可以取前面的树枝也可以取后面的数值。

s1 = [1,2,3,4,5]

a,*b = s1
*c,d = s1

print(a)
print(b)
print(c)
print(d)

三、逻辑运算符号

1、and

当and所连接的所有条件都成立时,返回True,否则返回False。

print(1<2and2>3) # False
print(1<2and2<3) # True

#and 运算符号输出时会输出最后一项的结果,如果不成立,就会输出不成立的第一项。
#当val = 1 and 3
这种形式的时候,除了要分辨条件是否成立,当我们print结果时,显示的结果会是3(输出最后的条件)
#当val = 0 and 3
这种形式的时候,先分辨条件是否成立,如果不成立就输出0
#当val = 1 and 1<3
这种形式的时候,先分辨条件是否成立,判断成立后,根据最后一项1<3会返回布尔值True

2、or

当or所连接的所有条件中有一个成立,就返回True,否则返回False。

print(1<2or2>3) # True
print(1>2or2>3) # False

#当or运算符号成立时,输出的结果时成立的第一项,如果一个都不成立,就会输出最后一项。
#当val1 = 1 or 3
这种形式的时候,除了要分辨条件是否成立,当我们print结果时,显示的结果会是1(输出第一个成立的条件)
#当val1 = 0 or 1>3和val2 = 1>3 or 0
这种形式的时候,先分辨条件是否成立,可以看出两个都不成立,所以我们可以通过打印发现val1的返回值是False,val2的返回值是0

3、not

返回与原来相反的布尔值。

is_example = True
print(not is_example)

以下假设变量 a 为 10, b为 20:

运算符名称 运算符表达式 描述 示例
and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
or x or y 布尔"或" - 如果 x 是 True,它返回 x 的值,否则它返回 y 的计算值。 (a or b) 返回 10。
not not x 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 not(a and b) 返回 False

四、成员运算符号

in

用于判断变量名或数据值是否在某个数据值或变量名内部。(例如个体是否在群体内部)

name_list = ['jason', 'kevin', 'oscar', 'jerry']
print('ja' in name_list)  # False
print('jason' in name_list)  # True

其中数据值也可以用变量名代替

not in

用于判断变量名或数据值是不是不在某个数据值或变量名内部。(与in相反)

运算符名称 描述 示例
in 如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in 如果在指定的序列中没有找到值返回 True,否则返回 False。 x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。

注:
1.字符串成员运算最小单位是单个单个的字符
2.列表成员运算最小单位是单个单个的数据值
3.字典成员运算最小单位是键(值不参与)

五、身份运算符号

is

相关知识拓展:

id()函数:可以返回数据值或变量名当前绑定的内存地址

is 运算符用于判断两个用来比较的对象内存地址是否相同。

a= ['jason', 'kevin', 'oscar', 'jerry']
b= ['jason', 'kevin', 'oscar', 'jerry']

print(a == b)
#会返回True
print(a is b)
#会返回False

从这里我们可以得出一个结论:内存地址相同时,两者数据值一定相同,数据值相同时,内存地址不一定相同。

not is

运算符名称 描述 示例
is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

==

判断数据值是否相同,跟数学中的等于号一个意思。

小整数池

当我们把两个不同的变量名绑定相同的数据值时,python解释器会进行优化,pycharm也会进行一定的优化,一些位数较小的数字和一些较短的字符串当被检测到相同时,内存空间中会只用一个地址表示,反之,就会使用两块内存空间中的地址表示

# 整形:较短的可以
a1 = 11
a2 = 11
print(a1 is a2)
#浮点型:较短的可以
a3 = 11.11
a4 = 11.11
print(a3 is a4)
#字符串:较短的可以
a5 = 'jason'
a6 = 'jason'
print(a5 is a6)
#列表:无论长短都不可以
a7 = [1,2,3]
a8 = [1,2,3]
print(a7 is a8)
#字典:无论长短都不可以
a9 = {'name' : 'zzh','age' : '21'}
a10 = {'name' : 'zzh','age' : '21'}
print(a9 is a10)
#元组:较短的可以
a11 = (1,2,3)
a12 = (1,2,3)
print(a11 is a12)
#集合:无论长短都不可以
a13 = {1,2}
a14 = {1,2}
print(a13 is a14)

六、位运算符

运算符名称 描述 示例
& 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 (a & b) 输出结果 12 ,二进制解释: 0000 1100
| 按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。 (a | b) 输出结果 61 ,二进制解释: 0011 1101
^ 按位异或运算符:当两对应的二进位相异时,结果为1 (a ^ b) 输出结果 49 ,二进制解释: 0011 0001
~ 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1。~x 类似于 -x-1 (~a ) 输出结果 -61 ,二进制解释: 1100 0011, 在一个有符号二进制数的补码形式。
<< 左移动运算符:运算数的各二进位全部左移若干位,由"<<"右边的数指定移动的位数,高位丢弃,低位补0。 a << 2 输出结果 240 ,二进制解释: 1111 0000
>> 右移动运算符:把">>“左边的运算数的各二进位全部右移若干位,”>>"右边的数指定移动的位数 a >> 2 输出结果 15 ,二进制解释: 0000 1111

七、运算符优先级

运算符名称 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,求余数和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 ‘AND’
<= < > >= 比较运算符
== != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not and or 逻辑运算符


posted @   致丶幻  阅读(373)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
  1. 1 So Far Away (Acoustic) Adam Christopher
  2. 2 雪 Distance Capper&罗言RollFlash
  3. 3 CollapsingWorld
  4. 4 Call You Tonight Johnta Austin
Call You Tonight - Johnta Austin
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Mmmm...

Mmmmm...

Mmmmmm....

The stars must be alighted tonight

I believe this has to have a meaning

Lightning had to strike tonight

Cause the two of us are finally meeting

In this place at this time

And I feel safe when I look in your eyes

I feel like I know you from another life

And it makes me wish I wasn‘t so pressed by time

I can‘t catch my breathe

Cause you take it away

The best writer in town

Could not find words to say

How there are so many things

I wanna get to know

I wish that I could stay

But I gotta go

So I‘mma call you tonight

I will baby

Just as soon as I get time alone

I‘mma call you tonight

I will baby

Just as soon as I get home

That‘s not the way it goes in life

You get busy when you just don‘t wanna

There‘s never enough time day or night

You have to make it so baby I‘m gonna

Make a way to connect

Cause your face is one I can‘t forget

I feel like I know you from another life

And it makes me wish I wasn‘t so pressed for time

I can‘t catch my breathe

I can‘t catch my breathe

Cause you take it away

The best writer in town

Could not find words to say

How there are so many things

I wanna get to know

I wish that I could stay

But I gotta go

So I‘mma call you tonight

I will baby

Just as soon as I get time alone

I‘mma call you tonight

I will baby

Just as soon as I get home

Destiny

I believe in it

Meant to be

Don‘t you see it‘s possible that this kind of magic

Anything can happen

And if you wanna know

Then you‘ll stay by the phone

Ohhhhhh

I can‘t catch my breathe

Cause you take it away

The best writer in town

Could not find words to say

How there are so many things

I wanna get to know

I wish that I could stay

But I gotta go

So I‘mma call you tonight

I will baby

Just as soon as I get time alone

I‘mma call you tonight

I will baby

Just as soon as I get home

I‘mma call you tonight

I will baby

Just as soon as I get home

点击右上角即可分享
微信分享提示