📚 Mastering Operators and Expressions in Edexcel A-Level Programming | 精通 Edexcel A-Level 编程中的运算符与表达式
Every Edexcel A-Level programming question eventually comes down to evaluating expressions: predicting what a line of code does, writing a condition for an if statement, or tracing a loop until a variable changes. This revision guide focuses on operators and expressions, using Python-style syntax alongside the Edexcel pseudocode conventions you are likely to meet in Paper 2.
每一道 Edexcel A-Level 编程题最终都会回到表达式求值:预测某行代码的运行结果、为 if 语句写出条件,或者追踪循环直到变量发生变化。本复习指南聚焦运算符与表达式,使用 Python 风格语法,并结合你在 Paper 2 中可能遇到的 Edexcel 伪代码规范。
The guide covers the full operator family - arithmetic, integer division and MOD, relational, Boolean, string and assignment operators - together with precedence rules, type conversion, trace tables and the classic mistakes that cost marks.
本指南覆盖完整的运算符家族——算术、整除与 MOD、关系、布尔、字符串与赋值运算符——并结合优先级规则、类型转换、跟踪表以及最常见的失分错误。
1. Operators and Expressions: Core Concepts | 运算符与表达式:核心概念
An operator is a symbol that tells the computer to perform a specific operation on one or more operands. An expression is any valid combination of values, variables, operators and function calls that can be evaluated to produce a single value.
运算符是告诉计算机对一个或多个操作数执行特定操作的符号。表达式是值、变量、运算符和函数调用的任意合法组合,能够被求值并产生一个单一的值。
Operators are classified by how many operands they take. A unary operator works on one operand, such as the minus sign in -5 or the Boolean operator NOT. A binary operator works on two operands, such as the plus sign in a + b.
运算符可以按操作数的数量分类。一元运算符只作用于一个操作数,例如 -5 中的负号或布尔运算符 NOT。二元运算符作用于两个操作数,例如 a + b 中的加号。
For example, in total = price * quantity + 10, the expression price * quantity + 10 is evaluated first and then assigned to the variable total. This distinction between expression evaluation and assignment is important in trace tables and written answers.
例如,在 total = price * quantity + 10 中,先计算表达式 price * quantity + 10,再把结果赋给变量 total。表达式求值与赋值之间的区别在跟踪表和书面答案中十分重要。
Expressions also have a data type. The type of an expression is determined by its operands and operators, so 3 + 4 produces an integer while 3 / 4 produces a real number. Examiners frequently ask you to state the value and the data type of an expression.
表达式也有数据类型。表达式的类型由其操作数和运算符共同决定,因此 3 + 4 产生整数,而 3 / 4 产生实数。考官经常会要求你写出表达式的值以及它的数据类型。
expression → evaluated to a single value → assigned or used in a statement
表达式 → 求值为单一值 → 赋给变量或用于语句
2. Arithmetic Operators | 算术运算符
The arithmetic operators perform basic mathematical calculations. Edexcel questions often expect you to know both the exact division operator and the integer-division operator, because they behave differently in Python and in pseudocode.
算术运算符执行基本数学计算。Edexcel 题目经常要求你同时掌握精确除法运算符和整数除法运算符,因为它们在 Python 和伪代码中的行为不同。
Key arithmetic operators are summarised below. The result column assumes a = 7 and b = 2.
下表汇总了关键的算术运算符。结果列假设 a = 7、b = 2。
| Operator 运算符 | Meaning 含义 | Example 示例 | Result 结果 |
| + | Addition 加法 | a + b | 9 |
| - | Subtraction 减法 | a - b | 5 |
| * | Multiplication 乘法 | a * b | 14 |
| / | Real division 精确除法 | a / b | 3.5 |
| DIV / // | Integer division 整除 | a DIV b | 3 |
| MOD / % | Remainder 取余 | a MOD b | 1 |
| ** | Exponent 幂 | b ** 3 | 8 |
A very common exam trap is that / always returns a real number in Python, even when the division is exact: 6 / 2 gives 3.0, not 3. If a question asks for an integer result, you must use DIV or // instead.
一个非常常见的考试陷阱是:在 Python 中 / 永远返回实数,即使能整除也是如此:6 / 2 得到 3.0 而不是 3。如果题目要求整数结果,就必须改用 DIV 或 //。
Note that ** is Python' exponent operator, whereas the caret symbol ^ is a bitwise XOR in Python and does not mean "to the power of". Never write 2 ^ 3 and expect 8; that expression evaluates to 1.
请注意,** 才是 Python 的幂运算符,而脱字符 ^ 在 Python 中是按位异或,并不表示"乘方"。千万不要写 2 ^ 3 并期待得到 8,该表达式的值是 1。
3. Integer Division, MOD and Remainder | 整除、取模与余数
DIV discards the fractional part of a division and keeps only the whole-number quotient; MOD returns the remainder. Together they let you split a number into a quotient and a remainder, which is the basis of many algorithms.
DIV 舍弃除法的小数部分,只保留整数商;MOD 返回余数。两者配合可以把一个数拆成商和余数,这是许多算法的基础。
For any integers a and b (b ≠ 0), the identity below always holds. Understanding it helps you check your own answers in the exam.
对于任意整数 a 与 b(b ≠ 0),下面的恒等式始终成立。理解它有助于在考试中自行验算。
a = (a DIV b) × b + (a MOD b)
a = (a DIV b) × b + (a MOD b)
Typical uses include testing whether a number is even or odd with n MOD 2, extracting individual digits with a MOD 10 and a DIV 10, wrapping an index around a fixed-size array, and converting seconds into minutes and seconds.
典型用途包括:用 n MOD 2 判断奇偶,用 a MOD 10 和 a DIV 10 逐位提取数字,让下标在定长数组中循环回头,以及把秒数换算成分钟和秒。
Behaviour with negative numbers differs between languages. Python uses floor division, so -7 // 2 is -4 and -7 % 2 is 1, whereas truncation towards zero would give -3 and -1. Unless a question explicitly involves negatives, stick to the conventional positive-integer interpretation.
负数行为在不同语言中并不一致。Python 采用向下取整除法,因此 -7 // 2 为 -4、-7 % 2 为 1,而向零截断则得到 -3 和 -1。除非题目明确涉及负数,否则按常规的正整数理解即可。
4. Operator Precedence and BODMAS | 运算符优先级与运算顺序
Operator precedence determines the order in which operators are applied when an expression contains more than one. If you ignore it, you will produce the wrong value even though your Python syntax is perfect.
运算符优先级决定了一个含多个运算符的表达式的计算顺序。若忽略优先级,即使 Python 语法完全正确,你也会得出错误的值。
The order from highest to lowest is: brackets, exponent, unary minus, multiplication and division operators (including DIV, MOD), addition and subtraction, then comparisons, then NOT, then AND, then OR.
从高到低的顺序是:括号、幂、一元负号、乘除类运算符(含 DIV、MOD)、加减、比较运算、NOT、AND、OR。
Operators of equal precedence are evaluated left to right. This is why 20 - 5 - 3 gives 12 rather than 18, and why 100 DIV 10 * 2 gives 20 rather than 5.
同级运算符按从左到右的顺序计算。这就是为什么 20 - 5 - 3 得到 12 而不是 18,也是为什么 100 DIV 10 * 2 得到 20 而不是 5。
2 + 3 × 4² = 2 + 3 × 16 = 2 + 48 = 50
2 + 3 × 4² = 2 + 3 × 16 = 2 + 48 = 50
In written answers, always show one line of working per step: brackets first, then powers, then multiplication and division, then addition and subtraction. That structure alone often earns method marks even if an arithmetic slip occurs.
在书面作答中,每一步单独写一行过程:先括号,再幂,再乘除,最后加减。即使出现算术失误,这种结构本身往往也能拿到方法分。
5. Relational (Comparison) Operators | 关系(比较)运算符
Relational operators compare two values and always produce a Boolean result: either True or False. They are the building blocks of every selection and every loop condition.
关系运算符比较两个值,结果始终是布尔值:True 或 False。它们是所有选择结构和循环条件的构成基础。
The standard set is shown below, with the difference between pseudocode and Python highlighted.
下表列出标准运算符集合,并突出显示伪代码与 Python 之间的差异。
| Meaning 含义 | Pseudocode 伪代码 | Python |
| Equal to 等于 | = | == |
| Not equal to 不等于 | ≠ | != |
| Less than 小于 | < | < |
| Greater than 大于 | > | > |
| Less than or equal 小于等于 | ≤ | <= |
| Greater than or equal 大于等于 | ≥ | >= |
Strings can be compared too, using lexicographic order based on Unicode code points. The character 'A' has code point 65 and 'a' has 97, so "Apple" < "apple" is True and "Zebra" < "apple" is also True.
字符串同样可以比较,依据的是基于 Unicode 码位的字典序。字符 'A' 的码位是 65,'a' 是 97,因此 "Apple" < "apple" 为 True,"Zebra" < "apple" 也为 True。
be written as x >= 0 AND x <= 10, because pseudocode evaluates each comparison separately.
不要在伪代码中使用数学式的连续比较。数学中的 0 ≤ x ≤ 10 必须写成 x >= 0 AND x <= 10,因为伪代码会分别计算每一个比较。
6. Boolean (Logical) Operators | 布尔(逻辑)运算符
Boolean operators combine or invert conditions. There are only three to learn: AND, OR and NOT, and their behaviour is fully defined by truth tables.
布尔运算符用于组合或反转条件。需要掌握的只有三个:AND、OR 和 NOT,它们的行为完全由真值表定义。
The table below shows every possible combination of two Boolean operands, A and B.
下表列出了两个布尔操作数 A 与 B 的所有可能组合。
| A | B | A AND B | A OR B | NOT A |
| False | False | False | False | True |
| False | True | False | True | True |
| True | False | False | True | False |
| True | True | True | True | False |
A useful rule of thumb: AND is only True when both sides are True, while OR is only False when both sides are False. De Morgan's laws let you rewrite negated expressions: NOT (A AND B) is equivalent to (NOT A) OR (NOT B).
一个实用的记忆法则:只有当两侧都为 True 时 AND 才为 True,而只有当两侧都为 False 时 OR 才为 False。德摩根定律可以重写取反表达式:NOT (A AND B) 等价于 (NOT A) OR (NOT B)。
Python uses short-circuit evaluation: in A AND B, if A is False the second condition is never evaluated. This matters when the second condition would otherwise cause an error, such as dividing by a value that might be zero.
Python 采用短路求值:在 A AND B 中,若 A 为 False,第二个条件根本不会被计算。当第二个条件本身可能引发错误(例如除以一个可能为零的值)时,这一点尤其重要。
When AND and OR appear together, NOT binds tightest, then AND, then OR. Use brackets to make your intention unambiguous - it is never penalised and it prevents logic errors.
当 AND 与 OR 同时出现时,NOT 结合最紧,其次是 AND,最后是 OR。请用括号明确表达意图——这不会被扣分,反而能避免逻辑错误。
7. String Operators | 字符串运算符
Strings support their own operators. Concatenation joins two strings end to end; in Python it uses +, while Edexcel pseudocode typically uses the ampersand symbol &.
字符串有自己的运算符。连接运算符把两个字符串首尾相接;在 Python 中使用 +,而在 Edexcel 伪代码中通常使用 & 符号。
Repetition uses *, so "ab" * 3 produces "ababab". Indexing uses square brackets and starts at position 0, so in name = "Alice", name[0] is "A" and name[4] is "e".
重复运算符使用 *,因此 "ab" * 3 得到 "ababab"。索引使用方括号且从位置 0 开始,因此在 name = "Alice" 中,name[0] 是 "A",name[4] 是 "e"。
Slicing extracts a substring using two indices, for example name[1:4] which returns "lic". Note that the start index is included but the end index is excluded - a detail that decides many one-mark questions.
切片用两个下标提取子串,例如 name[1:4] 返回 "lic"。注意起始下标包含在内、结束下标不包含——这个细节决定了许多一分的题目。
A frequent error is attempting to concatenate a string with a number, for example "Total: " + 42, which raises a TypeError. You must convert the number first using str(): "Total: " + str(42).
常见错误是把字符串与数字直接连接,例如 "Total: " + 42 会抛出 TypeError。必须先用 str() 转换数字:"Total: " + str(42)。
Strings are immutable, so individual characters cannot be reassigned: name[0] = "B" is invalid. Build a new string instead, or store the characters in a list.
字符串是不可变的,因此不能对单个字符重新赋值:name[0] = "B" 是非法的。应改为构造一个新字符串,或把字符存入列表。
8. Assignment and Augmented Assignment Operators | 赋值与复合赋值运算符
value of the right-hand expression in the variable on the left. It is a statement, not an expression, so it does not produce a value that can be used elsewhere.
赋值运算符 = 把右侧表达式的值存入左侧的变量。它是一条语句而不是表达式,因此不会产生可供其他地方使用的值。
Augmented assignment combines an arithmetic operation with assignment, giving a shorthand that examiners often use in loop bodies.
复合赋值把算术运算与赋值结合,形成一种简写形式,考官常在循环体中使用。
- count += 1 is equivalent to count = count + 1 / count += 1 等价于 count = count + 1
- total -= price is equivalent to total = total - price / total -= price 等价于 total = total - price
- score *= 2 is equivalent to score = score * 2 / score *= 2 等价于 score = score * 2
- average /= n is equivalent to average = average /
广告 / Sponsored2027年初级会计官方教材备考初级会计实务和经济法基础会计职称资格考试教材真题试卷必刷题押题app三色笔记网络课程财政…经济、会计 备考教辅 · 正版现货 · 学习备考通用查看详情 / View on Taobao参考价 ¥87.5广告 / Sponsored【正版书】 艺术管理学北京舞蹈学院规划教材 吕艺生 上海音乐出 吕艺生 著 上海音乐出版社艺术设计、音乐 备考教辅 · 正版现货 · 学习备考通用查看详情 / View on Taobao参考价 ¥15.55
更多咨询请联系16621398022(同微信)
备考资讯每日更新,关注不错过考纲变化 · 科目分析 · 学习方法
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图公众号:tutorhao