📚 8C Unit 6: Python Programming Essentials | 8C 第6单元:Python编程基础
This unit introduces the core building blocks of Python programming for Year 8 learners. You will learn how to store data in variables, make decisions with selection statements, repeat tasks with loops, and organise code using functions. The focus is on writing clear, correct programs and developing computational thinking skills.
本单元为八年级学生介绍 Python 编程的核心构件。你将学习如何在变量中存储数据、使用选择语句做出判断、使用循环重复任务,以及使用函数组织代码。重点是编写清晰、正确的程序,并培养计算思维能力。
1. Variables and Data Types | 变量与数据类型
A variable is a named space in memory that stores a value. In Python, you create a variable by assigning a value with the equals sign, for example score = 10. Variable names should be descriptive and follow the rules: they can contain letters, digits and underscores, but cannot start with a digit.
变量是内存中用于存储值的命名空间。在 Python 中,你用等号赋值来创建变量,例如 score = 10。变量名应具有描述性,并遵循规则:可以包含字母、数字和下划线,但不能以数字开头。
The main data types you will use are int for whole numbers, float for decimal numbers, str for text, and bool for True or False values. Python automatically assigns a data type when you create a variable, but you can convert types using int(), float() and str().
你将使用的主要数据类型有:用于整数的 int、用于小数的 float、用于文本的 str 以及用于真假的 bool。Python 在创建变量时会自动分配数据类型,但你也可以使用 int()、float() 和 str() 来转换类型。
2. Input and Output | 输入与输出
The print() function sends text or values to the screen. You can combine strings using a comma or the plus sign. For example, print("Hello", name) displays the word Hello followed by the value of the variable name.
print() 函数将文本或数值输出到屏幕。你可以使用逗号或加号组合字符串。例如,print("Hello", name) 显示单词 Hello,后跟变量 name 的值。
The input() function reads a line of text typed by the user. It always returns a string, so if you need a number you must convert it. A common line is age = int(input("Enter your age: ")), which stores the user’s answer as an integer.
input() 函数读取用户输入的一行文本。它始终返回字符串,因此如果需要数字,就必须进行转换。常见语句 age = int(input("Enter your age: ")) 会将用户的回答存储为整数。
3. Arithmetic Operators | 算术运算符
Python supports the standard arithmetic operators: + for addition, - for subtraction, * for multiplication, / for division, // for integer division, % for remainder and ** for powers. Integer division and remainder are especially useful when solving number problems.
Python 支持标准算术运算符:+ 加、- 减、* 乘、/ 除、// 整数除法、% 取余以及 ** 幂运算。整数除法和取余在解决数字问题时特别有用。
For example, 7 // 2 gives 3 because it discards the decimal part, while 7 % 2 gives 1 because 7 divided by 2 leaves a remainder of 1. The expression 2 ** 3 calculates 2 raised to the power 3, which equals 8.
例如,7 // 2 的结果是 3,因为它舍弃小数部分;7 % 2 的结果是 1,因为 7 除以 2 余数为 1。表达式 2 ** 3 计算 2 的 3 次方,结果为 8。
4. Comparison and Logical Operators | 比较运算符与逻辑运算符
Comparison operators compare two values and return a Boolean result. They include == equal to, != not equal to, > greater than, < less than, >= greater than or equal to, and <= less than or equal to.
比较运算符比较两个值并返回布尔结果。它们包括 == 等于、!= 不等于、> 大于、< 小于、>= 大于或等于,以及 <= 小于或等于。
Logical operators combine conditions: and requires both sides to be true, or requires at least one side to be true, and not reverses the truth value. Writing clear conditions is a key skill for control flow.
逻辑运算符用于组合条件:and 要求两边都为真,or 要求至少一边为真,not 取反。写出清晰的条件是控制流程的关键技能。
5. Selection: if, elif and else | 选择结构:if、elif 和 else
Selection allows a program to choose different paths based on conditions. The basic structure starts with if condition: followed by an indented block. The block only runs when the condition is true.
选择结构允许程序根据条件选择不同的执行路径。基本结构以 if condition: 开头,后跟缩进的代码块。仅当条件为真时,该代码块才会运行。
You can add elif to test further conditions and else to handle all remaining cases. Correct indentation is essential in Python because it defines which statements belong to each branch.
你可以添加 elif 来测试更多条件,并添加 else 来处理所有剩余情况。正确的缩进在 Python 中至关重要,因为它定义了哪些语句属于每个分支。
if score >= 90: print("A")elif score >= 75: print("B")else: print("C")
This example prints a grade based on the value of score. The first true condition wins, and only that branch runs.
该示例根据 score 的值打印等级。第一个为真的条件胜出,只有该分支会执行。
6. Iteration: while Loops | 迭代:while 循环
A while loop repeats a block as long as a condition remains true. It is used when the number of repetitions is not known in advance. The loop checks the condition before each repetition.
while 循环在条件保持为真时重复执行代码块。它用于重复次数事先未知的情况。循环在每次重复前检查条件。
You must ensure the condition eventually becomes false, otherwise the program enters an infinite loop. A counter variable or a sentinel value is often used to control the loop.
你必须确保条件最终会变为假,否则程序会进入无限循环。通常使用计数器变量或哨兵值来控制循环。
while count < 5: print(count); count = count + 1
This loop prints the numbers 0 to 4. The counter count increases each time, so the condition eventually fails.
该循环打印数字 0 到 4。计数器 count 每次增加,因此条件最终会不成立。
7. Iteration: for Loops and range | 迭代:for 循环与 range
A for loop is ideal when you know exactly how many times to repeat a task. The range() function generates a sequence of numbers. For example, range(5) produces 0, 1, 2, 3, 4.
当你知道任务需要重复的确切次数时,for 循环是理想选择。range() 函数生成一个数字序列。例如,range(5) 生成 0、1、2、3、4。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导