From cc8f20c0da231cbafdfd64451a74738cebcecfd4 Mon Sep 17 00:00:00 2001 From: Chuanyu Xue Date: Sun, 18 Jul 2021 11:24:32 +0800 Subject: [PATCH] fix the issue of graph display --- .DS_Store | Bin 0 -> 6148 bytes 1. Python 安装及基本语法.ipynb | 720 ++++++++++++++++----------------- 2 files changed, 360 insertions(+), 360 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..200e86bfbbc8a48fe4196cb35b954504cdadece1 GIT binary patch literal 6148 zcmeHKJ5Iwu5S<||EK!h>3T;V&Ko(Tcn8Xe3P>@I_=whLfh%wWZo?I5c(XGQ zuVPU~2+c^lZ+1RA_LIEpAtD|<%?qL-5#<c;0!neKMctE z5HJKw!&WgK9hhPZ0L);H0$q9u$q9y~VXFuagf$hYscbC_?~igXoAW4O>OoMeH#h=syCH5TBfZEimu}j0Qnv literal 0 HcmV?d00001 diff --git a/1. Python 安装及基本语法.ipynb b/1. Python 安装及基本语法.ipynb index eb97719..3fb80d3 100644 --- a/1. Python 安装及基本语法.ipynb +++ b/1. Python 安装及基本语法.ipynb @@ -2,67 +2,66 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, "source": [ "# Python 安装及基本语法" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "Python 是一种上手简单、功能丰富的编程语言,是数据科学领域首选的可靠工具。通过 Python 你可以迅速解决问题,而不是把精力消耗在复杂的语法与编程细节上,这与本教程的核心思想“简洁”不谋而合。\n", "\n", "当全世界的数据科学家与研究人员享受 Python 带来的便利时,另一部分人正在致力于开发各种强大的 数据分析 / 机器学习 / 神经网络运行库,使得复杂的算法和模型不再成为技术门槛,越来越多的人都有机会进入数据科学领域进行学习与创新。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "## 1.1.1 Python 安装运行" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.1.1 下载安装" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "- 对于 Windows / Mac 用户,首先在官网 https://www.python.org/downloads/ 下载对应操作系统的 Python3 安装包,然后双击运行安装。\n", "\n", "- 对于Linux用户,执行 sudo apt-get update && sudo apt-get install python3 命令进行安装。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "在安装完成后,打开命令提示符(win + R键,输入cmd回车) / 终端(command + 空格,输入term回车),执行 python3 -V 命令,若出现对应python版本号则安装成功。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ - " " - ] + " " + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.1.2 编辑器与解释器\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "一个 Python 程序要经过“编写”和“运行”两个阶段。\n", "\n", @@ -72,18 +71,18 @@ "编辑器可以是notepad++、emeditor、甚至 windows 中最常见的 txt 编辑器。但为了提高我们的编程效率,我们往往选择功能更全面的 PyCharm 或者 vscode 等专用集成代码编辑器(IDLE)。\n", "\n", "解释器就是我们在上步安装的 python 可执行文件,在 windows 中它是 python3.exe,在 Mac / Linux 中它是名为 python3 的可执行文件。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.1.3 第一个Python程序" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "在成功安装后,我们将尝试编写第一个Python程序。\n", "\n", @@ -92,43 +91,43 @@ "- Mac 在 启动 -> IDLE 打开,然后菜单栏 File -> New File\n", "\n", "然后输入 print(\"hello world\"),并保存到任意位置,我们的程序编写阶段就完成啦!" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ " " - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "下一步我们尝试用解释器运行保存的.py程序。\n", "\n", "首先通过命令行输入 cd ~/\\*\\*\\* 进入到文件存储的位置(这里 \\*\\*\\* 是你文件储存位置的路径)\n", "\n", "然后命令行执行 Python3 test.py 命令,这时输出 hello world 字样则程序运行成功!" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ " " - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.1.4 交互式环境" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "相比于其他编程语言,Python的一个特色是交互式环境,为我们提供了可以同时编写运行的编程方式。\n", "\n", @@ -141,50 +140,50 @@ "这时你发现你的浏览器自动打开了,并且出现了一个在线的精美简洁的编辑器,这就是notebook的主界面。\n", "\n", "我们点击右上角的 New 按钮,选择 Python3,然后输入 print('hello world'),之后按下上面的 Run 按钮,这时我们发现在代码的下面也出现了hello world。\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ " " - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "编写代码之后即刻运行,运行之后还可以继续编辑,省去了不停打开编写保存运行的过程,这便是交互式编程的奇妙之处!而交互式编程带来的便捷不止如此,在数据科学中我们经常要处理较大的数据,整个程序的运行动辄十几分钟。通过交互式编程我们可以一行一行运行程序,同时通过下面的输出内容观察程序的中间运行结果,在出现错误时及时修改代码,从而节省重复运行程序的时间!\n", "\n", "下面的教程中,交互式编程将全程陪伴你的学习过程,相信随着时间你会更加感触到先进工具带来的效率提升!" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "## 1.1.2 Python 常量与变量" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "可能你现在会产生疑惑,代码中的 print 代表什么意义?括号又是什么作用?为什么 hello world 外面有个双引号?没关系,下面我们就来了解 Python 语法的奥秘。\n", "\n", "首先我们介绍常量与变量,他们相当于语言中的 “名词” 部分,功能是用来表示某些事物。 " - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.2.1 常量" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "常量是编程语言中固定的量,它的值不能改变。例如 2 就表示数字二,不能被修改表示其他值。Python 中的常量包括数字,字符串,逻辑值三种。\n", "\n", @@ -193,241 +192,241 @@ "- 逻辑值:True 代表真, False 代表假\n", "\n", "使用 type( \\* ) 可以查看 \\* 的类型,例如 type(2) 返回 int 表示 2 为 整数。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "source": [ + "## 2 表示 整数(integer) 2\n", + "type(2)" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "int" ] }, - "execution_count": 6, "metadata": {}, - "output_type": "execute_result" + "execution_count": 6 } ], - "source": [ - "## 2 表示 整数(integer) 2\n", - "type(2)" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "source": [ + "## 2.333 与 1e-9 表示对应浮点数(float)\n", + "type(2.33)" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "float" ] }, - "execution_count": 7, "metadata": {}, - "output_type": "execute_result" + "execution_count": 7 } ], - "source": [ - "## 2.333 与 1e-9 表示对应浮点数(float)\n", - "type(2.33)" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "source": [ + "type(1e-9)" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "float" ] }, - "execution_count": 8, "metadata": {}, - "output_type": "execute_result" + "execution_count": 8 } ], - "source": [ - "type(1e-9)" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "source": [ + "## 用单引号括起来的内容表示字符串(string)\n", + "\n", + "type('这是1个字符串')" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "str" ] }, - "execution_count": 10, "metadata": {}, - "output_type": "execute_result" + "execution_count": 10 } ], - "source": [ - "## 用单引号括起来的内容表示字符串(string)\n", - "\n", - "type('这是1个字符串')" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 24, - "metadata": {}, + "source": [ + "## True 代表逻辑值(Boolen)\n", + "type(True)" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "bool" ] }, - "execution_count": 24, "metadata": {}, - "output_type": "execute_result" + "execution_count": 24 } ], - "source": [ - "## True 代表逻辑值(Boolen)\n", - "type(True)" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.2.2 变量" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "与常量相反,变量可以存储不同的值以表示不同的内容,并且它的值可以被更改。变量通过赋值符号 = 创建,例如variable = 1。\n", "\n", "注意变量存在命名规范。变量的第一个字符必须是字母,其余部分由下划线或数字组成,且区分大小写。例如 a_123 可以是一个变量,123_a 不可以是一个变量,A_123 与 a_123 表示两个不同变量。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ "## 创建名为 variable 的变量存储一个 整数 值\n", "\n", "variable = 1\n", "type(variable)" - ] + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "int" + ] + }, + "metadata": {}, + "execution_count": 14 + } + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ "## 修改 variable 变量的值为一个字符串\n", "\n", "variable = 'Hi'\n", "type(variable)" - ] + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 19, - "metadata": {}, + "source": [ + "## 常量的值无法修改 触发语法错误提示\n", + "\n", + "2.33 = 2.55" + ], "outputs": [ { + "output_type": "error", "ename": "SyntaxError", "evalue": "cannot assign to literal (, line 3)", - "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m 2.33 = 2.55\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to literal\n" ] } ], - "source": [ - "## 常量的值无法修改 触发语法错误提示\n", - "\n", - "2.33 = 2.55" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 20, - "metadata": {}, + "source": [ + "## 变量的命名不满足规范 触发语法错误提示\n", + "\n", + "123_a = 1" + ], "outputs": [ { + "output_type": "error", "ename": "SyntaxError", "evalue": "invalid decimal literal (, line 3)", - "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m 123_a = 1\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid decimal literal\n" ] } ], - "source": [ - "## 变量的命名不满足规范 触发语法错误提示\n", - "\n", - "123_a = 1" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "## 1.1.3 Python 运算符与函数" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "学习了常量与变量之后,我们可以在Python中表示一些数值或字符串,然而要想解决更复杂的问题,我们需要了解如何对这些常量与变量进行操作。\n", "\n", "运算符与函数相当于语言中的 “动词” 部分,用来表示某种操作。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.3.1 运算符" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "运算符有以下几种,表示运算操作/逻辑操作/位运算操作\n", "\n", @@ -463,11 +462,11 @@ " \n", "其中最常用的是算数运算符与逻辑运算符,位运算符在 集合 操作中经常使用。\n", " " - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "> 问题:给你任意五个整数和一个目标值target,找到这五个整数中和为target的那两个整数。\n", "\n", @@ -476,24 +475,12 @@ " 输入:2,3,7,11,15, target = 13\n", " 输出:2,7\n", " 解释:第0个数和第2个数的和为13 (注:编程时我们习惯从第0个开始数)" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 36, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ "## 尝试求解,改变 a b 的值依次两两尝试,直到 a + b = target\n", "\n", @@ -501,82 +488,94 @@ "a = 2\n", "b = 3\n", "a + b == target" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, - "execution_count": 37, "metadata": {}, - "output_type": "execute_result" + "execution_count": 36 } ], + "metadata": {} + }, + { + "cell_type": "code", + "execution_count": 37, "source": [ "a = 2\n", "b = 7\n", "a + b == target" - ] + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 38, - "metadata": {}, + "source": [ + "a = 2\n", + "b = 11\n", + "a + b == target" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, - "execution_count": 38, "metadata": {}, - "output_type": "execute_result" + "execution_count": 38 } ], - "source": [ - "a = 2\n", - "b = 11\n", - "a + b == target" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 39, - "metadata": {}, + "source": [ + "a,b" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(2, 11)" ] }, - "execution_count": 39, "metadata": {}, - "output_type": "execute_result" + "execution_count": 39 } ], - "source": [ - "a,b" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.3.2 函数" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "在上述过程中,下列代码被重复多次书写,这时我们可以使用函数减少代码冗余。函数是一种可复用的部件,用于定义更加复杂的操作以减少代码冗余。\n", "\n", @@ -585,11 +584,11 @@ " a + b == target\n", " \n", "如果把运算符比作 “握” “抬手” “张嘴” “吞咽” 等基本动作,那么函数往往是 “吃饭” “喝水” 等一系列基本动作构成的复杂动作。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "函数通过 def 关键字定义,函数的输入由函数名后括号内 参数 定义,函数的结果由 return 关键字定义。\n", "\n", @@ -597,66 +596,65 @@ "- 函数的 返回值 是函数的返回结果,参数与返回值都是可选的。\n", "\n", "另外程序中还存在着预先定义好的函数,例如我们在前面使用的 type 函数。以及 1.1.1.3 节中使用的 print 函数,它的功能是在屏幕输出某个变量的内容。可能你早就心存疑惑,为什么上面不用 print 也能输出呢?原因就在于交互式环境 notebook 会自动输出最后一个变量的内容。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 62, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ "## 自动输出最后的 target\n", "\n", "variable\n", "target" - ] + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "13" + ] + }, + "metadata": {}, + "execution_count": 62 + } + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 63, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hi'" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ "## 自动输出最后的 variable\n", "\n", "target\n", "variable" - ] + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Hi'" + ] + }, + "metadata": {}, + "execution_count": 63 + } + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "下面我们尝试定义一个函数减少之前代码的冗余。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 65, - "metadata": {}, - "outputs": [], "source": [ "## num1,num2 是输入的参数, return 后面的 a+b==target 是函数的结果\n", "\n", @@ -666,11 +664,12 @@ " a = num1\n", " b = num2\n", " return a + b == target" - ] + ], + "outputs": [], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "Python的一大语法特点是缩进敏感,这里第 5,6,7 行距离开头有 1个TAB / 4个空格 的距离并不只是为了美观,而是为了说明第 5,6,7 行是函数的内容。相比下面 C++ 函数用花括号的表示方法,相信你可以在这个角度感受 Python 的简洁。 \n", "\n", @@ -682,23 +681,12 @@ " }\n", " \n", "下面我们尝试调用前面定义的 check_sum 函数完成之前的任务。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 66, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n", - "False\n", - "True\n" - ] - } - ], "source": [ "## 这里 2,3,13 是实参,函数返回结果 False\n", "\n", @@ -707,11 +695,22 @@ "print(check_sum(2, 7, 13))\n", "\n", "print(check_sum(2, 11, 13))" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "False\n", + "False\n", + "True\n" + ] + } + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "通过引入函数,上面的小例子变得更加简洁。然而我们发现无论测试哪两个数字的和与target一致,target的值是始终不变的。我们可以通过引入 局部变量 与 全局变量 简化函数。\n", "\n", @@ -721,17 +720,22 @@ "在 check_sum 中定义的变量 a, b, num1, num2, target 都属于局部变量。\n", "\n", "在 1.1.3.1 节中我们定义的 target 变量属于全局变量。当我们希望在函数内部使用全局变量时,应当用 global 关键字予以标注。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 67, - "metadata": {}, + "source": [ + "## 尝试在函数外部使用变量 num1,系统报错 num1 未定义\n", + "\n", + "num1" + ], "outputs": [ { + "output_type": "error", "ename": "NameError", "evalue": "name 'num1' is not defined", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", @@ -740,17 +744,11 @@ ] } ], - "source": [ - "## 尝试在函数外部使用变量 num1,系统报错 num1 未定义\n", - "\n", - "num1" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 68, - "metadata": {}, - "outputs": [], "source": [ "## 尝试重新定义函数 check_sum, 此时函数参数中已不存在target\n", "\n", @@ -759,16 +757,24 @@ " a = num1\n", " b = num2\n", " return a + b == target" - ] + ], + "outputs": [], + "metadata": {} }, { "cell_type": "code", "execution_count": 71, - "metadata": {}, + "source": [ + "print(check_sum(2, 3))\n", + "\n", + "print(check_sum(2, 7))\n", + "\n", + "print(check_sum(2, 11))" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "False\n", "False\n", @@ -776,31 +782,24 @@ ] } ], - "source": [ - "print(check_sum(2, 3))\n", - "\n", - "print(check_sum(2, 7))\n", - "\n", - "print(check_sum(2, 11))" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "通过 全局变量 的方法,我们的函数变得更加简洁了。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "## 1.1.4 Python 控制流" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "通过控制符与函数,我们可以操作变量完成简单的任务。然而本质上我们还在把 Python 当作一个计算器使用,而不是一个可以实现自动化的编程语言,每行代码按照自上而下的顺序依次执行。通过控制流,我们可以让程序自动判断逻辑,自动跳转到某个位置,从而实现自动控制。\n", "\n", @@ -813,11 +812,11 @@ "- continue 继续语句:暂停当前 while 或 for-in 循环,继续执行循环到下一个迭代。\n", "\n", "控制流就像语言中的“介词”,帮助我们联接名词与动词组成流畅优美的句子。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.4.1 while 循环语句\n", "\n", @@ -827,16 +826,24 @@ " statemnt\n", "\n", "若 \\*\\*\\* 的结果为 True 则继续重复执行 statement,若结果为 False 则停止循环。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 76, - "metadata": {}, + "source": [ + "## while 循环语句样例\n", + "\n", + "a = 0\n", + "while a < 5:\n", + " a = a + 1\n", + " print(a)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "1\n", "2\n", @@ -846,27 +853,19 @@ ] } ], - "source": [ - "## while 循环语句样例\n", - "\n", - "a = 0\n", - "while a < 5:\n", - " a = a + 1\n", - " print(a)" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "在上面的代码中,首先我们定义变量 a 的值为 0,之后每次迭代使 a 的值增加 1,并输出当前 a 的值。\n", "\n", "当 a 的值等于 5 时,循环停止。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.4.2 for 循环语句\n", "\n", @@ -882,16 +881,22 @@ "其中 \\*\\*\\* 是被迭代的序列,\\* 是存储当前迭代元素的变量,当 \\*\\*\\* 中所有元素都被迭代一次后,循环停止。\n", "\n", "在下面的代码中,每次循环迭代序列中的一个元素,直到最后一个元素 5 被迭代。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 77, - "metadata": {}, + "source": [ + "## for-in 循环语句样例\n", + "\n", + "for i in [1,2,3,4,5]:\n", + " print(i)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "1\n", "2\n", @@ -901,28 +906,30 @@ ] } ], - "source": [ - "## for-in 循环语句样例\n", - "\n", - "for i in [1,2,3,4,5]:\n", - " print(i)" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "考虑之前的求和问题,在最坏的情况下,我们需要把所有元素两两求和才能得到答案。在5个元素时,我们最多需要对比10次,也就是重复写10次 check_sum 函数。但在有100个元素时,我们需要重复写4950次!因此这时我们用循环进行简化:" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 81, - "metadata": {}, + "source": [ + "## 依次迭代元素 a,一共迭代 5 次\n", + "for a in [2,3,7,11,15]:\n", + " ## 依次迭代元素 b,一共迭代 5 次\n", + " for b in [2,3,7,11,15]:\n", + " ## 每个元素 a 与 5 个元素 b 依次两两尝试,一共迭代了 5 * 5 = 25 次\n", + " print(a, b, check_sum(a, b))" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "2 2 False\n", "2 3 False\n", @@ -952,27 +959,19 @@ ] } ], - "source": [ - "## 依次迭代元素 a,一共迭代 5 次\n", - "for a in [2,3,7,11,15]:\n", - " ## 依次迭代元素 b,一共迭代 5 次\n", - " for b in [2,3,7,11,15]:\n", - " ## 每个元素 a 与 5 个元素 b 依次两两尝试,一共迭代了 5 * 5 = 25 次\n", - " print(a, b, check_sum(a, b))" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "在这个例子中,我们可以看到循环是可以嵌套的,在循环的每一次迭代中开启一个新的循环。外层的 for a in [2,3,7,11,15] 按照顺序选择第 1 个元素,内层的 for b in [2,3,7,11,15] 按照顺序选择第 2 个元素,每次迭代输出两个值以及是否与 target 相等。\n", "\n", "最终我们找到输出 True 的两个元素 ( 2, 11 ),只要三行代码就可以解决问题!" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.4.3 if-else 逻辑语句\n", "\n", @@ -984,31 +983,21 @@ " statement2\n", "\n", "若 \\*\\*\\* 的结果为 True 则执行 statement1,若结果为 False 则执行 statement2" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "虽然通过 for-in 不需要写很长代码,然而我们还需要从输出结果中一个个找。通过 if-else 逻辑语句,我们可以让程序真正的实现自动化!\n", "\n", "这里 pass 代表不进行任何操作。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 85, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 11\n", - "11 2\n" - ] - } - ], "source": [ "## 加入 if-else 逻辑语句\n", "\n", @@ -1019,44 +1008,45 @@ " print(a, b)\n", " else:\n", " pass" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2 11\n", + "11 2\n" + ] + } + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "通过 if-else 逻辑语句,我们仅输出求和等于 target 的两个元素,不再需要从输出结果中逐个查找。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.4.4 break 停止语句" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "break 停止语句用于停止当前的循环。在上面的例子中,我们仅输出 1 种顺序即可,可以添加 break 停止语句在找到符合条件的两个元素后停止。\n", "\n", "注意 break 仅能跳出当前循环,因此我们需要添加一个 finded 变量记录是否已找到符合条件的两个元素,若找到后外层循环也使用 break 跳出。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 87, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 11\n" - ] - } - ], "source": [ "## 添加 break 停止语句\n", "\n", @@ -1074,30 +1064,46 @@ " ## 若 finded 为 True,停止外层循环\n", " if finded:\n", " break" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2 11\n" + ] + } + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.4.5 continue 继续语句" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "continue 语句用于停止当前循环并继续执行循环到下一个迭代,下面我们用一个例子展示 continue 的用法。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 89, - "metadata": {}, + "source": [ + "for a in [2,3,7,11,15]:\n", + " print(a)\n", + " continue\n", + " ## continue 使循环停止,并继续执行下一个迭代,后面的内容被跳过\n", + " print(a + 1)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "2\n", "3\n", @@ -1107,38 +1113,31 @@ ] } ], - "source": [ - "for a in [2,3,7,11,15]:\n", - " print(a)\n", - " continue\n", - " ## continue 使循环停止,并继续执行下一个迭代,后面的内容被跳过\n", - " print(a + 1)" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "## 1.1.5 练习" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "### 1.1.5.1 练习一" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "> 按规定,某种电子元件使用寿命超过 1000 小时为一级品。已知某一大批产品的一级品率为 0.2,现在从中随机地抽查 20 只。使用 Python 计算 20 只元件中恰好有 k 只 (k=0,1,...,20) 为一级品的概率为?" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "根据二项分布公式,所求的概率为:\n", "\n", @@ -1146,13 +1145,12 @@ "20 \\\\\n", "k\n", "\\end{array}\\right)(0.2)^{k}(0.8)^{20-k}, k=0,1, \\cdots, 20$$" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 108, - "metadata": {}, - "outputs": [], "source": [ "## 定义阶乘函数,用于排列组合中\n", "def multiple(x):\n", @@ -1161,13 +1159,13 @@ " result = result * x\n", " x = x - 1\n", " return result" - ] + ], + "outputs": [], + "metadata": {} }, { "cell_type": "code", "execution_count": 110, - "metadata": {}, - "outputs": [], "source": [ "## 定义二项分布计算函数\n", "def p_xk(k):\n", @@ -1177,16 +1175,24 @@ " ## 计算概率\n", " p = (0.2 ** k) * (0.8 ** (20 - k))\n", " return temp * p" - ] + ], + "outputs": [], + "metadata": {} }, { "cell_type": "code", "execution_count": 117, - "metadata": {}, + "source": [ + "## 根据二项分布计算概率\n", + "k = 0\n", + "while k != 21:\n", + " print('P{ X =',k,'} = ', p_xk(k))\n", + " k = k + 1" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "P{ X = 0 } = 0.011529215046068483\n", "P{ X = 1 } = 0.05764607523034242\n", @@ -1212,13 +1218,7 @@ ] } ], - "source": [ - "## 根据二项分布计算概率\n", - "k = 0\n", - "while k != 21:\n", - " print('P{ X =',k,'} = ', p_xk(k))\n", - " k = k + 1" - ] + "metadata": {} } ], "metadata": {