docs: 添加 Java 开发者 Python 速查表
This commit is contained in:
parent
bc1a20caa5
commit
18a77736c0
120
langgraph-tutorial/Python速查表.md
Normal file
120
langgraph-tutorial/Python速查表.md
Normal file
@ -0,0 +1,120 @@
|
||||
# Java 开发者 Python 速查表
|
||||
|
||||
## 基础语法对比
|
||||
|
||||
| 概念 | Java | Python |
|
||||
|------|------|--------|
|
||||
| 打印 | `System.out.println("hi");` | `print("hi")` |
|
||||
| 条件 | `if (x > 0) { ... }` | `if x > 0: ...` |
|
||||
| 循环 | `for (String s : list) { ... }` | `for s in list: ...` |
|
||||
| 方法 | `public String hello() { return "hi"; }` | `def hello() -> str: return "hi"` |
|
||||
| 类 | `public class Foo { String bar; }` | `class Foo: bar: str` |
|
||||
| 导入 | `import java.util.List;` | `from typing import List` |
|
||||
|
||||
## 数据类型对比
|
||||
|
||||
| Java | Python | 说明 |
|
||||
|------|--------|------|
|
||||
| `String` | `str` | 字符串 |
|
||||
| `int` | `int` | 整数 |
|
||||
| `double` | `float` | 浮点数 |
|
||||
| `boolean` | `bool` | 布尔值 |
|
||||
| `List<T>` | `list` | 列表 |
|
||||
| `Map<K,V>` | `dict` | 字典 |
|
||||
| `null` | `None` | 空值 |
|
||||
| `true/false` | `True/False` | 布尔值 |
|
||||
|
||||
## 常用操作对比
|
||||
|
||||
### 字符串
|
||||
```java
|
||||
// Java
|
||||
String s = "hello";
|
||||
s.length(); // 长度
|
||||
s.toUpperCase(); // 大写
|
||||
s.contains("ll"); // 包含
|
||||
s.replace("l", "r"); // 替换
|
||||
String.format("%s %d", s, 42); // 格式化
|
||||
```
|
||||
|
||||
```python
|
||||
# Python
|
||||
s = "hello"
|
||||
len(s) # 长度
|
||||
s.upper() # 大写
|
||||
"ll" in s # 包含
|
||||
s.replace("l", "r") # 替换
|
||||
f"{s} {42}" # 格式化(f-string)
|
||||
```
|
||||
|
||||
### 集合
|
||||
```java
|
||||
// Java
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("a");
|
||||
list.get(0);
|
||||
list.size();
|
||||
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
map.put("key", 1);
|
||||
map.get("key");
|
||||
map.containsKey("key");
|
||||
```
|
||||
|
||||
```python
|
||||
# Python
|
||||
list = []
|
||||
list.append("a")
|
||||
list[0]
|
||||
len(list)
|
||||
|
||||
map = {}
|
||||
map["key"] = 1
|
||||
map.get("key")
|
||||
"key" in map
|
||||
```
|
||||
|
||||
## Python 特有概念
|
||||
|
||||
### 缩进代替花括号
|
||||
```python
|
||||
# Python 用缩进表示代码块
|
||||
if x > 0:
|
||||
print("正数") # 必须缩进
|
||||
else:
|
||||
print("非正数")
|
||||
```
|
||||
|
||||
### 类型提示(可选)
|
||||
```python
|
||||
# Python 类型提示只是给编辑器看的,运行时不检查
|
||||
def hello(name: str) -> str:
|
||||
return f"Hello, {name}"
|
||||
|
||||
# 这样写也可以运行
|
||||
def hello(name):
|
||||
return "Hello, " + name
|
||||
```
|
||||
|
||||
### 字典推导
|
||||
```python
|
||||
# 创建字典
|
||||
d = {"key": "value"}
|
||||
|
||||
# 遍历
|
||||
for k, v in d.items():
|
||||
print(k, v)
|
||||
|
||||
# 安全访问
|
||||
d.get("key") # 不存在返回 None
|
||||
d.get("key", "默认") # 不存在返回默认值
|
||||
```
|
||||
|
||||
## LangGraph 核心概念
|
||||
|
||||
| 概念 | 说明 | Java 类比 |
|
||||
|------|------|----------|
|
||||
| State | 节点间传递的数据 | DTO/Entity 类 |
|
||||
| Node | 处理函数 | Service 方法 |
|
||||
| Edge | 节点连接 | 流程控制 |
|
||||
| Graph | 有向图 | 工作流引擎 |
|
||||
Loading…
x
Reference in New Issue
Block a user