Operators in Programming: Arithmetic, Relational, Logical, and Bitwise | 编程运算符:算术、关系、逻辑与位运算

📚 Operators in Programming: Arithmetic, Relational, Logical, and Bitwise | 编程运算符:算术、关系、逻辑与位运算

Operators are the building blocks of any programming language, enabling you to perform calculations, make decisions, and manipulate data at the bit level. In your Edexcel A-Level Computer Science course, you will use Python to explore a wide range of operators, from simple arithmetic to bitwise operations. This article covers all essential operator categories, their syntax, precedence, and common pitfalls, helping you to write correct and efficient code in your exams and coursework.

运算符是任何编程语言的构建基石,使您能够执行计算、做出决策以及在位级别操作数据。在爱德思A-Level计算机科学课程中,您将使用Python探索各种运算符,从简单的算术运算到位运算。本文涵盖了所有基本运算符类别、语法、优先级和常见陷阱,帮助您在考试和课程作业中编写正确高效的代码。


1. Arithmetic Operators | 算术运算符

Arithmetic operators handle standard mathematical operations such as addition, subtraction, multiplication, division, modulus, exponentiation, and floor division. Python treats the division operators with extra care: the single slash / always returns a float, while double slash // performs floor division to give an integer result (truncated towards negative infinity).

算术运算符处理加法、减法、乘法、除法、取模、指数和整除等标准数学运算。Python对除法运算符有特殊处理:单斜杠 / 总是返回浮点数,而双斜杠 // 执行整除运算,给出整数结果(向负无穷方向截断)。

Operator Name / 名称 Example / 示例 Result / 结果
+ Addition
加法
7 + 3 10
Subtraction
减法
7 – 3 4
* Multiplication
乘法
7 * 3 21
/ Division
除法
7 / 2 3.5
% Modulus
取模
7 % 2 1
** Exponentiation
指数
2 ** 3 8
// Floor division
整除
-7 // 2 -4

Note that the exponentiation operator ** is right‑associative, meaning 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 512, not (2 ** 3) ** 2.

请注意指数运算符 ** 是右结合的,意味着 2 ** 3 ** 2 会被计算为 2 ** (3 ** 2) = 512,而不是 (2 ** 3) ** 2


2. Comparison (Relational) Operators | 比较(关系)运算符

Comparison operators test the relationship between two values and return a Boolean result (True or False). They are essential for constructing conditions in selection statements and loops. Python supports six such operators, which work with numbers, strings, and many other objects.

比较运算符测试两个值之间的关系并返回布尔值(TrueFalse)。它们对于在条件语句和循环中构造条件至关重要。Python支持六种此类运算符,可用于数字、字符串和许多其他对象。

Operator Meaning / 含义 Example / 示例 Result / 结果
== Equal to
等于
5 == 5 True
!= Not equal to
不等于
5 != 3 True
> Greater than
大于
5 > 3 True
< Less than
小于
5 < 3 False
>= Greater than or equal
大于等于
5 >= 5 True
<= Less than or equal
小于等于
5 <= 4 False

When comparing strings, Python uses lexicographical order based on Unicode code points. For example, 'apple' < 'banana' is True. Avoid confusing the assignment operator = with the equality operator ==, a common source of logical errors.

比较字符串时,Python使用基于Unicode码点的字典顺序。例如 'apple' < 'banana'True。切勿混淆赋值运算符 = 和相等运算符 ==,这是逻辑错误的常见来源。


3. Logical Operators | 逻辑运算符

Logical operators combine Boolean expressions and are often used to form complex conditions. Python provides and, or, and not. These operators employ short‑circuit evaluation: if the outcome can be determined by evaluating the left operand alone, the right operand is not evaluated, which can improve performance and prevent side effects.

逻辑运算符组合布尔表达式,通常用于形成复杂的条件。Python提供了 andornot。这些运算符采用短路求值:如果仅通过计算左操作数就能确定结果,则不会计算右操作数,这样可以提高性能并避免副作用。

The truth table for and requires both operands to be True for the result to be True. For or, the result is True if at least one operand is True. not simply negates the Boolean value. Examples: (5 > 3) and (2 < 4) yields True; not (5 == 5) yields False.

and 的真值表要求两个操作数都为 True 结果才为 True。对于 or,只要至少一个操作数为 True,结果就为 Truenot 简单地取反布尔值。示例:(5 > 3) and (2 < 4) 结果为 Truenot (5 == 5) 结果为 False

Python also treats non‑Boolean objects in a Boolean context: 0, None, empty sequences and mappings are considered False; most other objects are True. Consequently, 0 and 5 returns 0 (the first falsy value), while 'hello' or 0 returns 'hello' (the first truthy value).

Python还在布尔上下文中处理非布尔对象:0None、空序列和映射被视为 False;大多数其他对象被视为 True。因此 0 and 5 返回 0(第一个假值),而 'hello' or 0 返回 'hello'(第一个真值)。


4. Bitwise Operators | 位运算符

Bitwise operators act on the individual bits of integer values. They are particularly useful in low‑level programming, graphics, encryption, and when working with hardware interfaces. Python offers & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).

位运算符作用于整数值的各个二进制位。它们在底层编程、图形学、加密以及与硬件接口交互时特别有用。Python提供了 &(按位与)、|(按位或)、^(按位异或)、~(按位取反)、<<(左移)和 >>(右移)。

Operator Description / 描述 Example / 示例 Binary explanation / 二进制解释
& Bitwise AND
按位与
5 & 3 → 1 101 & 011 = 001
| Bitwise OR
按位或
5 | 3 → 7 101 | 011 = 111
^ Bitwise XOR
按位异或
5 ^ 3 → 6 101 ^ 011 = 110
~ Bitwise NOT
按位取反
~5 → -6 ~…101 → …010 (two’s complement)
<< Left shift
左移
5 << 1 → 10 101 → 1010 (multiply by 2)
>> Right shift
右移
5 >> 1 → 2 101 → 10 (integer division by 2)

The bitwise NOT ~x returns -x - 1 due to Python’s use of two’s complement representation. Shifts effectively multiply or divide by powers of two, but right shift with negative numbers preserves the sign by copying the leftmost bit (arithmetic shift).

按位取反 ~x 返回 -x - 1,因为Python使用二进制补码表示。移位运算实际上相当于乘以或除以2的幂次,但对负数进行右移时,会通过复制最左侧的位来保留符号(算术移位)。


5. Assignment Operators | 赋值运算符

The basic assignment operator = stores a value in a variable. Compound assignment operators combine an arithmetic or bitwise operation with assignment, making the code more concise. For example, x += 5 is equivalent to x = x + 5.

基本赋值运算符 = 将值存储到变量中。复合赋值运算符将算术或位运算与赋值结合起来,使代码更加简洁。例如 x += 5 等效于 x = x + 5

<

Published by TutorHao | A-Level 编程 Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Operator Equivalent / 等效形式 Example (x=10) / 示例 Final x / 最终x
+= x = x + 2 x += 3 13
-= x = x – 2 x -= 4 6
*= x = x * 2 x *= 2 20