Skip to content

第 2 课:Python 基础——测试人够用就行

不教面向对象,够用就行

一、核心语法

变量: name = "张三" age = 25 is_pass = True

字符串:

python
f"我叫{name},今年{age}岁"  # f-string 最方便
text.strip()    # 去空格
"hello" in text # 判断包含

列表:

python
fruits = ["苹果", "香蕉"]
fruits.append("橘子")
fruits[0]       # 取第一个
len(fruits)     # 长度

字典:

python
user = {"name": "张三", "age": 25}
user["name"]           # "张三"
user.get("name")       # 推荐,取不到返回 None

二、条件判断和循环

python
if score >= 90:
    print("优秀")
elif score >= 60:
    print("及格")

for fruit in fruits:
    print(fruit)

三、函数和文件

python
def add(a, b):
    return a + b

with open("data.txt", "r") as f:
    content = f.read()

四、异常处理

python
try:
    result = 10 / 0
except Exception as e:
    print(f"出错了:{e}")

不需要学面向对象、继承多态。 上面的足够写自动化了。

💬 给清秀留言