Skip to content

Commit 3197fbb

Browse files
📝 更新Python文档,优化内容与结构
- 在多个文档中调整了章节标题,提升了内容的逻辑性与可读性,例如将“数据类型和值”改为“基础语法”。 - 新增了对变量、文件操作和装饰器的详细说明,增强了对Python核心概念的理解。 - 删除了冗余的示例,简化了内容,确保文档更加清晰易懂。 - 更新了相关库推荐,增加了对`hashlib`的介绍,提升了文档的实用性。
1 parent 1d9d1a4 commit 3197fbb

File tree

7 files changed

+129
-141
lines changed

7 files changed

+129
-141
lines changed

docs/docs/选择编程语言/Python/0基础语法.mdx

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ print(keyword.kwlist)
147147
148148
## 关键字
149149
150-
### 数据类型和值
150+
### 基础语法
151151
- False, None, True: 基本的常量值
152-
153-
### 逻辑运算
154152
- and, or, not: 逻辑运算符
155153
- is, in: 身份和成员运算符
156154
155+
### 变量
156+
- del: 删除对象
157+
157158
### 控制流
158159
- if, elif, else: 条件判断
159160
- match, case, _: 模式匹配(软关键字)
@@ -169,25 +170,24 @@ print(keyword.kwlist)
169170
- return: 函数返回
170171
- yield: 生成器
171172
- lambda: 匿名函数
172-
- global: 全局变量声明
173-
- nonlocal: 非局部变量声明
174173
175-
###
174+
### 类与对象
176175
- class: 定义类
177-
178-
### 泛型
179176
- type: 泛型声明(软关键字)
180177
181-
### 模块和导入
182-
- import, from: 模块导入
183-
- as: 别名
178+
### 装饰器和作用域
179+
- global: 全局变量声明
180+
- nonlocal: 非局部变量声明
181+
182+
### 文件操作
183+
- with: 上下文管理器
184184
185185
### 异步编程
186186
- async, await: 异步编程关键字
187187
188-
### 其他
189-
- del: 删除对象
190-
- with: 上下文管理器
188+
### 模块
189+
- import, from: 模块导入
190+
- as: 别名
191191
192192
`} />
193193

@@ -437,7 +437,7 @@ print(keyword.kwlist)
437437
- ascii(): ASCII表示
438438
- repr(): 对象表示
439439
440-
### 序列操作与控制流
440+
### 序列操作
441441
- list(): 列表
442442
- dict(): 字典
443443
- tuple(): 元组
@@ -448,16 +448,18 @@ print(keyword.kwlist)
448448
- sorted(): 排序
449449
- reversed(): 反向生成迭代器
450450
- filter(): 过滤序列返回迭代器
451+
452+
### 控制流
451453
- all(): 所有元素为真
452454
- any(): 任一元素为真
453455
- range(): 数字序列
454-
- zip(): 并行迭代
455456
- enumerate(): 枚举索引和值
457+
- zip(): 并行迭代
456458
457459
### 函数
458460
- map(): 映射
459461
460-
### 面向对象编程
462+
### 类与对象
461463
- object(): 基础对象
462464
- super(): 父类引用
463465
- delattr(): 删除属性
@@ -469,15 +471,15 @@ print(keyword.kwlist)
469471
- type(): 对象类型
470472
- isinstance(): 类型检查
471473
- issubclass(): 子类检查
474+
- property(): 属性装饰器
475+
- classmethod(): 类方法装饰器
476+
- staticmethod(): 静态方法装饰器
472477
473-
### 生成器与迭代器
478+
### 迭代器
474479
- iter(): 创建迭代器
475480
- next(): 获取下一个元素
476481
477482
### 装饰器与作用域
478-
- property(): 属性装饰器
479-
- classmethod(): 类方法装饰器
480-
- staticmethod(): 静态方法装饰器
481483
- callable(): 是否可调用
482484
- globals(): 全局变量字典
483485
- locals(): 局部变量字典
@@ -490,14 +492,17 @@ print(keyword.kwlist)
490492
- aiter(): 异步迭代器
491493
- anext(): 异步next
492494
493-
### 模块操作
494-
- __ import __(): 动态导入模块
495-
496-
### 其他
495+
### 代码执行与调试
497496
- breakpoint(): 调试断点
498497
- compile(): 编译代码
499498
- eval(): 执行表达式
500499
- exec(): 执行代码
500+
501+
502+
### 模块操作
503+
- __ import __(): 动态导入模块
504+
505+
### 其他
501506
- help(): 显示帮助信息
502507
503508
`} />

docs/docs/选择编程语言/Python/10函数.mdx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -353,31 +353,6 @@ b = [10, 11, 15]
353353
list(map(add, a, b)) # [12, 14, 19]
354354
```
355355

356-
### reduce 函数
357-
358-
reduce() 对序列元素进行累积运算。
359-
360-
reduce函数签名:`reduce(function, iterable[, initializer]) -> value`
361-
362-
参数说明:
363-
- `function`:累积函数,接受两个参数
364-
- `iterable`:要累积的可迭代对象
365-
- `initializer`:可选的初始值
366-
367-
返回值:
368-
- 返回累积的结果
369-
370-
```python showLineNumbers
371-
from functools import reduce
372-
373-
def add(x, y) : # 两数相加
374-
return x + y
375-
sum1 = reduce(add, [1,2,3,4,5]) # 计算列表和:1+2+3+4+5
376-
sum2 = reduce(lambda x, y: x+y, [1,2,3,4,5]) # 使用 lambda 匿名函数
377-
print(sum1)
378-
print(sum2)
379-
```
380-
381356
## 推荐库
382357

383358
- functools: 提供高阶函数和装饰器

docs/docs/选择编程语言/Python/1变量.mdx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,22 +357,35 @@ for i in range(10):
357357

358358
### id函数、hash函数
359359

360-
id函数用于获取对象的内存地址。id函数返回的值是对象的内存地址的整数表示,用于快速查找和比较对象。
360+
id函数用于获取对象的内存地址。id函数返回的值是对象的<Highlight color="g">内存地址的整数表示</Highlight>,用于快速查找和比较对象。所有对象都可以用id函数来获取其内存地址。
361+
362+
id保证在对象存活期间进程内唯一。标识“这是哪一个对象实例”。
361363

362364
函数签名:`id(object) -> int`
363365

364-
hash函数用于获取对象的哈希值。哈希值是对象的唯一标识,只有不可变对象才有哈希值,同id函数一样用于快速查找和比较对象。
366+
hash函数用于获取对象的哈希值。<Highlight color="g">只有不可变对象才有哈希值</Highlight>,同id函数一样用于快速查找和比较对象。
367+
368+
用于标识“这个对象的内容(或值)是什么”。在相同进程下,等价对象应该有相同的 hash。
365369

366370
函数签名:`hash(object) -> int`
367371

368-
```python
369-
a = (1,2,3)
370-
print(id(a))
371-
print(hash(a))
372+
```python showLineNumbers
373+
a = tuple([1,2,3])
374+
375+
b = tuple([1,2,3])
376+
377+
print(id(a),id(b))
378+
# id 不同说明不是一个对象
379+
# 2667887440704 2667887219328
380+
381+
print(hash(a),hash(b))
382+
# hash结果相同,说明值相同
383+
# 529344067295497451 529344067295497451
372384
```
373385

374386
## 相关库推荐
375387

388+
- hashlib: 更加丰富的hash方法。
376389
- <Highlight color="g">typing: 对类型提示的支持</Highlight>
377390
- <Highlight color="g">pydantic: **第三方**数据验证模块</Highlight>
378391
- pydoc: 文档生成器和在线帮助系统

docs/docs/选择编程语言/Python/1数字.mdx

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,34 @@ print(abs(10.5)) # 10.5
497497
print(abs(3+4j)) # 5.0 - 复数的模长,即sqrt(3²+4²)
498498
```
499499

500+
501+
### divmod函数
502+
503+
以两个(非复数)数字为参数,在作整数除法时,返回商和余数。
504+
505+
若操作数为混合类型,则适用二进制算术运算符的规则。
506+
507+
函数签名:`divmod(a, b) -> tuple`
508+
509+
参数说明:
510+
- `a`:被除数
511+
- `b`:除数
512+
513+
返回值:
514+
- 返回一个包含商和余数的元组
515+
516+
```python showLineNumbers
517+
print(divmod(7, 3)) # (2, 1)
518+
519+
print(divmod(6, 3)) # (2, 0)
520+
521+
print(divmod(6.0, 3.0)) # (2.0, 0.0)
522+
523+
print(divmod(-6, 3)) # (-2, 0)
524+
525+
print(divmod(6.5, 3)) # (2.0, 0.5)
526+
```
527+
500528
### max函数、min函数
501529

502530
二者语法一致、参数一致,只是行为相反。
@@ -528,21 +556,6 @@ def _sort(value):
528556
print(max(lists,key=_sort)) # -2
529557
```
530558

531-
### sum函数
532-
533-
返回可迭代对象中所有元素的和。
534-
535-
函数签名:`sum(iterable, /, start=0)`
536-
537-
参数说明:
538-
- `iterable`:要计算和的可迭代对象
539-
- `start`:要添加到和的初始值,默认为0
540-
541-
```python showLineNumbers
542-
print(sum((1, 2, 3))) # 6
543-
544-
print(sum([1,2,3],10)) # 16
545-
```
546559

547560
### round函数
548561

@@ -571,34 +584,6 @@ print(round(123.555,2)) # 123.56
571584
这不算是程序错误:这一结果是由于大多数十进制小数实际上都不能以浮点数精确地表示。
572585
:::
573586

574-
575-
### divmod函数
576-
577-
以两个(非复数)数字为参数,在作整数除法时,返回商和余数。
578-
579-
若操作数为混合类型,则适用二进制算术运算符的规则。
580-
581-
函数签名:`divmod(a, b) -> tuple`
582-
583-
参数说明:
584-
- `a`:被除数
585-
- `b`:除数
586-
587-
返回值:
588-
- 返回一个包含商和余数的元组
589-
590-
```python showLineNumbers
591-
print(divmod(7, 3)) # (2, 1)
592-
593-
print(divmod(6, 3)) # (2, 0)
594-
595-
print(divmod(6.0, 3.0)) # (2.0, 0.0)
596-
597-
print(divmod(-6, 3)) # (-2, 0)
598-
599-
print(divmod(6.5, 3)) # (2.0, 0.5)
600-
```
601-
602587
### pow函数
603588

604589
函数签名:`pow(base, exp, mod=None)`
@@ -623,6 +608,22 @@ print(pow(2, 3, 5)) # 3
623608
print((2**3) % 5) # 3
624609
```
625610

611+
### sum函数
612+
613+
返回可迭代对象中所有元素的和。
614+
615+
函数签名:`sum(iterable, /, start=0)`
616+
617+
参数说明:
618+
- `iterable`:要计算和的可迭代对象
619+
- `start`:要添加到和的初始值,默认为0
620+
621+
```python showLineNumbers
622+
print(sum((1, 2, 3))) # 6
623+
624+
print(sum([1,2,3],10)) # 16
625+
```
626+
626627

627628
## 相关库推荐
628629

docs/docs/选择编程语言/Python/25迭代器.mdx

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -118,45 +118,3 @@ try:
118118
except StopIteration:
119119
print("迭代器已耗尽")
120120
```
121-
122-
### enumerate 函数
123-
124-
`enumerate()` 函数为可迭代对象添加计数器,返回一个枚举对象(也是迭代器)。
125-
126-
**函数签名:** `enumerate(iterable, start=0) -> iterator`
127-
128-
```python showLineNumbers
129-
# 基本用法:同时获取索引和值
130-
fruits = ['apple', 'banana', 'cherry']
131-
for i, fruit in enumerate(fruits):
132-
print(f"{i}: {fruit}")
133-
# 0: apple
134-
# 1: banana
135-
# 2: cherry
136-
137-
# 指定起始索引
138-
for i, fruit in enumerate(fruits, start=1):
139-
print(f"{i}. {fruit}")
140-
# 1. apple
141-
# 2. banana
142-
# 3. cherry
143-
144-
# enumerate 返回的是迭代器
145-
enum_obj = enumerate(fruits)
146-
print(type(enum_obj)) # <class 'enumerate'>
147-
print(next(enum_obj)) # (0, 'apple')
148-
print(next(enum_obj)) # (1, 'banana')
149-
150-
# 验证 enumerate 对象是迭代器
151-
print({'__iter__', '__next__'} <= set(dir(enum_obj))) # True
152-
153-
# 实际应用:查找元素位置
154-
numbers = [10, 20, 30, 40, 50]
155-
target = 30
156-
for i, num in enumerate(numbers):
157-
if num == target:
158-
print(f"找到 {target} 在索引 {i}")
159-
break
160-
```
161-
162-

docs/docs/选择编程语言/Python/3序列.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,20 @@ print(sorted(dicts.items(), key=lambda x: x[1], reverse=True))
14741474
"""
14751475
[('b', 5), ('d', 4), ('c', 3), ('e', 2), ('a', 1)]
14761476
"""
1477+
1478+
# 如果你想要同时对字典的键和值排序, 可以试试下面的方法
1479+
dicts = {'a': 1, 'b': 1, 'c': 3, 'd': 1, 'e': 3}
1480+
print(sorted(dicts.items(), key=lambda x: (x[1],x[0]), reverse=False))
14771481
```
14781482

1483+
:::tip
1484+
当 Python 比较元组时,首先比较每个元组的第一个元素,并确定哪个元素较小。如果第一个元素小于另一个元组中匹配的第一个元素,则整个元组被视为小于另一个元组。
1485+
1486+
但如果出现平局,Python 会查看每个元组中的第二个元素。
1487+
1488+
字符串比较时,会逐字符比较其 Unicode 码点值。
1489+
:::
1490+
14791491
### reversed函数
14801492

14811493
reversed函数用于反转序列,返回一个迭代器对象。

0 commit comments

Comments
 (0)