Operators in Programming | 编程中的运算符 · Assignment Operators

📚 Operators in Programming | 编程中的运算符

Operators are fundamental building blocks in programming. They allow a program to perform calculations, compare values, build logical decisions, and manipulate data at the bit level. In the Edexcel A Level specification, you must be able to identify and apply arithmetic, relational, logical, bitwise, and assignment operators confidently.

运算符是编程中最基本的构件。它们让程序能够执行计算、比较数值、构建逻辑判断,并在位级别上操作数据。在 Edexcel A Level 大纲中,你必须能够熟练识别并应用算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符。

1. What Is an Operator? | 什么是运算符?

An operator is a symbol or keyword that tells the compiler or interpreter to perform a specific operation on one or more operands. Operands are the values or variables on which the operator acts.

运算符是告诉编译器或解释器对一个或多个操作数执行特定操作的符号或关键字。操作数就是运算符所作用的值或变量。

For example, in the expression a + b, ‘+’ is the operator and a and b are operands. Understanding this terminology is essential for reading and writing code accurately.

例如,在表达式 a + b 中,’+’ 是运算符,a 和 b 是操作数。理解这一术语对于准确读写代码至关重要。


2. Arithmetic Operators | 算术运算符

Arithmetic operators perform basic mathematical calculations. The common arithmetic operators are addition (+), subtraction (-), multiplication (× or *), division (/), integer division (//), modulus (%), and exponentiation (** or ^ depending on the language).

算术运算符用于执行基本的数学计算。常见的算术运算符包括加(+)、减(-)、乘(× 或 *)、除(/)、整数除法(//)、取模(%)和幂运算(** 或 ^,取决于语言)。

In Python, which is widely used in Edexcel courses, integer division uses ‘//’ and exponentiation uses ‘**’. For example, 7 // 2 evaluates to 3, and 2 ** 3 evaluates to 8.

在 Edexcel 课程广泛使用的 Python 中,整数除法使用 ‘//’,幂运算使用 ‘**’。例如,7 // 2 的结果是 3,2 ** 3 的结果是 8。

Operator 运算符 Symbol 符号 Example 示例 Result 结果
Addition 加法 + 5 + 3 8
Subtraction 减法 – 5 – 3 2
Multiplication 乘法 * 5 * 3 15
Division 除法 / 5 / 2 2.5
Integer division 整数除法 // 5 // 2 2
Modulus 取余 % 5 % 2 1
Exponentiation 幂运算 ** 5 ** 2 25

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

Comparison operators compare two values and return a Boolean result: True or False. They are used extensively in selection statements such as if-else and in loop conditions.

比较运算符用于比较两个值并返回布尔结果:真或假。它们广泛用于选择结构(如 if-else)和循环条件中。

The main comparison operators are equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

主要的比较运算符包括等于(==)、不等于(!=)、大于(>)、小于(<)、大于等于(>=)和小于等于(<=)。

A common mistake is to use a single ‘=’ instead of ‘==’ when checking equality. In many languages, ‘=’ is assignment, while ‘==’ is comparison.

一个常见错误是在检查相等性时使用单个 ‘=’ 而不是 ‘==’。在许多语言中,’=’ 表示赋值,而 ‘==’ 表示比较。


4. Logical Operators | 逻辑运算符

Logical operators combine Boolean expressions and produce a Boolean result. The three standard logical operators are AND, OR, and NOT.

逻辑运算符将布尔表达式组合起来,产生布尔结果。三种标准逻辑运算符是 AND、OR 和 NOT。

In Python, they are written as ‘and’, ‘or’, and ‘not’. In pseudocode and formal logic, the symbols ∧, ∨, and ¬ are often used.

在 Python 中,它们写作 ‘and’、’or’ 和 ‘not’。在伪代码和形式逻辑中,通常使用符号 ∧、∨ 和 ¬。

Truth tables define the outcome of these operators. For example, A AND B is True only when both A and B are True.

真值表定义了这些运算符的输出。例如,只有当 A 和 B 都为真时,A AND B 才为真。

A B A AND B A OR B NOT A
True 真 True 真 True 真 True 真 False 假
True 真 False 假 False 假 True 真 False 假
False 假 True 真 False 假 True 真 True 真
False 假 False 假 False 假 False 假 True 真

5. Bitwise Operators | 位运算符

Bitwise operators act on the individual bits of integer values. They are useful in low-level programming, compression, encryption, and efficient arithmetic.

位运算符作用于整数值的各个二进制位。它们在底层编程、压缩、加密和高效算术中非常有用。

Common bitwise operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>).

常见的位运算符包括按位与(&)、按位或(|)、按位异或(^)、按位取反(~)、左移(<<)和右移(>>)。

For example, 5 & 3 evaluates to 1 because 5 is 0101₂ and 3 is 0011₂; the AND operation gives 0001₂.

例如,5 & 3 的结果是 1,因为 5 是 0101₂,3 是 0011₂,按位与运算得到 0001₂。


6. Assignment Operators | 赋值运算符

Assignment operators store values in variables. The basic assignment operator is ‘=’. Compound assignment operators combine an arithmetic or bitwise operation with assignment.

赋值运算符用于将值存储到变量中。基本的赋值运算符是 ‘=’。复合赋值运算符将算术或位运算与赋值结合起来。

Examples include ‘+=’ (add and assign), ‘-=’ (subtract and assign), ‘*=’ (multiply and assign), ‘/=’ (divide and assign), and ‘%=’ (modulus and assign).

示例包括 ‘+=’(加后赋值)、’-=’(减后赋值)、’*=’(乘后赋值)、’/=’(除后赋值)和 ‘%=’(取模后赋值)。

The expression x += 5 is equivalent to x = x + 5. Compound operators can make code shorter and sometimes clearer.

表达式 x += 5 等价于 x = x + 5。复合运算符可以使代码更短,有时也更清晰。


7. Operator Precedence | 运算符优先级

Operator precedence determines the order in which operators are evaluated in an expression. Higher-precedence operators are evaluated before lower-precedence ones.

运算符优先级决定了表达式中各运算符的求值顺序。优先级高的运算符先于优先级低的运算符求值。

For example, in 3 + 4 * 2, multiplication has higher precedence than addition, so the result is 11, not 14.

例如,在 3 + 4 * 2 中,乘法的优先级高于加法,因此结果是 11,而不是 14。

3 + 4 × 2 = 11

The general order from highest to lowest is: parentheses, exponentiation, unary plus/minus, multiplication/division/modulus, addition/subtraction, comparison, logical NOT, logical AND, logical OR, and assignment.

从高到低的一般顺序是:括号、幂运算、一元正负号、乘/除/取模、加/减、比较、逻辑非、逻辑与、逻辑或、赋值。


8. Associativity and Evaluation Order | 结合性与求值顺序

When operators have the same precedence, associativity decides the direction of evaluation. Most arithmetic operators are left-associative, meaning they are evaluated from left to right.

当运算符具有相同优先级时,结合性决定求值方向。大多数算术运算符是左结合的,即从左到右求值。

For example, 20 / 5 / 2 is evaluated as (20 / 5) / 2 = 2, not 20 / (5 / 2).

例如,20 / 5 / 2 的求值方式是 (20 / 5) / 2 = 2,而不是 20 / (5 / 2)。

Exponentiation is right-associative in many languages

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

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

📞 咨询 / 微信:16621398022(同微信)
A-Level / IGCSE / IB / AP 课程辅导 · 真题资料 · 留学申请规划
查看联系方式 →

Comments

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

Turn on JavaScript to leave a comment.

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

咨询微信:16621398022(同微信) · 联系我们
Exit mobile version