Skip to content

Commit 8c8fdbe

Browse files
walter201230claude
andcommitted
fix: 同步 learn-py.org 内容审查发现的 4 处教学错误(unichr / int 转换笔误 / f-string 基础缺失 / pathlib 错误输出)
- Type_conversion.md:删 Python 3 不存在的 unichr 表格行(实跑 NameError);修笔误「小数形式的字符串不能强转为字符串」→「不能用 int() 强转为整数」 - Type_of_data.md:f-string 节开头补基础语法讲解——原文「前面咱们已经在代码里看到过」不成立(本文前文与 python0/python1 均无 f-string,基础语法从未正式教过);节标题相应改为「f-string:把变量嵌进字符串」 - python18/1.md:p3 = 'foo' / Path('/tmp') 声称输出 /tmp/foo,实测输出 /tmp(右侧绝对路径会重置拼接前缀)——示例改用 Path('tmp'),输出 foo/tmp 与文本一致 同批修复已先行应用于 python-lab(learn-py.org)并上线。验证:example_runner 跑 python2+python18 两章 89 块,3 个失败经干净树对照确认为预先存在、与本次无关 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VvNQURTu9tDQzmHvE9rGGZ
1 parent 9177bb9 commit 8c8fdbe

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

Article/PythonBasis/python18/1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ from pathlib import Path
127127

128128
p1 = Path('/tmp') / 'foo'
129129
p2 = Path('/tmp') / Path('foo')
130-
p3 = 'foo' / Path('/tmp')
130+
p3 = 'foo' / Path('tmp')
131131

132132
print(p1)
133133
print(p2)
@@ -139,7 +139,7 @@ print(p3)
139139
```
140140
/tmp/foo
141141
/tmp/foo
142-
/tmp/foo
142+
foo/tmp
143143
```
144144

145145
不管 `/` 左边还是右边是字符串,只要有一边是 `Path`,结果就还是 `Path`

Article/PythonBasis/python2/Type_conversion.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Python 中基本数据类型转换的方法有下面几个。
1313
|tuple(s ) | 将序列 s 转换为一个元组 |
1414
|list(s ) | 将序列 s 转换为一个列表 |
1515
|chr(x ) | 将一个整数转换为一个字符 |
16-
|unichr(x ) | 将一个整数转换为 Unicode 字符 |
1716
|ord(x ) | 将一个字符转换为它的整数值 |
1817
|hex(x ) | 将一个整数转换为一个十六进制字符串 |
1918
|oct(x ) | 将一个整数转换为一个八进制字符串 |
@@ -56,7 +55,7 @@ Traceback (most recent call last):
5655
ValueError: invalid literal for int() with base 10: '88.88'
5756
```
5857

59-
但这并不是意味着浮点数不能转化为整数,而是小数形式的字符串不能强转为字符串
58+
但这并不是意味着浮点数不能转化为整数,而是小数形式的字符串不能用 `int()` 强转为整数
6059

6160
浮点数还是可以通过 `int()` 函数转换的。
6261

Article/PythonBasis/python2/Type_of_data.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,26 @@ print(0.55+0.411)
294294

295295

296296

297-
## 6、 Python 3.12+ 的 f-string 进阶 ##
297+
## 6、 f-string:把变量嵌进字符串 ##
298298

299-
各位童鞋,前面咱们已经在代码里看到过 `f'昵称:{name}'` 这种写法,叫做 f-string(Python 3.6+ 引入),是目前最推荐的字符串拼接方式。这里再补几个不少人没注意到的进阶用法。
299+
各位童鞋,想把变量的值嵌进一段文字里,该怎么写呢?
300+
301+
Python 最推荐的写法叫 **f-string**(Python 3.6+ 引入):在字符串的引号前面加一个 `f`,字符串里用 `{}` 包住变量名,Python 就会把 `{}` 换成变量的值。
302+
303+
```python
304+
name = '两点水'
305+
print(f'昵称:{name}')
306+
```
307+
308+
输出的结果:
309+
310+
```
311+
昵称:两点水
312+
```
313+
314+
就这么简单:`f` 前缀 + `{变量名}`,单引号双引号都行,一个字符串里可以放多个 `{}`。这是目前最推荐的字符串拼接方式。
315+
316+
基础会了,这里再补几个不少人没注意到的进阶用法。
300317

301318
### (1) self-documenting:调试的神器 `f"{x=}"` ###
302319

0 commit comments

Comments
 (0)