知识点回顾
- 规范的文件命名
- 规范的文件夹管理
- 机器学习项目的拆分
- 编码格式和类型注解
作业:尝试针对之前的心脏病项目,准备拆分的项目文件,思考下哪些部分可以未来复用。
# 变量类型注解
age: int = 25
name: str = "Alice"
is_student: bool = True
height: float = 1.75
# 函数参数和返回值类型注解
def calculate_bmi(weight: float, height: float) -> float:
return weight / (height ** 2)
# 可选类型
from typing import Optional
def get_user_email(user_id: int) -> Optional[str]:
# 如果用户没有邮箱,返回None
if user_id in database:
return database[user_id].email
return None
# 复杂类型 - 列表
from typing import List
def process_names(names: List[str]) -> None:
for name in names:
print(f"Hello, {name}")
# 复杂类型 - 字典
from typing import Dict
def get_user_info(user_id: int) -> Dict[str, str]:
return {
"name": "Alice",
"email": "alice@example.com",
"role": "admin"
}
1087

被折叠的 条评论
为什么被折叠?



