Skip to content

Commit 567b71e

Browse files
committed
add pep515: make numbers readable.
1 parent 1c46153 commit 567b71e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

zh_CN/3-tips-on-numbers-and-strings.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ def main():
275275
- Saw (2004)""")
276276
```
277277

278+
#### 大数字也可以变得更加可读
279+
280+
Python 中的数字,可以通过在中间添加下划线来提高可读性
281+
([PEP515](https://www.python.org/dev/peps/pep-0515/),需要 Python3.6+)。
282+
283+
比如:
284+
285+
```
286+
>>> 10_000_000.0 # 以“千”为单位划分数字
287+
10000000.0
288+
>>> 0xCAFE_F00D # 16进制数字同样有效,4个一组更易读
289+
3405705229
290+
>>> 0b_0011_1111_0100_1110 # 二进制也有效
291+
16206
292+
>>> int('0b_1111_0000', 2) # 处理字符串的时候也会正确处理下划线
293+
240
294+
```
295+
278296
### 3. 别忘了那些 “r” 开头的内建字符串函数
279297

280298
Python 的字符串有着非常多实用的内建方法,最常用的有 `.strip()``.split()` 等。这些内建方法里的大多数,处理起来的顺序都是从左往右。但是其中也包含了部分以 `r` 打头的**从右至左处理**的镜像方法。在处理特定逻辑时,使用它们可以让你事半功倍。

0 commit comments

Comments
 (0)