File tree Expand file tree Collapse file tree 4 files changed +8
-8
lines changed Expand file tree Collapse file tree 4 files changed +8
-8
lines changed Original file line number Diff line number Diff line change 31
31
32
32
当然如果要构造出更多的分支,可以使用` if…elif…else… ` 结构,例如下面的分段函数求值。
33
33
34
- ![ ] ( ./res/formula_1.png )
34
+ ![ $$ f(x)=\begin{cases} 3x-5&\text{(x>1)}\\x+2&\text{(-1}\leq\text{x}\leq\text{1)}\\5x+3&\text {(x<-1)}\end{cases} $$ ] ( ./res/formula_1.png )
35
35
36
36
``` Python
37
37
"""
Original file line number Diff line number Diff line change 6
6
7
7
### for-in循环
8
8
9
- 如果明确的知道循环执行的次数或者是要对一个容器进行迭代(后面会讲到),那么我们推荐使用` for-in ` 循环,例如下面代码中计算![ ] ( ./res/formula_1.png ) 。
9
+ 如果明确的知道循环执行的次数或者是要对一个容器进行迭代(后面会讲到),那么我们推荐使用` for-in ` 循环,例如下面代码中计算![ $\sum _ {n=1}^{100}n$ ] ( ./res/formula_1.png ) 。
10
10
11
11
``` Python
12
12
"""
Original file line number Diff line number Diff line change 2
2
3
3
在讲解本章节的内容之前,我们先来研究一道数学题,请说出下面的方程有多少组正整数解。
4
4
5
- ![ ] ( ./res/formula_1.png )
5
+ ![ $$ x_1 + x_2 + x_3 + x_4 = 8 $$ ] ( ./res/formula_1.png )
6
6
7
7
事实上,上面的问题等同于将8个苹果分成四组每组至少一个苹果有多少种方案。想到这一点问题的答案就呼之欲出了。
8
8
9
- ![ ] ( ./res/formula_2.png )
9
+ ![ $$ C_M^N =\frac{M!}{N!(M-N)!}, \text{(M=7, N=3)} $$ ] ( ./res/formula_2.png )
10
10
11
11
可以用Python的程序来计算出这个值,代码如下所示。
12
12
Original file line number Diff line number Diff line change 2
2
3
3
### 使用字符串
4
4
5
- 第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台电子计算机ENIAC每秒钟能够完成约5000次浮点运算。随着时间的推移,虽然对数值运算仍然是计算机日常工作中最为重要的事情之一,但是今天的计算机处理得更多的数据都是以文本信息的方式存在的,而Python表示文本信息的方式我们在很早以前就说过了,那就是字符串类型。所谓** 字符串** ,就是由零个或多个字符组成的有限序列,一般记为![ ] ( ./res/formula_1.png ) 。
5
+ 第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台电子计算机ENIAC每秒钟能够完成约5000次浮点运算。随着时间的推移,虽然对数值运算仍然是计算机日常工作中最为重要的事情之一,但是今天的计算机处理得更多的数据都是以文本信息的方式存在的,而Python表示文本信息的方式我们在很早以前就说过了,那就是字符串类型。所谓** 字符串** ,就是由零个或多个字符组成的有限序列,一般记为![ $$ {\displaystyle s=a_{1}a_{2}\dots a_{n}(0\leq n \leq \infty)} $$ ] ( ./res/formula_1.png ) 。
6
6
7
7
我们可以通过下面的代码来了解字符串的使用。
8
8
@@ -183,11 +183,11 @@ if __name__ == '__main__':
183
183
184
184
除了上面提到的生成器语法,Python中还有另外一种定义生成器的方式,就是通过` yield ` 关键字将一个普通函数改造成生成器函数。下面的代码演示了如何实现一个生成[ 斐波拉切数列] ( https://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97 ) 的生成器。所谓斐波拉切数列可以通过下面[ 递归] ( https://zh.wikipedia.org/wiki/%E9%80%92%E5%BD%92 ) 的方法来进行定义:
185
185
186
- ![ ] ( ./res/formula_2.png )
186
+ ![ $$ {\displaystyle F_{0}=0} $$ ] ( ./res/formula_2.png )
187
187
188
- ![ ] ( ./res/formula_3.png )
188
+ ![ $$ {\displaystyle F_{1}=1} $$ ] ( ./res/formula_3.png )
189
189
190
- ![ ] ( ./res/formula_4.png )
190
+ ![ $$ {\displaystyle F_{n}=F_{n-1}+F_{n-2}}({n}\geq{2}) $$ ] ( ./res/formula_4.png )
191
191
192
192
![ ] ( ./res/fibonacci-blocks.png )
193
193
You can’t perform that action at this time.
0 commit comments