← 返回资讯屏轩教育 · 行业资讯

Core Programming Constructs and Algorithms for Edexcel A-Level | Edexcel A-Level 核心编程构造与算法

2026-08-29 14:43 · 本文部分内容由AI辅助创作

📚 Core Programming Constructs and Algorithms for Edexcel A-Level | Edexcel A-Level 核心编程构造与算法

This article covers the essential programming constructs, data structures and algorithms required for the Edexcel A-Level Computer Science specification. You will learn how to trace code, apply pseudocode ideas and translate them into Python, with clear examples and exam-focused explanations. Every section links the abstract idea to the concrete question style you will meet in Paper 1 and Paper 2, so that you can move fluently between a written algorithm and a running program.

本文介绍 Edexcel A-Level 计算机科学考试中必备的核心编程构造、数据结构和算法。你将学会如何跟踪代码、运用伪代码思想并将其转换为 Python,配合清晰的示例和紧扣考点的解释。每一节都把抽象概念与 Paper 1、Paper 2 中真实的题型联系起来,帮助你自如地在书面算法与可运行程序之间切换。


1. Data Types and Variables | 数据类型与变量

In Edexcel A-Level programming questions, you must distinguish between primitive data types and compound data types. The common primitive types are integer, real/float, Boolean and character. A variable is a named memory location whose value can change during execution; a constant is fixed at compile time. Strong typing requires every variable to be declared with a type, while Python uses dynamic typing but you still need to reason about types when tracing code.

在 Edexcel A-Level 编程题中,你必须区分基本数据类型和复合数据类型。常见的基本类型有整型、实型/浮点型、布尔型和字符型。变量是命名的内存位置,其值在执行期间可以改变;常量在编译时固定。强类型要求声明变量的类型,而 Python 使用动态类型,但你在跟踪代码时仍需推断类型。

When tracing code, watch for implicit type conversion: in Python, 3/2 gives 1.5, while 3//2 gives 1. Integer division in pseudocode DIV also gives the whole-number quotient. Variables should have meaningful names and follow the language's naming rules, such as no spaces and not starting with a digit.

跟踪代码时,注意隐式类型转换:在 Python 中 3/2 得到 1.5,而 3//2 得到 1。伪代码中的整数除法 DIV 也给出整数商。变量应使用有意义的名称,并遵循语言命名规则,例如不能包含空格,不能以数字开头。

A compound data type groups several values together under one identifier. The table below summarises the types you are expected to recognise, together with a typical Python equivalent.

复合数据类型把多个值组合在同一个标识符之下。下表总结了考纲要求你识别的类型,以及常见的 Python 对应写法。

Type | 类型Meaning | 含义Python example | Python 示例
Integer | 整型Whole numbers | 整数count = 7
Real / Float | 实型Numbers with a fractional part | 含小数部分price = 3.5
Boolean | 布尔型True or False only | 只有真或假found = True
Character | 字符型A single symbol | 单个符号grade = 'A'
String | 字符串A sequence of characters | 字符序列name = 'Ada'

Remember that a character and a one-character string are not the same thing in a strongly typed language, and that converting between types is done with casting functions such as int(), float() and str().

记住,在强类型语言中字符与长度为 1 的字符串并不相同,类型之间的转换通过 int()、float()、str() 这类强制转换函数完成。


2. Constants, Naming and Scope | 常量、命名与作用域

ility because a single edit updates every place the value is used, and it makes the code self-documenting. Identifiers should be descriptive, use camelCase or snake_case consistently, and avoid reserved words.

常量保存程序运行期间不得改变的值,例如 PI = 3.14159 或 MAX_SIZE = 100。使用常量能提升可维护性,因为只需修改一处即可更新所有使用该值的位置,同时也让代码具备自解释性。标识符应具有描述性,统一使用 camelCase 或 snake_case,并避开保留字。

Scope describes where a variable can be seen. A local variable is declared inside a subprogram and exists only during that call; a global variable is declared outside and is visible throughout. Overusing global variables makes programs hard to debug, because any subprogram may change them.

作用域描述变量的可见范围。局部变量在子程序内部声明,仅在该次调用期间存在;全局变量在外部声明,在整个程序中可见。过度使用全局变量会让程序难以调试,因为任何子程序都可能修改它们。

Local variable lifetime: created on call → destroyed on return | 局部变量生命周期:调用时创建 → 返回时销毁


3. Operators and Expressions | 运算符与表达式

written as ** in Python. Relational operators compare values and produce a Boolean: =, ≠, <, >, ≤ and ≥. Boolean operators AND, OR and NOT combine conditions.

运算符用于构造表达式。算术运算符包括 +、-、*、/、DIV(整数除法)、MOD(取余)以及表示幂运算的 ^(在 Python 中写作 **)。关系运算符比较值并产生布尔结果:=、≠、<、>、≤ 和 ≥。布尔运算符 AND、OR 和 NOT 用于组合条件。

Operator precedence decides evaluation order. Brackets are evaluated first, then exponentiation, then multiplication and division (including DIV and MOD), then addition and subtraction, and finally the relational and Boolean operators. When in doubt, add brackets in your exam answer.

运算符优先级决定求值顺序。先算括号,再算幂运算,然后算乘除(含 DIV 和 MOD),接着算加减,最后才计算关系运算符和布尔运算符。考试作答时若不确定,就加上括号。

Precedence | 优先级: ( ) → ^ → * / DIV MOD → + − → = ≠ < > ≤ ≥ → NOT → AND → OR

String operations also appear in questions: concatenation joins two strings, and a substring or length function extracts information. Note that adding a number to a string raises a type error unless you cast it first, for example 'Score: ' + str(score).

题目中也会出现字符串操作:连接运算把两个字符串拼接,取子串或求长度函数用于提取信息。注意字符串与数字相加会引发类型错误,除非先进行转换,例如 'Score: ' + str(score)。


4. Sequence, Selection and Iteration | 顺序、选择与迭代

All programs are built from three constructs: sequence, selection and iteration. Sequence means statements execute one after another in order. Selection chooses between alternative paths using IF...THEN...ELSE...ENDIF, a nested IF, or a CASE/SWITCH statement when several fixed options exist.

所有程序都由三种构造组成:顺序、选择和迭代。顺序指语句按次序依次执行。选择使用 IF...THEN...ELSE...ENDIF、嵌套 IF,或在存在多个固定选项时使用 CASE/SWITCH 语句,在不同分支间做出选择。

Iteration repeats statements. A FOR loop is count-controlled and runs a known number of times; a WHILE loop is condition-controlled and checks the condition before each pass; a REPEAT...UNTIL loop checks after each pass, so the body always executes at least once. Choosing the wrong loop type is a common source of lost marks.

迭代用于重复执行语句。FOR 循环是计数控制型,执行已知的次数;WHILE 循环是条件控制型,在每轮开始前检查条件;REPEAT...UNTIL 循环在每轮结束后检查条件,因此循环体至少执行一次。选错循环类型是常见的失分点。

A trace table is the standard tool for showing how a loop behaves. Write one column per variable, add a column for any output, and record a new row each time a value changes. Nested loops require careful indentation in your working, because the inner loop completes all of its passes for each single pass of the outer loop.

跟踪表是展示循环运行过程的标准工具。每个变量占一列,输出占一列,每当某个值发生变化就新增一行。嵌套循环在你的草稿中需要仔细缩进,因为外层循环每执行一轮,内层循环都要完整跑完它的全部轮次。


5. Subprograms, Parameters and Return Values | 子程序、参数与返回值

A subprogram is a named block of code that performs one task and can be called repeatedly. A function returns a value; a procedure performs an action and returns nothing. Decomposing a large problem into subprograms is called modularity or stepwise refinement, and it makes code easier to test, reuse and maintain.

子程序是完成单一任务、可被反复调用的命名代码块。函数返回一个值;过程执行一个动作但不返回值。把大问题分解为子程序称为模块化或逐步求精,它让代码更易测试、复用和维护。

Parameters pass data into a subprogram. Passing by value copies the data, so changes inside the subprogram do not affect the original variable. Passing by reference passes the address, so changes do affect the caller's variable. In Python, immutable objects such as integers behave like pass by value, while mutable objects such as lists behave like pass by reference.

参数把数据传入子程序。按值传递复制数据,因此子程序内部的修改不会影响原变量;按引用传递传入地址,因此修改会影响调用方的变量。在 Python 中,整数等不可变对象表现得像按值传递,而列表等可变对象表现得像按引用传递。

A good exam answer states the subprogram's purpose, its parameters with types, its return type and any preconditions. Just as important is avoiding duplication: if the same block appears twice, it should probably become a subprogram.

优秀的答卷会说明子程序的目的、带类型的参数、返回类型以及任何前置条件。同样重要的是避免重复:如果同一段代码出现两次,它大概就应该被改写为子程序。


6. Data Structures | 数据结构

Data structures organise related data so that it can be accessed and modified efficiently. You must know how each structure stores data, how items are added and removed, and the cost of those operations. Arrays are fixed-length and indexed, lists are dynamic, and records group fields of different types under one name.

广告 / Sponsored

数据结构用于组织相关数据,以便高效访问和修改。你必须了解每种结构如何存储数据、如何添加和删除元素,以及这些操作的代价。数组长度固定且有下标,列表是动态的,记录则把不同类型的字段组合在同一个名称下。

Stacks and queues are abstract data types defined by their access rules. A stack is LIFO (last in, first out) with push and pop operations, while a queue is FIFO (first in, first out) with enqueue and dequeue. Linked lists store nodes containing data and a pointer to the next node, which makes insertion cheap but random access expensive.

栈和队列是由访问规则定义的抽象数据类型。栈是 LIFO(后进先出),操作包括 push 和 pop;队列是 FIFO(先进先出),操作包括 enqueue 和 dequeue。链表存储包含数据与指向下一节点指针的结点,因此插入代价低,但随机访问代价高。

Structure | 结构Access rule | 访问规则Typical use | 典型用途
Array / List | 数组/列表Direct by index | 按下标直接访问Storing a fixed set of marks | 存放一组固定成绩
Stack | 栈LIFO | 后进先出Undo history, expression evaluation | 撤销历史、表达式求值
Queue | 队列FIFO | 先进先出Print jobs, buffer of keystrokes | 打印队列、按键缓冲
Linked list | 链表Sequential via pointers | 通过指针顺序访问Frequently changing collections | 频繁增删的集合
Binary tree | 二叉树Left subtree < node < right subtree | 左子树小于结点、右子树大于结点Fast ordered searching | 快速有序查找
Hash table | 哈希表Key to index by hash function | 通过哈希函数由键得下标Constant-time lookup | 常数时间查找

7. Searching Algorithms | 查找算法

A linear search checks each item in turn until the target is found or the list ends. It works on any list, sorted or not, and its worst case is n comparisons, giving a time complexity of O(n). It is the correct choice for small or unsorted data.

线性查找依次检查每一项,直到找到目标或列表结束。它对任何列表都适用,无论是否有序,最坏情况下需要比较 n 次,时间复杂度为 O(n)。对于小型或无序数据,它是最合适的选择。

A binary search is far more efficient but requires a sorted list. It examines the middle item, then discards the half that cannot contain the target and repeats. Each comparison halves the search space, so the time complexity is O(log n).

二分查找效率高得多,但要求列表已排序。它先检查中间元素,然后舍弃不可能包含目标的一半并重复该过程。每次比较都会把搜索范围减半,因此时间复杂度为 O(log n)。

mid ← (low + high) DIV 2 | while low ≤ high and not found

You should be able to state the maximum number of comparisons for a binary search on n items, which is ⌈log₂ n⌉ rounded up, and to explain why the algorithm fails on unsorted data. A common exam task is to complete a trace table showing low, high, mid and the value tested at each stage.

你应当能够说出对 n 个元素做二分查找的最大比较次数,即向上取整的 ⌈log₂ n⌉,并解释为什么该算法在无序数据上会失效。常见考题是补全跟踪表,记录每一步的 low、high、mid 以及被比较的值。


8. Sorting Algorithms | 排序算法

Bubble sort repeatedly compares adjacent pairs and swaps them if they are in the wrong order, repeating passes until no swaps occur. It is easy to implement but inefficient, with an average and worst-case complexity of O(n²), although a flag that detects a swap-free pass gives a best case of O(n) on already sorted data.

冒泡排序反复比较相邻的两个元素,若顺序错误就交换,持续多轮直到某一轮没有发生交换。它实现简单但效率低,平均和最坏时间复杂度为 O(n²);不过若用标志位检测到某轮无交换,则对已排好序的数据可达 O(n) 的最佳情况。

Insertion sort takes each item in turn and inserts it into its correct position within the already sorted part of the list. It performs well on small or nearly sorted data, with a best case of O(n) and a worst case of O(n²).

插入排序依次取出每个元素,将其插入到列表已排序部分的正确位置。它在小型或近乎有序的数据上表现良好,最佳情况为 O(n),最坏情况为 O(n²)。

Merge sort uses divide and conquer: it splits the list in half repeatedly until each part holds one item, then merges the parts back together in order. Its time complexity is O(n log n) in all cases, which is faster than the other two for large datasets, but it needs additional memory for the merging, giving a space complexity of O(n).

归并排序采用分治思想:不断把列表对半拆分,直到每部分只剩一个元素,再按顺序把各部分合并回去。它在所有情况下的时间复杂度都是 O(n log n),对大数据集比前两种更快,但合并时需要额外内存,空间复杂度为 O(n)。

Algorithm | 算法Best | 最佳Average | 平均Worst | 最坏
Bubble sort | 冒泡排序O(n)O(n²)O(n²)
Insertion sort | 插入排序O(n)O(n²)O(n²)
Merge sort | 归并排序O(n log n)O(n log n)O(n log n)

9. Recursion | 递归

Recursion is a technique where a subprogram calls itself. Every recursive solution needs a base case that stops the recursion and a general case that reduces the problem towards the base case. Forgetting the base case causes infinite recursion and eventually a stack overflow.

递归是子程序调用自身的技术。每个递归解法都需要一个终止递归的基本情形,以及一个把问题逐步缩小并趋近基本情形的一般情形。忘记基本情形会导致无限递归,最终造成栈溢出。

factorial(n) = 1 if n = 0, otherwise n × factorial(n − 1)

Each call is placed on the call stack with its own copy of the parameters and local variables, which is why recursion uses more memory than iteration. Comparing the two is a frequent exam question: recursion gives shorter, more elegant code for naturally recursive problems such as tree traversal, while iteration is usually faster and uses less memory.

每次调用都会连同自己那份参数和局部变量被压入调用栈,这正是递归比迭代占用更多内存的原因。两者的比较是高频考题:对于树遍历这类天然递归的问题,递归代码更短更优雅;而迭代通常更快、占用内存更少。

Classic examples to learn include factorial, Fibonacci numbers, and recursive binary search. Be prepared to rewrite any of these as an iterative solution.

需要掌握的经典例子包括阶乘、斐波那契数列以及递归版的二分查找。你还要准备好把其中任何一个改写成迭代解法。


10. Pseudocode and Tracing Code | 伪代码与代码跟踪

Edexcel pseudocode is a language-independent way of expressing an algorithm. Assignment uses the arrow ←, blocks are closed with ENDIF, ENDWHILE, ENDFOR or ENDSUB, and indentation shows structure. Arrays are written with square brackets and are usually 1-indexed in pseudocode, which differs from Python's 0-indexed lists.

Edexcel 伪代码是一种与具体语言无关的算法表达方式。赋值使用箭头 ←,代码块用 ENDIF、ENDWHILE、ENDFOR 或 ENDSUB 结束,缩进用于体现结构。数组用方括号表示,在伪代码中通常从下标 1 开始,这与 Python 列表从下标 0 开始不同。

ries, the difference between < and ≤, integer division and the order in which nested conditions are evaluated.

跟踪代码时,逐行推进,只在值真正发生变化时才更新跟踪表。要特别留意循环边界、< 与 ≤ 的区别、整数除法,以及嵌套条件的求值顺序。

ge, a missing initial value, using = instead of = in a comparison, and swapping without a temporary variable.

典型的调试题会给出有缺陷的算法,要求你指出错误并改正。经典错误包括循环范围差一、缺少初始值、在比较中误用赋值符号,以及交换变量时没有使用临时变量。


11. Translating Pseudocode into Python | 伪代码转换为 Python

Translating between pseudocode and Python is a core skill. Most structures map directly, but you must remember Python-specific details: colons after conditions and loops, indentation instead of END statements, range() for count-controlled loops, and 0-based indexing for lists and strings.

在伪代码与 Python 之间转换是一项核心技能。大多数结构可以直接对应,但你必须记住 Python 的细节:条件和循环后要加冒号,用缩进代替 END 语句,计数循环使用 range(),列表和字符串从下标 0 开始。

Pseudocode | 伪代码Python
x ← 5x = 5
IF x > 3 THEN ... ENDIFif x > 3: ...
FOR i ← 1 TO 10 ... ENDFORfor i in range(1, 11): ...
WHILE x < 10 ... ENDWHILEwhile x < 10: ...
FUNCTION f(a) ... RETURN adef f(a): ... return a
array[1]array[0]

A useful habit is to keep your Python variable names identical to those in the pseudocode, so that a marker can follow your logic and so that you reduce the chance of introducing a mistake during translation.

一个有用的习惯是让 Python 中的变量名与伪代码中的完全一致,这样阅卷人能够跟上你的逻辑,你也能减少转换过程中引入错误的可能。

When you write a Python solution, add short comments describing the purpose of each block, validate user input where the question requires it, and test with at least one normal, one boundary and one erroneous value.

编写 Python 解法时,为每个代码块添加简短注释说明用途;若题目要求,则对用户输入进行验证;并至少用一个正常值、一个边界值和一个错误值进行测试。


12. Complexity, Testing and Exam Technique |
广告 / Sponsored
官方2027年初级会计职称师教材书奇兵制胜章节练习题册马勇网课必刷题库实务和经济法基础辅导料资格证26历中真题考试卷知…
经济、会计 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao
广告 / Sponsored
新版南艺乐理考级南京艺术学院音乐考级类基本乐理与听音训练教材乐理考级知识练习教程社会艺术水平考级教材
艺术设计、音乐 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao

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

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao