【Python】For Midterm Review2(week1-6)
1. 计算机/Python相关词汇翻译表
| 英文术语 | 中文翻译 | 详细解释 |
|---|---|---|
| assembly language | 汇编语言 | 一种低级编程语言,与机器指令有一一对应关系 |
| Allocating | 分配 | 在内存中为变量或数据分配空间的过程 |
| Binary digits | 二进制数字 | 由0和1组成的基本信息单位 |
| Primitive data types | 基本数据类型 | 编程语言内置的基础数据类型,如int、float、str等 |
| memory addressing | 内存寻址 | 通过唯一地址访问计算机内存中特定位置的过程 |
| syntax error | 语法错误 | 代码不符合编程语言语法规则导致的错误 |
| Programming scripts | 编程脚本 | 包含一系列指令的文本文件,通常以.py为后缀 |
| Interactive Python | 交互式Python | 逐行输入代码并立即获得结果的Python运行模式 |
| suffix | 后缀 | 文件名中点后面的部分,如.py |
| Assignment statement | 赋值语句 | 将表达式值赋给变量的语句,如 x = 5 |
| Bind the value of the expression to the name to the left of = in the current environment | 将表达式值绑定到当前环境中等号左侧的名称 | 在内存中将计算结果与变量名关联的过程 |
| Cascaded assignment | 级联赋值 | 多个变量赋相同值:x = y = z = 10 |
| Simultaneous assignment | 同时赋值 | 同时为多个变量赋不同值:x, y = 1, 2 |
| operator precedence | 运算符优先级 | 运算符的执行顺序规则 |
| Asterisk | 星号 | * 符号,用于乘法或重复操作 |
| multiplication operator | 乘法运算符 | * 符号,执行乘法运算 |
| Exponentiation | 幂运算 | ** 运算符,计算一个数的幂次方 |
| Parenthesis are always with highest priority Power Multiplication, division and remainder Addition and subtraction Left to right | 括号始终具有最高优先级 幂运算 乘法、除法和取余 加法和减法 从左到右 | 运算符优先级完整规则 |
| Floor division | 整除 | // 运算符,返回除法结果的整数部分 |
| Augmented assignment | 增强赋值 | 如 +=, -= 等组合赋值运算符 |
| parenthesis | 括号 | () 用于改变运算顺序或函数调用 |
| Break up long series of math expressions to make them easy to understand | 拆分长的数学表达式使其易于理解 | 代码可读性优化技巧 |
| Sequential flow | 顺序流程 | 代码按书写顺序逐行执行 |
| Drawing environment diagrams | 绘制环境图 | 可视化变量和函数调用关系的图表 |
| Comparison operators | 比较运算符 | ==, !=, >, <, >=, <= 等用于比较的运算符 |
| Indentation | 缩进 | Python中用于表示代码块层次的空格 |
| Increase indent: indent after an if or for statement (after 😃 Maintain indent: to indicate the scope of the block Decrease indent: to back to the level of the if statement or for Blank lines are ignored Comments on a line by themselves are ignored | 增加缩进:在if或for语句后缩进 保持缩进:表示代码块范围 减少缩进:回到if或for语句的层级 空行被忽略 单独注释行在缩进方面被忽略 | Python缩进规则详解 |
| Nested decisions | 嵌套决策 | 在条件语句内部包含另一个条件语句 |
| Two way decisions | 双向决策 | if-else 结构,提供两种执行路径 |
| Multi-way decisions | 多路决策 | if-elif-else 结构,提供多个执行路径 |
| Argument, parameter, and result | 实参、形参和结果 | 函数调用时的实际值、函数定义时的变量名、函数返回值 |
| Void functions | 无返回值函数 | 不返回任何值的函数,返回None |
| Scope of variables | 变量作用域 | 变量可访问的代码范围 |
| Default argument | 默认参数 | 函数参数在定义时指定的默认值 |
| For strings, + means “concatenate” | 对于字符串,+ 表示"连接" | 字符串拼接操作 |
| using an index specified in square brackets | 使用方括号中指定的索引 | 通过索引访问序列元素 |
| The block (body) of the loop | 循环体 | 循环语句中重复执行的代码块 |
| Slicing strings | 字符串切片 | 使用[start:end]语法获取子字符串 |
| string library | 字符串库 | Python内置的字符串处理方法集合 |
| List constants | 列表常量 | 用方括号定义的列表字面量:[1, 2, 3] |
| Concatenating lists using + | 使用 + 连接列表 | 将多个列表合并为一个新列表 |
| Dictionary literals (constants) | 字典字面量(常量) | 用花括号定义的字典:{‘key’: ‘value’} |
| Retrieving lists of keys and values | 获取键和值的列表 | 从字典中提取所有键或值 |
| Bonus: two iteration variables | 额外技巧:双迭代变量 | 在字典遍历时同时获取键和值 |
| Recursion | 递归 | 函数调用自身来解决问题的方法 |
| factorial function | 阶乘函数 | 计算n!的数学函数 |
| Binary search | 二分查找 | 在有序序列中高效查找元素的算法 |
2. Python函数/方法用法详解表
| 函数/方法 | 参数 | 返回值 | 用法说明 |
|---|---|---|---|
type() | object: 要检查的对象 | 对象的类型 | type(5) → <class 'int'>type("hello") → <class 'str'> |
int() | x: 要转换的值base: 进制数(默认10) | 整数值 | int("123") → 123int("101", 2) → 5int(3.7) → 3 |
float() | x: 要转换的值 | 浮点数值 | float("3.14") → 3.14float(5) → 5.0 |
str() | object: 要转换的对象 | 字符串表示 | str(123) → "123"str([1,2]) → "[1, 2]" |
divmod() | a: 被除数b: 除数 | 元组(商, 余数) | divmod(10, 3) → (3, 1)divmod(257, 60) → (4, 17) |
input() | prompt: 提示信息(可选) | 用户输入的字符串 | name = input("Enter name: ") |
print() | *values: 要打印的值sep: 分隔符(默认空格)end: 结束符(默认换行)file: 输出文件flush: 是否刷新 | None | print("a", "b", sep="-") → "a-b"print("hello", end=" ") |
eval() | expression: 字符串表达式 | 表达式计算结果 | eval("2 + 3 * 4") → 14eval("len('hello')") → 5 |
bool() | x: 要判断的值 | 布尔值(True/False) | bool(1) → Truebool("") → Falsebool([]) → False |
if-elif-else | 条件表达式 | 无 | python<br>if x > 10:<br> print("big")<br>elif x > 5:<br> print("medium")<br>else:<br> print("small") |
Try/except | 无参数 | 无 | python<br>try:<br> x = int("123")<br>except:<br> x = -1 |
for loop | 迭代变量和可迭代对象 | 无 | python<br>for i in range(5):<br> print(i)<br>for item in list: |
while loop | 条件表达式 | 无 | python<br>while x > 0:<br> print(x)<br> x -= 1 |
break | 无 | 无 | 立即退出当前循环 |
continue | 无 | 无 | 跳过当前循环剩余部分,进入下一次迭代 |
for-in | 迭代变量和可迭代对象 | 无 | for char in "hello":for key in dict: |
is/is not | 两个要比较的对象 | 布尔值 | x is y (身份比较)x is not None |
def | 函数名(参数) | 无 | python<br>def func(x, y=10):<br> return x + y |
return | 要返回的值(可选) | 指定的值或None | return resultreturn x, y (返回元组) |
global | 变量名 | 无 | python<br>def func():<br> global x<br> x = 10 |
len() | object: 序列或集合 | 元素个数 | len("hello") → 5len([1,2,3]) → 3len({"a":1}) → 1 |
in (条件语句) | item in sequence | 布尔值 | "a" in "apple" → True5 in [1,2,3] → False |
str.find() | sub: 要查找的子串start: 开始位置end: 结束位置 | 索引或-1 | "hello".find("l") → 2"hello".find("x") → -1 |
str.upper() | 无 | 新字符串(全大写) | "Hello".upper() → "HELLO" |
str.lower() | 无 | 新字符串(全小写) | "HELLO".lower() → "hello" |
str.replace() | old: 旧子串new: 新子串count: 替换次数 | 新字符串 | "hello".replace("l", "x") → "hexxo" |
str.lstrip() | chars: 要删除的字符 | 新字符串 | " hello ".lstrip() → "hello " |
str.rstrip() | chars: 要删除的字符 | 新字符串 | " hello ".rstrip() → " hello" |
str.strip() | chars: 要删除的字符 | 新字符串 | " hello ".strip() → "hello" |
str.startswith() | prefix: 前缀start: 开始位置end: 结束位置 | 布尔值 | "hello".startswith("he") → True |
open() | file: 文件名mode: 模式(r/w/a等)encoding: 编码 | 文件对象 | f = open("file.txt", "r")f = open("data.txt", "w", encoding="utf-8") |
"\n" | 无 | 无 | 换行符,用于字符串中表示新行 |
file.read() | size: 读取字节数(可选) | 文件内容字符串 | content = f.read()first_10 = f.read(10) |
file.write() | text: 要写入的文本 | 写入的字符数 | f.write("Hello World") → 11 |
file.close() | 无 | 无 | 关闭文件,释放资源 |
range() | start: 起始值stop: 结束值step: 步长 | range对象 | range(5) → 0,1,2,3,4range(2,10,2) → 2,4,6,8 |
list() | iterable: 可迭代对象(可选) | 新列表 | list() → []list("abc") → ['a','b','c']list(range(3)) → [0,1,2] |
list.append() | object: 要添加的对象 | None | lst = [1,2]; lst.append(3) → [1,2,3] |
list.sort() | key: 排序键函数reverse: 是否反转 | None | lst = [3,1,2]; lst.sort() → [1,2,3]lst.sort(reverse=True) → [3,2,1] |
len(),max(),min(),sum() | iterable: 可迭代对象 | 相应计算结果 | len([1,2,3]) → 3max([1,2,3]) → 3min([1,2,3]) → 1sum([1,2,3]) → 6 |
str.split() | sep: 分隔符maxsplit: 最大分割次数 | 字符串列表 | "a,b,c".split(",") → ['a','b','c']"hello world".split() → ['hello','world'] |
dict.get() | key: 键default: 默认值 | 值或默认值 | d = {"a":1}; d.get("a") → 1d.get("b", 0) → 0 |
dict.keys() | 无 | 键的视图对象 | list(d.keys()) → ['a','b'] |
dict.values() | 无 | 值的视图对象 | list(d.values()) → [1,2] |
dict.items() | 无 | (键,值)对的视图对象 | list(d.items()) → [('a',1),('b',2)] |
sorted() | iterable: 可迭代对象key: 排序键reverse: 是否反转 | 新排序列表 | sorted([3,1,2]) → [1,2,3]sorted(d.items(), key=lambda x: x[1]) |
这个表格提供了Python核心函数和方法的完整参考,包括所有参数说明和典型用法示例,非常适合期中复习使用。
