百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Python 运算符(python运算符优先级最高)

off999 2024-10-24 12:34 97 浏览 0 评论

什么是运算符?

本章节主要说明Python的运算符。举个简单的例子 4 +5 = 9 。 例子中,4和5被称为操作数,"+"号为运算符。

Python语言支持以下类型的运算符:

算术运算符

比较(关系)运算符

赋值运算符

逻辑运算符

位运算符

成员运算符

身份运算符

运算符优先级

接下来让我们一个个来学习Python的运算符。

Python算术运算符

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

运算符

描述

实例

+ 加 - 两个对象相加 a + b 输出结果 30

- 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10

* 乘 - 两个数相乘或是返回一个被重复若干次的字符串 a * b 输出结果 200

/ 除 - x除以y b / a 输出结果 2

% 取模 - 返回除法的余数 b % a 输出结果 0

** 幂 - 返回x的y次幂 a**b 输出结果 20

// 取整除 - 返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0

以下实例演示了Python所有算术运算符的操作:

#!/usr/bin/python

a = 21

b = 10

c = 0

c = a + b

print "Line 1 - Value of c is ", c

c = a - b

print "Line 2 - Value of c is ", c

c = a * b

print "Line 3 - Value of c is ", c

c = a / b

print "Line 4 - Value of c is ", c

c = a % b

print "Line 5 - Value of c is ", c

a = 2

b = 3

c = a**b

print "Line 6 - Value of c is ", c

a = 10

b = 5

c = a//b

print "Line 7 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 31

Line 2 - Value of c is 11

Line 3 - Value of c is 210

Line 4 - Value of c is 2

Line 5 - Value of c is 1

Line 6 - Value of c is 8

Line 7 - Value of c is 2

Python比较运算符

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

运算符

描述

实例

== 等于 - 比较对象是否相等 (a == b) 返回 False。

!= 不等于 - 比较两个对象是否不相等 (a != b) 返回 true.

<> 不等于 - 比较两个对象是否不相等 (a <> b) 返回 true。这个运算符类似 != 。

> 大于 - 返回x是否大于y (a > b) 返回 False。

< 小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。 (a < b) 返回 true。

>= 大于等于 - 返回x是否大于等于y。 (a >= b) 返回 False。

<= 小于等于 - 返回x是否小于等于y。 (a <= b) 返回 true。

以下实例演示了Python所有比较运算符的操作:

#!/usr/bin/python

a = 21

b = 10

c = 0

if ( a == b ):

print "Line 1 - a is equal to b"

else:

print "Line 1 - a is not equal to b"

if ( a != b ):

print "Line 2 - a is not equal to b"

else:

print "Line 2 - a is equal to b"

if ( a <> b ):

print "Line 3 - a is not equal to b"

else:

print "Line 3 - a is equal to b"

if ( a < b ):

print "Line 4 - a is less than b"

else:

print "Line 4 - a is not less than b"

if ( a > b ):

print "Line 5 - a is greater than b"

else:

print "Line 5 - a is not greater than b"

a = 5;

b = 20;

if ( a <= b ):

print "Line 6 - a is either less than or equal to b"

else:

print "Line 6 - a is neither less than nor equal to b"

if ( b >= a ):

print "Line 7 - b is either greater than or equal to b"

else:

print "Line 7 - b is neither greater than nor equal to b"

以上实例输出结果:

Line 1 - a is not equal to b

Line 2 - a is not equal to b

Line 3 - a is not equal to b

Line 4 - a is not less than b

Line 5 - a is greater than b

Line 6 - a is either less than or equal to b

Line 7 - b is either greater than or equal to b

Python赋值运算符

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

运算符

描述

实例

= 简单的赋值运算符 c = a + b 将 a + b 的运算结果赋值为 c

+= 加法赋值运算符 c += a 等效于 c = c + a

-= 减法赋值运算符 c -= a 等效于 c = c - a

*= 乘法赋值运算符 c *= a 等效于 c = c * a

/= 除法赋值运算符 c /= a 等效于 c = c / a

%= 取模赋值运算符 c %= a 等效于 c = c % a

**= 幂赋值运算符 c **= a 等效于 c = c ** a

//= 取整除赋值运算符 c //= a 等效于 c = c // a

以下实例演示了Python所有赋值运算符的操作:

#!/usr/bin/python

a = 21

b = 10

c = 0

c = a + b

print "Line 1 - Value of c is ", c

c += a

print "Line 2 - Value of c is ", c

c *= a

print "Line 3 - Value of c is ", c

c /= a

print "Line 4 - Value of c is ", c

c = 2

c %= a

print "Line 5 - Value of c is ", c

c **= a

print "Line 6 - Value of c is ", c

c //= a

print "Line 7 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 31

Line 2 - Value of c is 52

Line 3 - Value of c is 1092

Line 4 - Value of c is 52

Line 5 - Value of c is 2

Line 6 - Value of c is 2097152

Line 7 - Value of c is 99864

Python位运算符

按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:

运算符

描述

实例

& 按位与运算符 (a & b) 输出结果 12 ,二进制解释: 0000 1100

| 按位或运算符 (a | b) 输出结果 61 ,二进制解释: 0011 1101

^ 按位异或运算符 (a ^ b) 输出结果 49 ,二进制解释: 0011 0001

~ 按位取反运算符 (~a ) 输出结果 -61 ,二进制解释: 1100 0011, 在一个有符号二进制数的补码形式。

<< 左移动运算符 a << 2 输出结果 240 ,二进制解释: 1111 0000

>> 右移动运算符 a >> 2 输出结果 15 ,二进制解释: 0000 1111

以下实例演示了Python所有位运算符的操作:

#!/usr/bin/python

a = 60 # 60 = 0011 1100

b = 13 # 13 = 0000 1101

c = 0

c = a & b; # 12 = 0000 1100

print "Line 1 - Value of c is ", c

c = a | b; # 61 = 0011 1101

print "Line 2 - Value of c is ", c

c = a ^ b; # 49 = 0011 0001

print "Line 3 - Value of c is ", c

c = ~a; # -61 = 1100 0011

print "Line 4 - Value of c is ", c

c = a << 2; # 240 = 1111 0000

print "Line 5 - Value of c is ", c

c = a >> 2; # 15 = 0000 1111

print "Line 6 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is -61

Line 5 - Value of c is 240

Line 6 - Value of c is 15

Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:

运算符

描述

实例

and 布尔"与" - 如果x为False,x and y返回False,否则它返回y的计算值。 (a and b) 返回 true。

or 布尔"或" - 如果x是True,它返回True,否则它返回y的计算值。 (a or b) 返回 true。

not 布尔"非" - 如果x为True,返回False。如果x为False,它返回True。 not(a and b) 返回 false。

以下实例演示了Python所有逻辑运算符的操作:

#!/usr/bin/python

a = 10

b = 20

c = 0

if ( a and b ):

print "Line 1 - a and b are true"

else:

print "Line 1 - Either a is not true or b is not true"

if ( a or b ):

print "Line 2 - Either a is true or b is true or both are true"

else:

print "Line 2 - Neither a is true nor b is true"

a = 0

if ( a and b ):

print "Line 3 - a and b are true"

else:

print "Line 3 - Either a is not true or b is not true"

if ( a or b ):

print "Line 4 - Either a is true or b is true or both are true"

else:

print "Line 4 - Neither a is true nor b is true"

if not( a and b ):

print "Line 5 - a and b are true"

else:

print "Line 5 - Either a is not true or b is not true"

以上实例输出结果:

Line 1 - a and b are true

Line 2 - Either a is true or b is true or both are true

Line 3 - Either a is not true or b is not true

Line 4 - Either a is true or b is true or both are true

Line 5 - a and b are true

Python成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

运算符

描述

实例

in 如果在指定的序列中找到值返回True,否则返回False。 x 在 y序列中 , 如果x在y序列中返回True。

not in 如果在指定的序列中没有找到值返回True,否则返回False。 x 不在 y序列中 , 如果x不在y序列中返回True。

以下实例演示了Python所有成员运算符的操作:

#!/usr/bin/python

a = 10

b = 20

list = [1, 2, 3, 4, 5 ];

if ( a in list ):

print "Line 1 - a is available in the given list"

else:

print "Line 1 - a is not available in the given list"

if ( b not in list ):

print "Line 2 - b is not available in the given list"

else:

print "Line 2 - b is available in the given list"

a = 2

if ( a in list ):

print "Line 3 - a is available in the given list"

else:

print "Line 3 - a is not available in the given list"

以上实例输出结果:

Line 1 - a is not available in the given list

Line 2 - b is not available in the given list

Line 3 - a is available in the given list

Python身份运算符

身份运算符用于比较两个对象的存储单元

运算符

描述

实例

is is是判断两个标识符是不是引用自一个对象 x is y, 如果 id(x) 等于 id(y) , is 返回结果 1

is not is not是判断两个标识符是不是引用自不同对象 x is not y, 如果 id(x) 不等于 id(y). is not返回结果 1

以下实例演示了Python所有身份运算符的操作:

#!/usr/bin/python

a = 20

b = 20

if ( a is b ):

print "Line 1 - a and b have same identity"

else:

print "Line 1 - a and b do not have same identity"

if ( id(a) == id(b) ):

print "Line 2 - a and b have same identity"

else:

print "Line 2 - a and b do not have same identity"

b = 30

if ( a is b ):

print "Line 3 - a and b have same identity"

else:

print "Line 3 - a and b do not have same identity"

if ( a is not b ):

print "Line 4 - a and b do not have same identity"

else:

print "Line 4 - a and b have same identity"

以上实例输出结果:

Line 1 - a and b have same identity

Line 2 - a and b have same identity

Line 3 - a and b do not have same identity

Line 4 - a and b do not have same identity

Python运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符

描述

** 指数 (最高优先级)

~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)

* / % // 乘,除,取模和取整除

+ - 加法减法

>> << 右移,左移运算符

& 位 'AND'

^ | 位运算符

<= < > >= 比较运算符

<> == != 等于运算符

= %= /= //= -= += *= **= 赋值运算符

is is not 身份运算符

in not in 成员运算符

not or and 逻辑运算符

以下实例演示了Python所有运算符优先级的操作:

#!/usr/bin/python

a = 20

b = 10

c = 15

d = 5

e = 0

e = (a + b) * c / d #( 30 * 15 ) / 5

print "Value of (a + b) * c / d is ", e

e = ((a + b) * c) / d # (30 * 15 ) / 5

print "Value of ((a + b) * c) / d is ", e

e = (a + b) * (c / d); # (30) * (15/5)

print "Value of (a + b) * (c / d) is ", e

e = a + (b * c) / d; # 20 + (150/5)

print "Value of a + (b * c) / d is ", e

以上实例输出结果:

Value of (a + b) * c / d is 90

Value of ((a + b) * c) / d is 90

Value of (a + b) * (c / d) is 90

Value of a + (b * c) / d is 50

更多技巧请《转发 + 关注》哦!

相关推荐

大文件传不动?WinRAR/7-Zip 入门到高手,这 5 个技巧让你效率翻倍

“这200张照片怎么传给女儿?微信发不了,邮箱附件又超限……”62岁的张阿姨对着电脑犯愁时,儿子只用了3分钟就把照片压缩成一个文件,还教她:“以后用压缩软件,比打包行李还方便!”职场人更懂这...

电脑解压缩软件推荐——7-Zip:免费、高效、简洁的文件管理神器

在日常工作中,我们经常需要处理压缩文件。无论是下载软件包、接收文件,还是存储大量数据,压缩和解压缩文件都成为了我们日常操作的一部分。而说到压缩解压软件,7-Zip绝对是一个不可忽视的名字。今天,我就来...

设置了加密密码zip文件要如何打开?这几个方法可以试试~

Zip是一种常见的压缩格式文件,文件还可以设置密码保护。那设置了密码的Zip文件要如何打开呢?不清楚的小伙伴一起来看看吧。当我们知道密码想要打开带密码的Zip文件,我们需要用到适用于Zip格式的解压缩...

大文件想要传输成功,怎么把ZIP文件分卷压缩

不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?方法一:使用7-Zip免费且强大的文件管理工具7...

高效处理 RAR 分卷压缩包:合并解压操作全攻略

在文件传输和存储过程中,当遇到大文件时,我们常常会使用分卷压缩的方式将其拆分成多个较小的压缩包,方便存储和传输。RAR作为一种常见的压缩格式,分卷压缩包的使用频率也很高。但很多人在拿到RAR分卷...

2个方法教你如何删除ZIP压缩包密码

zip压缩包设置了加密密码,每次解压文件都需要输入密码才能够顺利解压出文件,当压缩包文件不再需要加密的时候,大家肯定想删除压缩包密码,或是忘记了压缩包密码,想要通过删除操作将压缩包密码删除,就能够顺利...

速转!漏洞预警丨压缩软件Winrar目录穿越漏洞

WinRAR是一款功能强大的压缩包管理器,它是档案工具RAR在Windows环境下的图形界面。该软件可用于备份数据,缩减电子邮件附件的大小,解压缩从Internet上下载的RAR、ZIP及其它类...

文件解压方法和工具分享_文件解压工具下载

压缩文件减少文件大小,降低文件失效的概率,总得来说好处很多。所以很多文件我们下载下来都是压缩软件,很多小伙伴不知道怎么解压,或者不知道什么工具更好,所以今天做了文件解压方法和工具的分享给大家。一、解压...

[python]《Python编程快速上手:让繁琐工作自动化》学习笔记3

1.组织文件笔记(第9章)(代码下载)1.1文件与文件路径通过importshutil调用shutil模块操作目录,shutil模块能够在Python程序中实现文件复制、移动、改名和删除;同时...

Python内置tarfile模块:读写 tar 归档文件详解

一、学习目标1.1学习目标掌握Python内置模块tarfile的核心功能,包括:理解tar归档文件的原理与常见压缩格式(gzip/bz2/lzma)掌握tar文件的读写操作(创建、解压、查看、过滤...

使用python展开tar包_python拓展

类Unix的系统,打包文件经常使用的就是tar包,结合zip工具,可以方便的打包并解压。在python的标准库里面有tarfile库,可以方便实现生成了展开tar包。使用这个库最大的好处,可能就在于不...

银狐钓鱼再升级:白文件脚本化实现GO语言后门持久驻留

近期,火绒威胁情报中心监测到一批相对更为活跃的“银狐”系列变种木马。火绒安全工程师第一时间获取样本并进行分析。分析发现,该样本通过阿里云存储桶下发恶意文件,采用AppDomainManager进行白利...

ZIP文件怎么打开?2个简单方法教你轻松搞定!

在日常工作和生活中,我们经常会遇到各种压缩文件,其中最常见的格式之一就是ZIP。ZIP文件通过压缩数据来减少文件大小,方便我们进行存储和传输。然而,对于初学者来说,如何打开ZIP文件可能会成为一个小小...

Ubuntu—解压多个zip压缩文件.zip .z01 .z02

方法将所有zip文件放在同一目录中:zip_file.z01,zip_file.z02,zip_file.z03,...,zip_file.zip。在Zip3.0版本及以上,使用下列命令:将所有zi...

如何使用7-Zip对文件进行加密压缩

7-Zip是一款开源的文件归档工具,支持多种压缩格式,并提供了对压缩文件进行加密的功能。使用7-Zip可以轻松创建和解压.7z、.zip等格式的压缩文件,并且可以通过设置密码来保护压缩包中的...

取消回复欢迎 发表评论: