什么是运算符?
本章节主要说明Python的运算符。举个简单的例子** 4 +5 = 9 。 例子中,4** 和 5 被称为操作数,"+" 称为运算符。
Python语言支持以下类型的运算符:
- 算术运算符
- 比较(关系)运算符
- 赋值运算符
- 逻辑运算符
- 位运算符
- 成员运算符
- 身份运算符
- 运算符优先级
接下来让我们一个个来学习Python的运算符。
Python算术运算符Å
以下假设变量a为10,变量b为20:

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

以上实例输出结果:Å

ÅPython比较运算符
以下假设变量a为10,变量b为20:

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

以上实例输出结果:

Python赋值运算符
以下假设变量a为10,变量b为20:

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

以上实例输出结果:

Python位运算符
按位运算符是把数字看作二进制来进行计算的。
Python中的按位运算法则如下:
下表中变量 a 为 60,b 为 13,二进制格式如下:


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

以上实例输出结果:

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

以上实例输出结果:

以上实例输出结果:

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

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

以上实例输出结果:

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

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

以上实例输出结果:

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

运算符描述*指数 (最高优先级)~ + -按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) / % //乘,除,取模和取整除+ -加法减法>> <<右移,左移运算符&位 'AND'^ |位运算符<= < > >=比较运算符<> == !=等于运算符= %= /= //= -= += *= **=赋值运算符is is not身份运算符in not in成员运算符not or and逻辑运算符
以下实例演示了Python所有运算符优先级的操作:

以上实例输出结果:

网友评论