📚 Programming Operators: Essential Building Blocks for Edexcel A-Level | 编程运算符:Edexcel A-Level 的基础构建块
Operators are fundamental symbols in programming that perform specific operations on one or more operands. In the Edexcel A-Level Computer Science syllabus, understanding how to correctly use arithmetic, relational, logical, bitwise, and assignment operators is essential for writing effective code and solving algorithmic problems. This article explores the key operators you need to master for your exams, focusing on Python as the primary pseudocode language where applicable.
运算符是编程中的基本符号,用于对一个或多个操作数执行特定操作。在 Edexcel A-Level 计算机科学课程大纲中,正确使用算术、关系、逻辑、位运算和赋值运算符对于编写高效代码和解决算法问题至关重要。本文将探讨你需要在考试中掌握的关键运算符,重点以 Python 作为主要伪代码语言。
1. What Are Operators? | 什么是运算符?
An operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation and return a result. Operators operate on values called operands. For example, in the expression 5 + 3, ‘+’ is the arithmetic operator that adds the operands 5 and 3 to produce 8. Programming languages define a set of built-in operators, and the Edexcel specification expects you to know how they behave and how to use them in algorithms.
运算符是告诉编译器或解释器执行特定数学、关系或逻辑操作并返回结果的符号。运算符作用于被称为操作数的值。例如,在表达式 5 + 3 中,“+”是算术运算符,将操作数 5 和 3 相加得到 8。编程语言定义了一系列内置运算符,Edexcel 规范要求你了解它们的行为以及如何在算法中使用它们。
2. Arithmetic Operators | 算术运算符
Arithmetic operators perform basic mathematical calculations. They are the most frequently used operators in programming. The standard arithmetic operators in Python include addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation (**). It is crucial to understand the difference between true division (/) that returns a floating-point number and floor division (//) that returns the integer quotient.
算术运算符执行基本的数学计算。它们是编程中最常用的运算符。Python 中的标准算术运算符包括加法(+)、减法(-)、乘法(*)、除法(/)、取整除(//)、取模(%)和幂运算(**)。理解真除法(/)返回浮点数与取整除(//)返回整数商之间的区别至关重要。
-
Addition (+): Adds two operands. Example: 3 + 4 yields 7.
加法(+):将两个操作数相加。示例:3 + 4 得到 7。
-
Subtraction (-): Subtracts the right operand from the left. 10 – 3 yields 7.
减法(-):从左操作数中减去右操作数。10 – 3 得到 7。
-
Multiplication (*): Multiplies two operands. 5 * 6 yields 30.
乘法(*):将两个操作数相乘。5 * 6 得到 30。
-
Division (/): Divides the left operand by the right, always returning a float. 10 / 3 yields 3.3333333333333335.
除法(/):将左操作数除以右操作数,始终返回浮点数。10 / 3 得到 3.3333333333333335。
-
Floor division (//): Divides and truncates the decimal part, returning an integer (floor). 10 // 3 yields 3.
取整除(//):进行除法并截断小数部分,返回整数(向下取整)。10 // 3 得到 3。
-
Modulus (%): Returns the remainder of the division. 10 % 3 yields 1.
取模(%):返回除法运算的余数。10 % 3 得到 1。
-
Exponentiation (**): Raises the left operand to the power of the right. 2 ** 4 yields 16.
幂运算(**):将左操作数提升到右操作数的指数幂。2 ** 4 得到 16。
3. Relational (Comparison) Operators | 关系(比较)运算符
Relational operators compare two values and return a Boolean result (True or False). They are essential for decision-making structures such as if statements and while loops. Python’s comparison operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Be careful: using a single equals sign (=) is assignment, not comparison.
关系运算符比较两个值并返回布尔结果(True 或 False)。它们对 if 语句和 while 循环等决策结构至关重要。Python 的比较运算符包括等于(==)、不等于(!=)、大于(>)、小于(<)、大于或等于(>=)和小于或等于(<=)。注意:使用单个等号(=)表示赋值而非比较。
-
Equal to (==): Returns True if both operands are equal. 5 == 5 is True.
等于(==):如果两个操作数相等则返回 True。5 == 5 为 True。
-
Not equal to (!=): Returns True if operands are different. 5 != 3 is True.
不等于(!=):如果两个操作数不相等则返回 True。5 != 3 为 True。
-
Greater than (>): True if left operand is greater. 7 > 4 is True.
大于(>):如果左操作数大于右操作数则为 True。7 > 4 为 True。
-
Less than (<): True if left operand is smaller. 2 < 9 is True.
小于(<):如果左操作数小于右操作数则为 True。2 < 9 为 True。
-
Greater than or equal to (>=): True if left is greater or equal. 5 >= 5 is True.
大于或等于(>=):如果左操作数大于或等于右操作数则为 True。5 >= 5 为 True。
-
Less than or equal to (<=): True if left is smaller or equal. 3 <= 10 is True.
小于或等于(<=):如果左操作数小于或等于右操作数则为 True。3 <= 10 为 True。
4. Logical (Boolean) Operators | 逻辑(布尔)运算符
Logical operators combine multiple conditions and evaluate to a Boolean value. Python uses ‘and’, ‘or’, and ‘not’. ‘and’ returns True only if both operands are true; ‘or’ returns True if at least one operand is true; ‘not’ negates the Boolean value. Short-circuit evaluation is a key concept: for ‘and’, if the left operand is False, the right operand is not evaluated; for ‘or’, if the left operand is True, the right is not evaluated.
逻辑运算符组合多个条件并求值为布尔值。Python 使用 “and”、”or” 和 “not”。”and” 仅当两个操作数都为真时才返回 True;”or” 只要至少一个操作数为真就返回 True;”not” 对布尔值取反。短路求值是一个关键概念:对于 “and”,如果左操作数为 False,则不会计算右操作数;对于 “or”,如果左操作数为 True,则不会计算右操作数。
Truth table for logical operators:
逻辑运算符的真值表:
| 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 |
5. Assignment Operators | 赋值运算符
The basic assignment operator (=) gives a value to a variable. Python also provides compound assignment operators that combine an arithmetic or bitwise operation with assignment, making code more concise. These include +=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, and >>=. For example, x += 5 is equivalent to x = x + 5.
基本赋值运算符(=)将一个值赋给一个变量。Python 还提供了复合赋值运算符,它们将算术或位运算与赋值相结合,使代码更加简洁。这些运算符包括 +=、-=、*=、/=、//=、%=、**=、&=、|=、^=、<<= 和 >>=。例如,x += 5 等价于 x = x + 5。
-
= : Assigns the right operand to the left. x = 10
= :将右操作数赋给左操作数。 x = 10
-
+= : Add and assign. x += 3 (x becomes x + 3)
+= :加后赋值。 x += 3(x 变为 x + 3)
-
-= : Subtract and assign. x -= 2 (x becomes x – 2)
-= :减后赋值。 x -= 2(x 变为 x – 2)
-
*= : Multiply and assign. x *= 4 (x becomes x * 4)
*= :乘后赋值。 x *= 4(x 变为 x * 4)
-
/= : Divide and assign (float). x /= 2 (x becomes x / 2)
/= :除后赋值(浮点)。 x /= 2(x 变为 x / 2)
-
//= : Floor divide and assign. x //= 3 (x becomes x // 3)
//= :取整除后赋值。 x //= 3(x 变为 x // 3)
-
%= : Modulus and assign. x %= 5 (x becomes x % 5)
%= :取模后赋值。 x %= 5(x 变为 x % 5)
-
**= : Exponentiate and assign. x **= 2 (x becomes x ** 2)
**= :幂运算后赋值。 x **= 2(x 变为 x ** 2)
6. Bitwise Operators | 位运算符
Bitwise operators manipulate the binary representations of integers. They include bitwise AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). These are less common in A-Level exam questions but may appear in algorithm tracing. Understanding bitwise operations is beneficial for low-level programming tasks and optimisation.
位运算符操作整数的二进制表示。包括按位与(&)、按位或(|)、按位异或(^)、按位取反(~)、左移(<<)和右移(>>)。尽管在 A-Level 考试题目中不太常见,但可能在算法跟踪中出现。理解位运算对于底层编程任务和优化很有帮助。
-
Bitwise AND (&): Sets each bit to 1 if both bits are 1. 5 & 3 (0101 & 0011) = 1 (0001)
按位与(&):如果两个位均为 1 则结果位为 1。5 & 3 (0101 & 0011) = 1 (0001)
-
Bitwise OR (|): Sets each bit to 1 if at least one bit is 1. 5 | 3 (0101 | 0011) = 7 (0111)
按位或(|):如果至少一个位为 1 则结果位为 1。5 | 3 (0101 | 0011) = 7 (0111)
-
Bitwise XOR (^): Sets each bit to 1 if only one bit is 1. 5 ^ 3 (0101 ^ 0011) = 6 (0110)
按位异或(^):如果两个位不相同则结果位为 1。5 ^ 3 (0101 ^ 0011) = 6 (0110)
-
Bitwise NOT (~): Inverts all bits. ~5 = -6 (in two’s complement)
按位取反(~):反转所有位。~5 = -6(二进制补码形式)
-
Left shift (<<): Shifts bits left, filling with zeros. 5 << 1 = 10 (1010)
左移(<<):将位向左移动,右边补零。5 << 1 = 10 (1010)
-
Right shift (>>): Shifts bits right, preserving sign. 5 >> 1 = 2 (0010)
右移(>>):将位向右移动,保留符号位。5 >> 1 = 2 (0010)
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导