Python 编程语言
Python · Backend · API · Data

Python
编程语言

The Python Language

Python 是后端主力语言——简洁语法、强大生态。在本项目中负责 API 服务、数据逻辑、数据库交互。

如果 JavaScript 偏海报设计(视觉层),Python 偏工业设计——关注结构、功能、系统的运作。拒绝大括号和分号,用缩进表达结构。

SCOPE API · Data · DB · ML
PYTHON SYNTAX MAP
variables
type hints
f-string
strintfloatboolNonelistdicttupleset
KEYWORDdef if elif
else for while
return import
class try except
OPERATOR== != < >
and or not
in is + - * /
BUILT-INlen() print()
range() type()
int() str() float()
PY
INDENTATION OVER BRACES
1.1Variables & Types

变量与类型

# 变量不需要声明类型,Python 会自动推断
name = "凝神"          # str(字符串)
age = 28               # int(整数)
height = 1.75          # float(浮点数)
is_active = True       # bool(布尔值)
data = None            # NoneType(空值)
1.2Type Hints

类型注解

Python 3.5+ 支持类型注解(Type Hints)。类型注解不影响运行(Python 不会强制类型检查),但能让编辑器提供智能提示,也便于阅读:

不强制检查,仅辅助
编辑器智能提示
提升代码可读性
type_hints.py
name: str = "凝神"
age: int = 28
tags: list[str] = ["设计", "前端", "全栈"]
scores: dict[str, int] = {"math": 95, "english": 88}

def greet(name: str) -> str:
    return f"Hello, {name}!"
1.3Strings

字符串

F-STRING & MULTI-LINE
# f-string(格式化字符串,最常用)
name = "凝神"
greeting = f"你好, {name}!"         # "你好, 凝神!"
info = f"年龄: {age}, 身高: {height:.1f}m"

# 多行字符串
description = """
这是一段
多行文本。
"""
METHODS
# 常用方法
"hello world".upper()           # "HELLO WORLD"
"Hello World".lower()           # "hello world"
"  hello  ".strip()             # "hello"(去除首尾空白)
"hello world".split(" ")        # ["hello", "world"]
"-".join(["a", "b", "c"])       # "a-b-c"
"hello world".replace("world", "python")  # "hello python"
"hello".startswith("he")        # True
"hello".endswith("lo")          # True