Operators and Expressions in Programming | 编程中的运算符与表达式 · Precedence and Assoc

📚 Operators and Expressions in Programming | 编程中的运算符与表达式

Operators are the symbols that tell a computer to perform specific mathematical, relational or logical operations. In Edexcel A-Level Computer Science, you must be able to read, write and trace expressions that combine operators and operands.

运算符是告诉计算机执行特定数学、关系或逻辑操作的符号。在 Edexcel A-Level 计算机科学中,你必须能够阅读、编写并追踪结合了运算符和操作数的表达式。

1. Why Operators Matter | 为什么运算符重要

Operators form the core of every executable instruction. Without them, a program could store data but never calculate, compare or decide.

运算符构成了每条可执行指令的核心。没有它们,程序只能存储数据,却无法计算、比较或做出决策。

  • Arithmetic operators build formulas for prices, coordinates and counters. 算术运算符用于为价格、坐标和计数器构建公式。
  • Comparison operators drive conditional statements such as IF and WHILE. 比较运算符驱动 IF 和 WHILE 等条件语句。
  • Logical operators combine multiple true/false conditions in selection. 逻辑运算符在选择结构中组合多个真/假条件。

2. Arithmetic Operators | 算术运算符

The usual arithmetic operators are addition, subtraction, multiplication, division, modulus, integer division and exponentiation. In Python they are written +, -, *, /, %, //, **.

常见的算术运算符包括加、减、乘、除、取模、整除和乘方。在 Python 中它们分别写作 +、-、*、/、%、//、**。

  • + addition 加;- subtraction 减;* multiplication 乘
  • / division 除;% modulus 取模;// integer division 整除;** exponentiation 乘方

17 % 5 = 2    and    17 // 5 = 3    and    2 ** 3 = 8

Remember that % gives the remainder after division, while // discards the fractional part and returns an integer in Python.

请记住,% 给出除法后的余数,而 // 在 Python 中丢弃小数部分并返回整数。


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

Comparison operators compare two values and return a Boolean result: True or False. They are essential in selection and iteration.

比较运算符比较两个值并返回布尔结果:True 或 False。它们在选择和迭代中是必不可少的。

  • == means equal to;!= means not equal to. == 表示等于;!= 表示不等于。
  • >, <, >=, <= test order relationships. >、<、>=、<= 测试大小关系。

In Edexcel pseudocode, the symbols may be written as = or == depending on the context, so always check the question wording.

在 Edexcel 伪代码中,符号可能根据上下文写作 = 或 ==,因此务必核对题目措辞。


4. Logical Operators | 逻辑运算符

Logical operators act on Boolean values. Python uses and, or and not. Many pseudocode specifications show AND, OR, NOT.

逻辑运算符作用于布尔值。Python 使用 and、or 和 not。许多伪代码规范显示 AND、OR、NOT。

  • and returns True only if both operands are True. and 仅当两个操作数都为 True 时才返回 True。
  • or returns True if at least one operand is True. or 只要有一个操作数为 True 就返回 True。
  • not reverses the Boolean value. not 反转布尔值。

(age >= 18) and (country == ‘UK’)

The expression above is True only when both the age condition and the country condition are True.

上面的表达式仅当年龄条件和国家条件都为 True 时才为 True。


5. Bitwise Operators | 位运算符

Bitwise operators work on binary representations of integers. They are less common in A-Level exams, but you may need to trace bit-level operations such as AND, OR, XOR, NOT, left shift and right shift.

位运算符对整数的二进制表示进行操作。它们在 A-Level 考试中较少出现,但你可能需要追踪位级运算,例如 AND、OR、XOR、NOT、左移和右移。

  • & bitwise AND;| bitwise OR;^ bitwise XOR;~ bitwise NOT
  • << shifts bits left;>> shifts bits right. << 将位向左移;>> 将位向右移。

Example: 5 & 3 = 1 because 0101 and 0011 produce 0001.

示例:5 & 3 = 1,因为 0101 与 0011 得到 0001。


6. Assignment Operators | 赋值运算符

The basic assignment operator is =. Compound assignment operators combine arithmetic with assignment, which shortens code.

基本赋值运算符是 =。复合赋值运算符将算术与赋值结合起来,可以缩短代码。

  • x += 5 is equivalent to x = x + 5. x += 5 等价于 x = x + 5。
  • x *= 2 is equivalent to x = x * 2. x *= 2 等价于 x = x * 2。
  • x //= 3 and x %= 3 behave similarly for integer division and remainder. x //= 3 和 x %= 3 分别类似地执行整除和取余。

7. Membership and Identity Operators | 成员与身份运算符

Membership operators test whether a value exists in a sequence. Identity operators test whether two references point to the same object in memory.

成员运算符测试一个值是否存在于序列中。身份运算符测试两个引用是否指向内存中的同一个对象。

  • in returns True if the element is present in a list, string or tuple. in 如果元素存在于列表、字符串或元组中,则返回 True。
  • not in returns True if the element is absent. not in 如果元素不存在,则返回 True。
  • is checks identity; == checks equality of value. is 检查身份;== 检查值的相等性。

8. Precedence and Associativity | 优先级与结合性

Operator precedence decides the order in which operations are evaluated. In most languages, brackets have the highest priority, then exponent, then multiplication/division, then addition/subtraction, then comparisons, then logical operators.

运算符优先级决定了运算的求值顺序。在大多数语言中,括号优先级最高,然后是乘方、乘除、加减,再然后是比较运算,最后是逻辑运算。

  • Parentheses override all default priority rules. 括号覆盖所有默认优先级规则。
  • *, /, //, % are evaluated before + and -. *、/、//、% 在 + 和 – 之前计算。
  • and is evaluated after comparisons but before or in Python. 在 Python 中,and 在比较之后、or 之前计算。

Example: 3 + 4 * 2 equals 11, not 14.

示例:3 + 4 * 2 等于 11,而不是 14。


9. Common Pitfalls in A-Level Exam | A-Level 考试常见易错点

Students often confuse = and ==, misuse integer division, or ignore short-circuit evaluation in logical expressions.

学生经常混淆 = 和 ==,误用整除,或忽略逻辑表达式中的短路求值。

  • Using = for comparison in Python causes a syntax error or unintended assignment. 在 Python 中误将 = 用作比较会导致语法错误或意外赋值。
  • In many languages, / gives a real result while // gives integer division in Python. 在许多语言中,/ 给出实数结果,而 Python 中的 // 给出整除。
  • Check whether the question uses pseudocode or Python; operator symbols can differ. 检查题目使用伪代码还是 Python;运算符符号可能不同。

10. Quick Revision Table | 快速复习表

Operator Meaning 含义 Example 示例
+ Addition 加 3 + 2 = 5
% Modulus 取模 7 % 4 = 3
// Integer division 整除 7 // 4 = 1
== Equal to 等于 5 == 5 → True
and Logical AND 逻辑与 True and False → False
in Membership 成员 ‘a’ in ‘cat’ → True

Use this table as a quick check before the exam, but always practise tracing full expressions with mixed operators.

在考试前可以用此表快速自查,但务必练习追踪包含混合运算符的完整表达式。


Published by TutorHao | Programming 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