Skip to content

Commit bd7e9cd

Browse files
authored
Merge pull request #12 from HuaYuXiao/master
针对虚数教程的补充
2 parents d73240d + 1ea6f9d commit bd7e9cd

File tree

4 files changed

+358
-81
lines changed

4 files changed

+358
-81
lines changed

003 虚数妙用/more.ipynb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"PyCharm 复数\n",
8+
"\n",
9+
"2022年10月19日 by littlefean \n",
10+
"\n",
11+
"2023.12.10 updated by YuxiaoHua"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 6,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# -*- encoding: utf-8 -*-\n",
21+
"from typing import *\n",
22+
"import cmath"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 7,
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"name": "stdout",
32+
"output_type": "stream",
33+
"text": [
34+
"z1 = (1+0j)\n",
35+
"z2 = 1j\n"
36+
]
37+
}
38+
],
39+
"source": [
40+
"a1 = 1; b1 = 0\n",
41+
"# 用实部 a1 和虚部 b1 创建复数 z1\n",
42+
"z1 = complex(a1, b1) \n",
43+
"print('z1 = ', z1)\n",
44+
"\n",
45+
"a2 = 0; b2 = 1\n",
46+
"# 用实部 a2 和虚部 b2 创建复数 z2\n",
47+
"z2 = complex(a2, b2) \n",
48+
"print('z2 = ', z2)"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 11,
54+
"metadata": {},
55+
"outputs": [
56+
{
57+
"name": "stdout",
58+
"output_type": "stream",
59+
"text": [
60+
"phase2 = 1.5707963267948966\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"# 传入一个虚数,返回这个虚数的弧度\n",
66+
"phase2 = cmath.phase(z2)\n",
67+
"print('phase2 = ', phase2)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 9,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"angle = 1.5707963267948966\n"
80+
]
81+
}
82+
],
83+
"source": [
84+
"# 快速计算两个向量之间的夹角(弧度制)\n",
85+
"angle = cmath.phase(z2) - cmath.phase(z1)\n",
86+
"print('angle = ', angle)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 12,
92+
"metadata": {},
93+
"outputs": [
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"(1.4142135623730951, 0.7853981633974483)\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"# 长度 , 角度\n",
104+
"z3 = z1 + z2\n",
105+
"print(cmath.polar(z3)) "
106+
]
107+
}
108+
],
109+
"metadata": {
110+
"kernelspec": {
111+
"display_name": "Python 3",
112+
"language": "python",
113+
"name": "python3"
114+
},
115+
"language_info": {
116+
"codemirror_mode": {
117+
"name": "ipython",
118+
"version": 3
119+
},
120+
"file_extension": ".py",
121+
"mimetype": "text/x-python",
122+
"name": "python",
123+
"nbconvert_exporter": "python",
124+
"pygments_lexer": "ipython3",
125+
"version": "3.9.6"
126+
}
127+
},
128+
"nbformat": 4,
129+
"nbformat_minor": 2
130+
}

003 虚数妙用/补充.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

004 pass和点点点/show.ipynb

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"PyCharm show\n",
8+
"\n",
9+
"2022年06月17日 by littlefean\n",
10+
"\n",
11+
"2023.12.10 updated by YuxiaoHua"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 14,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# -*- encoding: utf-8 -*-\n",
21+
"from typing import *"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 15,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"# ... 可以写在注解里面\n",
31+
"def test(a: ..., b: ...) -> ...:\n",
32+
" ...\n",
33+
"\n",
34+
"# 有一个类我还不知道怎么写,我可以先写个占位符\n",
35+
"class A:\n",
36+
" ...\n",
37+
"\n",
38+
"class B:\n",
39+
" pass\n",
40+
"\n",
41+
"def f():\n",
42+
" ...\n",
43+
"\n",
44+
"def g():\n",
45+
" pass"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 16,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"# 写个空循环\n",
55+
"for _ in range(100):\n",
56+
" ..."
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 17,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"for _ in range(100):\n",
66+
" pass\n",
67+
"\n",
68+
"# 经过测试发现 点点点和pass做空循环速度是一样的。"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 18,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"for _ in range(1000):\n",
78+
" ...\n",
79+
" ...\n",
80+
" ...\n",
81+
" ...\n",
82+
"\n",
83+
"# 经过测试发现,这样写四个,并没有出现速度慢了四倍的情况"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"在`Python`中,`Ellipsis` 是一个特殊的对象,用来表示省略。它的类型是 `ellipsis` 类型。"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 19,
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"True\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"print(id(...) == id(Ellipsis))\n",
108+
"\n",
109+
"# 没有小写开头的东西了,因为是单例了,虽然pycharm里这个单词还会变蓝\n",
110+
"# print(id(...) == id(ellipsis)) "
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 20,
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"name": "stdout",
120+
"output_type": "stream",
121+
"text": [
122+
"<class 'ellipsis'> <class 'ellipsis'>\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"print(type(...), type(Ellipsis))"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"这里的 `Ellipsis.__class__` 返回 `ellipsis` 类型,它表示省略对象的类型。这部分不需要括号,因为我们只是获取类型。\n",
135+
"\n",
136+
"而 `Ellipsis.__class__()` 中的括号是用来创建 `ellipsis` 类型的实例的。"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 21,
142+
"metadata": {},
143+
"outputs": [
144+
{
145+
"name": "stdout",
146+
"output_type": "stream",
147+
"text": [
148+
"<class 'ellipsis'> Ellipsis\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"print(Ellipsis.__class__, Ellipsis.__class__())"
154+
]
155+
},
156+
{
157+
"cell_type": "markdown",
158+
"metadata": {},
159+
"source": [
160+
"但是,`ellipsis` 类型是一个特殊的单例对象,不允许通过构造函数创建多个实例。因此,即使你加上括号,它仍然会返回 `Ellipsis` 单例对象。"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 22,
166+
"metadata": {},
167+
"outputs": [
168+
{
169+
"ename": "TypeError",
170+
"evalue": "'ellipsis' object is not callable",
171+
"output_type": "error",
172+
"traceback": [
173+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
174+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
175+
"Cell \u001b[0;32mIn[22], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mEllipsis\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mEllipsis\u001b[39m()\n",
176+
"\u001b[0;31mTypeError\u001b[0m: 'ellipsis' object is not callable"
177+
]
178+
}
179+
],
180+
"source": [
181+
"a = Ellipsis()\n",
182+
"b = Ellipsis()"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": 23,
188+
"metadata": {},
189+
"outputs": [
190+
{
191+
"name": "stdout",
192+
"output_type": "stream",
193+
"text": [
194+
"ellipsis\n",
195+
"(<class 'object'>,)\n",
196+
"<class 'type'>\n"
197+
]
198+
}
199+
],
200+
"source": [
201+
"print(Ellipsis.__class__.__name__)\n",
202+
"print(Ellipsis.__class__.__bases__)\n",
203+
"print(Ellipsis.__class__.__class__)"
204+
]
205+
}
206+
],
207+
"metadata": {
208+
"kernelspec": {
209+
"display_name": "Python 3",
210+
"language": "python",
211+
"name": "python3"
212+
},
213+
"language_info": {
214+
"codemirror_mode": {
215+
"name": "ipython",
216+
"version": 3
217+
},
218+
"file_extension": ".py",
219+
"mimetype": "text/x-python",
220+
"name": "python",
221+
"nbconvert_exporter": "python",
222+
"pygments_lexer": "ipython3",
223+
"version": "3.9.6"
224+
}
225+
},
226+
"nbformat": 4,
227+
"nbformat_minor": 2
228+
}

0 commit comments

Comments
 (0)