diff --git a/README.md b/README.md index 7e9513f..91ab7d1 100644 --- a/README.md +++ b/README.md @@ -17,15 +17,38 @@ 告别枯燥,告别枯燥,致力于打造 Python 经典小例子、小案例。 -同时托管在[Python中文网](http://www.zglg.work/python-small-examples/) - -使用Python制作的各种小工具:[Python实用小工具](http://www.zglg.work:5000/pytools) - ## License 允许按照要求转载,但禁止用于任何商用目的。如果转载本库小例子、小案例,请备注下方链接: -Python小例子 https://github.com/jackzhenguo/python-small-examples +[Python小例子所有汇总](https://ai-jupyter.com/python-small-examples/) + +### 更多教程 + +[AI消息](https://ai-jupyter.com/) + +[AI新闻报道](https://ai-jupyter.com/ai-news-all/) + +[AI大模型](https://ai-jupyter.com/ai-llm/) + +[AI工具集](https://ai-jupyter.com/ai-chatgpt/) + +[ChatGPT4o免费使用六种方法](https://ai-jupyter.com/ai-chatgpt/) + +[Python教程](https://ai-jupyter.com/python-packages/) + +[数据分析教程](https://ai-jupyter.com/numpy-intro/) + +[算法教程](https://ai-jupyter.com/algorithm-basic/) + +[AI教程](https://ai-jupyter.com/statistics/) + +[Git教程](https://ai-jupyter.com/git/) + +[程序员](https://ai-jupyter.com/others/) + +[资料下载](https://ai-jupyter.com/python-20/) + ## Python 小例子 @@ -98,7 +121,7 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples | 24 | [使用堆升序列表](md/196.md) | sort heapq | v1.0 | ⭐️⭐⭐⭐ | -### 面向对象OOP +### 面向对象 | 小例子 | 链接 | 标签 | 版本 | 难度 | | ---- | ---------------------------------- | ---- | ---- | ---- | | 1 | [所有对象之根](md/43.md) | object | V1.0 | ⭐️ | @@ -240,7 +263,7 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples | 37 | [一行代码让 pip 安装加速 100 倍](md/176.md) | pip install | v1.0 | ⭐️⭐⭐ | -### 案例 +### 工作常用案例 | 小例子 | 链接 | 标签 | 版本 | 难度 | | ---- | ---------------------------------- | ---- | ---- | ---- | | 1 | [不用else和if实现计算器](md/60.md) | operator | V1.0 | ⭐️⭐️⭐️ | @@ -276,104 +299,4 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples -## Python原创教程 -这是经过很久打磨的一个Python教程,全部是个人原创,已首发在公众号,并且托管在我的[个人网站](http://www.zglg.work/python-level/)。 - -想系统入门Python的欢迎学习: - -[Python进阶完整教程](http://www.zglg.work/python-level/) - -[Python前言](http://www.zglg.work/Python-20-topics/intro-python/) - -[Google Python代码风格指南](http://www.zglg.work/Python-20-topics/google-python-coding-style/) - -Python关于数字的20个操作 - -[Python数字](http://www.zglg.work/Python-20-topics/python-number/) - -[Python正则之提取正整数和大于0浮点数](http://www.zglg.work/python-level/python-re-integer-float.md) - -[Python字符串](http://www.zglg.work/Python-20-topics/python-string/) - -[CSV读写乱码问题](http://www.zglg.work/Python-20-topics/csv-rw-coding-issue/) - -[Unicode标准化](http://www.zglg.work/Python-20-topics/unicode-standard/) - -[Unicode, UTF-8, ASCII](http://www.zglg.work/Python-20-topics/unicode-utf-8/) - -[Python动态生成变量](http://www.zglg.work/Python-20-topics/dynamic_variable/) - -[Python字符串对齐](http://www.zglg.work/Python-20-topics/python-string-align/) - -[Python小项目1:文本句子关键词的KWIC显示](http://www.zglg.work/Python-20-topics/python-project1-kwic/) - -[Python列表](http://www.zglg.work/Python-20-topics/python-list/) - -[Python流程控制](http://www.zglg.work/Python-20-topics/python-control/) - -[Python编程习惯专题](http://www.zglg.work/Python-20-topics/python-program-habit/) - -[Python函数专题](http://www.zglg.work/Python-20-topics/python-functions/) - -[Python面向对象编程-上篇](http://www.zglg.work/Python-20-topics/python-oop-1/) - -[Python面向对象编程-下篇](http://www.zglg.work/Python-20-topics/python-oop-2/) - -[Python十大数据结构使用专题](http://www.zglg.work/Python-20-topics/python-data-structure/) - -[Python包和模块使用注意事项专题](http://www.zglg.work/Python-20-topics/python-package-module-apply-items/) - -[Python正则使用专题](http://www.zglg.work/Python-20-topics/python-re-apply/) - -[Python时间专题](http://www.zglg.work/Python-20-topics/python-time/) - -[Python装饰器专题](http://www.zglg.work/Python-20-topics/python-decorator-apply/) - -[Python迭代器使用专题](http://www.zglg.work/Python-20-topics/python-iterator-apply/) - -[Python生成器使用专题](http://www.zglg.work/Python-20-topics/python-generator-apply/) - -[Python 绘图入门专题](http://www.zglg.work/Python-20-topics/python-graph-intro/) - -[Matplotlib绘图基础专题](http://www.zglg.work/Python-20-topics/python-matplotlib-1/) - -[Matplotlib绘图进阶专题](http://www.zglg.work/Python-20-topics/python-matplotlib-2/) - -[Matplotlib绘图案例](http://www.zglg.work/Python-20-topics/python-matplotlib-examples/) - -[NumPy图解入门](http://www.zglg.work/Python-20-topics/numpy-graph-intro/) - - -## NumPy入门教程 - -1 [NumPy介绍](http://www.zglg.work/numpy-intro/) - -2 [安装和导入NumPy](http://www.zglg.work/numpy/numpy-install/) - -3 [Python列表和NumPy数组有什么区别?](http://www.zglg.work/numpy/numpy-array-different-python-list/) - -4 [什么是array?](http://www.zglg.work/numpy/what-is-numpy/) - -5 [有关Array的详细信息](http://www.zglg.work/numpy/numpy-array-more/) - -6 [如何创建array](http://www.zglg.work/numpy/numpy-create-array/) - -7 [添加、删除和排序元素](http://www.zglg.work/numpy/numpy-add-remove-sort/) - -8 [数组形状和大小](http://www.zglg.work/numpy/numpy-shape/) - -9 [重塑array](http://www.zglg.work/numpy/numpy-reshape/) - -10 [如何将一维array转换为二维array(如何向数组添加新轴)](http://www.zglg.work/numpy/numpy-1d-2d-more/) - -11 [NumPy索引和切片](http://www.zglg.work/numpy/numpy-indexing-slicing/) - -12 [如何从现有数据创建数组](http://www.zglg.work/numpy/numpy-create-an-array-from-existing-data/) - -13 [数组基本操作](http://www.zglg.work/numpy/numpy-basic-array-operations/) - -14 [广播](http://www.zglg.work/numpy/numpy-broadcasting/) - -15 [更有用的数组操作](http://www.zglg.work/numpy/numpy-more-array-operations/) -16 [生成随机数](http://www.zglg.work/numpy/numpy-generate-random-number/) diff --git a/md/1.md b/md/1.md index f135dee..817a25e 100644 --- a/md/1.md +++ b/md/1.md @@ -4,7 +4,7 @@ @date 2019/2/10 ``` -#### 2 实现 relu +#### 1 实现 relu 在神经网络中,`relu`作为神经元的激活函数: diff --git a/md/10.md b/md/10.md index b8ca63b..1fdec25 100644 --- a/md/10.md +++ b/md/10.md @@ -6,12 +6,39 @@ #### 10 转为整型   -int(x, base =10) , x可能为字符串或数值,将 x 转换为一个普通整数。如果参数是字符串,那么它可能包含符号和小数点。 +int(x, base =10) , x可能为字符串或数值,将 x 转换为一个普通整数。 + +参数base指定x进制数,常见的2,8,10,16分别表示二进制、八进制、十进制、十六进制的数字 + +如果参数是字符串,必须为整数型字符串,如果是浮点数字符串会抛出异常。 + +如果x是浮点数,int后截去小数点,只保留整数部分。 ```python -In [1]: int('12',16) -Out[1]: 18 +In [2]: int('0110',2) + +Out[2]: 6 + +In [3]: int('0732',8) +Out[3]: 474 + +In [4]: int('12',16) +Out[4]: 18 + +In [5]: int('12',10) +Out[5]: 12 + +In [6]: int(1.45) +Out[6]: 1 + +In [7]: int('1.45') +--------------------------------------------------------------------------- +ValueError Traceback (most recent call last) + in +----> 1 int('1.45') + +ValueError: invalid literal for int() with base 10: '1.45' ``` -
[上一个例子](9.md) [下一个例子](11.md)
\ No newline at end of file +
[上一个例子](9.md) [下一个例子](11.md)
diff --git a/md/11.md b/md/11.md index 40874b4..3107239 100644 --- a/md/11.md +++ b/md/11.md @@ -9,9 +9,12 @@ base为底的exp次幂,如果mod给出,取余 ```python -In [1]: pow(3, 2, 4) +In [1]: pow(2,1.5) +Out[1]: 2.8284271247461903 + +In [1]: pow(3, 2, 4) # 3的2次方结果再对4取余数 Out[1]: 1 ``` -
[上一个例子](10.md) [下一个例子](12.md)
\ No newline at end of file +
[上一个例子](10.md) [下一个例子](12.md)
diff --git a/md/118.md b/md/118.md index ca14fe8..8b3eaec 100644 --- a/md/118.md +++ b/md/118.md @@ -23,7 +23,7 @@ print(t) # <_MainThread(MainThread, started 139908235814720)> 所以,验证了程序默认是在`MainThead`中执行。 -`t.getName()`获得这个线程的名字,其他常用方法,`getName()`获得线程`id`,`isAlive()`判断线程是否存活等。 +`t.getName()`获得这个线程的名字,其他常用方法,`t.ident`获得线程`id`,`isAlive()`判断线程是否存活等。 ```python print(t.getName()) # MainThread @@ -31,4 +31,4 @@ print(t.ident) # 139908235814720 print(t.isAlive()) # True ``` -
[上一个例子](117.md) [下一个例子](119.md)
\ No newline at end of file +
[上一个例子](117.md) [下一个例子](119.md)
diff --git a/md/123.md b/md/123.md index 20f70c4..ccff06f 100644 --- a/md/123.md +++ b/md/123.md @@ -54,10 +54,10 @@ t8 adds a to 1: 9 t9 adds a to 1: 10 ``` -一起正常,其实这已经是单线程顺序执行了,就本例子而言,已经失去多线程的价值,并且还带来了因为线程创建开销,浪费时间的副作用。 +一切正常,其实这已经是单线程顺序执行了,就本例子而言,已经失去多线程的价值,并且还带来了因为线程创建开销,浪费时间的副作用。 程序中只有一把锁,通过 `try...finally`还能确保不发生死锁。但是,当程序中启用多把锁,还是很容易发生死锁。 注意使用场合,避免死锁,是我们在使用多线程开发时需要注意的一些问题。 -
[上一个例子](122.md) [下一个例子](124.md)
\ No newline at end of file +
[上一个例子](122.md) [下一个例子](124.md)
diff --git a/md/13.md b/md/13.md index 634ac88..3cc2f3c 100644 --- a/md/13.md +++ b/md/13.md @@ -6,6 +6,8 @@ #### 13 链式比较 +Python支持这种连续不等比较,写起来更方便 + ```python i = 3 print(1 < i < 3) # False @@ -13,4 +15,4 @@ print(1 < i <= 3) # True ``` -
[上一个例子](12.md) [下一个例子](14.md)
\ No newline at end of file +
[上一个例子](12.md) [下一个例子](14.md)
diff --git a/md/14.md b/md/14.md index a66f7ac..061a1d8 100644 --- a/md/14.md +++ b/md/14.md @@ -11,9 +11,20 @@ ```python In [12]: s = "apple" -In [13]: bytes(s,encoding='utf-8') -Out[13]: b'apple' +In [13]: a = bytes(s,encoding='utf-8') +In [14] a +Out[14]: b'apple' + +# 转化后a变为字节序列,bytes类型, +# 并且每个字符都被转化为数值,如下所示 +In [15]: for i in a: + ...: print(i) +97 +112 +112 +108 +101 ``` -
[上一个例子](13.md) [下一个例子](15.md)
\ No newline at end of file +
[上一个例子](13.md) [下一个例子](15.md)
diff --git a/md/15.md b/md/15.md index 60fdbe9..b512730 100644 --- a/md/15.md +++ b/md/15.md @@ -7,16 +7,30 @@ #### 15 任意对象转为字符串   ```python -In [1]: i = 100 + -In [2]: str(i) -Out[2]: '100' +In [1]: str(100) +Out[1]: '100' -In [3]: str([]) -Out[3]: '[]' +In [2]: str([3,2,10]) +Out[2]: '[3, 2, 10]' + +In [3]: str({'a':1, 'b':10}) +Out[3]: "{'a': 1, 'b': 10}" + +In [11]: from collections import defaultdict +In [12]: dd = defaultdict(int) + +In [14]: for i in [1,3,2,2,3,3]: + ...: dd[i] += 1 + ...: + +In [15]: dd +Out[15]: defaultdict(int, {1: 1, 3: 3, 2: 2}) + +In [16]: str(dd) +Out[16]: "defaultdict(, {1: 1, 3: 3, 2: 2})" -In [4]: str(tuple()) -Out[4]: '()' ``` -
[上一个例子](14.md) [下一个例子](16.md)
\ No newline at end of file +
[上一个例子](14.md) [下一个例子](16.md)
diff --git a/md/159.md b/md/159.md index 84bec8a..98b7426 100644 --- a/md/159.md +++ b/md/159.md @@ -13,7 +13,7 @@ ```python def del_item(lst,e): - return [lst.remove(i) for i in e if i==e] # NO! + return [lst.remove(i) for i in lst if i==e] # NO! ``` 考虑删除这个序列[1,3,3,3,5]中的元素3,结果发现只删除其中两个: @@ -31,4 +31,4 @@ def del_item(lst,e): ``` -
[上一个例子](158.md) [下一个例子](160.md)
\ No newline at end of file +
[上一个例子](158.md) [下一个例子](160.md)
diff --git a/md/16.md b/md/16.md index 45bccd4..5cc1a5a 100644 --- a/md/16.md +++ b/md/16.md @@ -18,6 +18,18 @@ Out[3]: at 0x0000000005DE75D0, file "", line 1> In [4]: exec(r) helloworld + +s = """ +def f(): + a = 100 % 52 + print(a) +f() +""" +r = compile(s,"", "exec") +exec(r) ``` -
[上一个例子](15.md) [下一个例子](17.md)
\ No newline at end of file +输出 +48 + +
[上一个例子](15.md) [下一个例子](17.md)
diff --git a/md/17.md b/md/17.md index e09883c..3b3c360 100644 --- a/md/17.md +++ b/md/17.md @@ -13,6 +13,21 @@ In [1]: s = "1 + 3 +5" In [2]: eval(s) Out[2]: 9 + +s = ["{'小汽车':10, '面包车':8}", "{'面包车':5}"] +from collections import defaultdict +d = defaultdict(int) + +for item in s: + my_dict = eval(item) + print(type(my_dict)) + for key in my_dict: + d[key] += my_dict[key] +print(d) + + + +defaultdict(, {'小汽车': 10, '面包车': 13}) ``` -
[上一个例子](16.md) [下一个例子](18.md)
\ No newline at end of file +
[上一个例子](16.md) [下一个例子](18.md)
diff --git a/md/2.md b/md/2.md index 22a5a27..aa58fea 100644 --- a/md/2.md +++ b/md/2.md @@ -8,21 +8,21 @@ 十进制转换为二进制: ```python -In [2]: bin(10) -Out[2]: '0b1010' +In [1]: bin(10) +Out[1]: '0b1010' ``` 十进制转换为八进制: ```python -In [3]: oct(9) -Out[3]: '0o11' +In [2]: oct(9) +Out[2]: '0o11' ``` 十进制转换为十六进制: ```python -In [4]: hex(15) -Out[4]: '0xf' +In [3]: hex(15) +Out[3]: '0xf' ``` - - + +
[上一个例子](1.md) [下一个例子](3.md)
\ No newline at end of file diff --git a/md/23.md b/md/23.md index 05fb188..ac994a9 100644 --- a/md/23.md +++ b/md/23.md @@ -6,6 +6,30 @@ #### 23 交换两元素 +理解交换两个元素,需要首先明白什么是pack,什么是unpack + +```python +In [1]: a=[3,1] + +# unpack +In [2]: a0,a1 = a + +In [3]: a0 +Out[3]: 3 + +In [4]: a1 +Out[4]: 1 + +# pack +In [5]: b = a0, a1 + +In [6]: b +Out[6]: (3, 1) + +``` + +所以下面 `b,a = a,b` 交换2个元素的过程,实际是先pack a,b为元组 (a,b),然后再unpack (a,b) 给 b, a的过程 + ```python def swap(a, b): return b, a @@ -14,4 +38,4 @@ def swap(a, b): print(swap(1, 0)) # (0,1) ``` -
[上一个例子](22.md) [下一个例子](24.md)
\ No newline at end of file +
[上一个例子](22.md) [下一个例子](24.md)
diff --git a/md/29.md b/md/29.md index a228289..9998a64 100644 --- a/md/29.md +++ b/md/29.md @@ -9,17 +9,24 @@ 创建数据字典 ```python +# 方法1:使用dict In [1]: dict() Out[1]: {} - In [2]: dict(a='a',b='b') Out[2]: {'a': 'a', 'b': 'b'} +# 方法2:zip In [3]: dict(zip(['a','b'],[1,2])) Out[3]: {'a': 1, 'b': 2} +# 方法3:嵌入元组的列表 In [4]: dict([('a',1),('b',2)]) Out[4]: {'a': 1, 'b': 2} + +# 方法4:自典型字符串 +In [1]: s = "{'a':1, 'b':2}" +In [2]: eval(s) +Out[2]: {'a': 1, 'b': 2} ``` -
[上一个例子](28.md) [下一个例子](30.md)
\ No newline at end of file +
[上一个例子](28.md) [下一个例子](30.md)
diff --git a/md/31.md b/md/31.md index f2417ab..c9a7354 100644 --- a/md/31.md +++ b/md/31.md @@ -9,10 +9,37 @@ 返回一个set对象,集合内不允许有重复元素: ```python -In [159]: a = [1,4,2,3,1] +In [1]: a = [1,4,2,3,1] -In [160]: set(a) -Out[160]: {1, 2, 3, 4} +In [2]: set(a) +Out[2]: {1, 2, 3, 4} + +In [3]: b = set(a) + +In [4]: b.add(5) + +In [5]: b +Out[5]: {1, 2, 3, 4, 5} + +In [6]: b.pop() +Out[6]: 1 + +In [7]: b +Out[7]: {2, 3, 4, 5} + +In [8]: b.pop() +Out[8]: 2 + +In [9]: b +Out[9]: {3, 4, 5} + +# 注意pop删除集合内任意一个元素 +In [10]: help(b.pop) +Help on built-in function pop: + +pop(...) method of builtins.set instance + Remove and return an arbitrary set element. + Raises KeyError if the set is empty. ``` -
[上一个例子](30.md) [下一个例子](32.md)
\ No newline at end of file +
[上一个例子](30.md) [下一个例子](32.md)
diff --git a/md/48.md b/md/48.md index 3807325..c62565f 100644 --- a/md/48.md +++ b/md/48.md @@ -5,14 +5,22 @@ ``` #### 48 查看变量所占字节数 +`getsizeof`查看变量占用字节数 +看到:字典比列表占用更多空间 ```python In [1]: import sys -In [2]: a = {'a':1,'b':2.0} +In [3]: a = [('a',1),('b',2)] + +In [5]: sys.getsizeof(a) +Out[5]: 88 + + +In [6]: a = {'a':1,'b':2.0} +In [7]: sys.getsizeof(a) +Out[7]: 248 -In [3]: sys.getsizeof(a) # 占用240个字节 -Out[3]: 240 ``` -
[上一个例子](47.md) [下一个例子](49.md)
\ No newline at end of file +
[上一个例子](47.md) [下一个例子](49.md)
diff --git a/md/9.md b/md/9.md index 982caa4..3cd61f7 100644 --- a/md/9.md +++ b/md/9.md @@ -17,6 +17,20 @@ In [1]: float('3') Out[1]: 3.0 ``` +浮点数最大值 +```python +import sys + +In[4]: sys.float_info.max +Out[4]: 1.7976931348623157e+308 +``` + +正无穷大、负无穷大 +```python +float('inf') # 正无穷大 +float('-inf') # 负无穷大 +``` + 如果不能转化为浮点数,则会报`ValueError`: ```python diff --git a/script/avatar.png b/script/avatar.png new file mode 100644 index 0000000..4455032 Binary files /dev/null and b/script/avatar.png differ