{ "cells": [ { "cell_type": "markdown", "id": "cf04d555", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 聪明办法学 Python 2nd Edition\n", "## Chapter 3 变量与函数 Variables and Functions\n", "---\n", "聪明办法学 Python 教学团队\n", "\n", "
learn.python.the.smart.way@gmail.com
" ] }, { "cell_type": "markdown", "id": "52eb108d", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 变量 Variables\n", "\n", "> A variable is a named value that references or stores a piece of data.\n", "\n", "- 变量是一个**名字**,它所指代的是一段数据\n", "- 使用 `=` 来对这段数据的区域进行**赋值**" ] }, { "cell_type": "code", "execution_count": 7, "id": "de02b297", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:24:31.038253Z", "start_time": "2023-08-12T12:24:31.026210Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "x = 5" ] }, { "cell_type": "code", "execution_count": 8, "id": "98d2e95d", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:24:32.338555Z", "start_time": "2023-08-12T12:24:32.318722Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 9, "id": "aeb8757d", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:24:35.503312Z", "start_time": "2023-08-12T12:24:35.484439Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "print(x*2)" ] }, { "cell_type": "markdown", "id": "50cb960e", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- 新的值会**覆盖**掉旧的值\n", "- 新值的数据类型不必与旧值相同" ] }, { "cell_type": "code", "execution_count": 10, "id": "8259c68b", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:26:26.571537Z", "start_time": "2023-08-12T12:26:26.558650Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "y = 10\n", "print(y - 2)" ] }, { "cell_type": "code", "execution_count": 11, "id": "9d7e214a", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:26:29.278538Z", "start_time": "2023-08-12T12:26:29.265519Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "y = True\n", "print(y)" ] }, { "cell_type": "markdown", "id": "6ef1a41e", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "变量命名规则:\n", "- **必须以字母或下划线(`_`)开头**\n", "- 命名可由字母、数字和下划线组成\n", "- **大小写敏感**\n", "- 尽量**避免使用保留字**命名" ] }, { "cell_type": "code", "execution_count": 12, "id": "455ea687", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:30:25.758511Z", "start_time": "2023-08-12T12:30:25.741457Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "numberOfRabbits = 40\n", "courseIs15112 = True" ] }, { "cell_type": "code", "execution_count": 13, "id": "1dd257e1", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:30:27.468546Z", "start_time": "2023-08-12T12:30:27.448622Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (1604835474.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn [13], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m 99problems = 0 # 会崩溃!因为变量名以数字开头\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "99problems = 0 # 会崩溃!因为变量名以数字开头" ] }, { "cell_type": "markdown", "id": "5bfbb648", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 保留字" ] }, { "cell_type": "code", "execution_count": 14, "id": "86b01766", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:30:29.195893Z", "start_time": "2023-08-12T12:30:29.174701Z" }, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['False',\n", " 'None',\n", " 'True',\n", " '__peg_parser__',\n", " 'and',\n", " 'as',\n", " 'assert',\n", " 'async',\n", " 'await',\n", " 'break',\n", " 'class',\n", " 'continue',\n", " 'def',\n", " 'del',\n", " 'elif',\n", " 'else',\n", " 'except',\n", " 'finally',\n", " 'for',\n", " 'from',\n", " 'global',\n", " 'if',\n", " 'import',\n", " 'in',\n", " 'is',\n", " 'lambda',\n", " 'nonlocal',\n", " 'not',\n", " 'or',\n", " 'pass',\n", " 'raise',\n", " 'return',\n", " 'try',\n", " 'while',\n", " 'with',\n", " 'yield']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import keyword\n", "keyword.kwlist" ] }, { "cell_type": "markdown", "id": "5b2a4a68", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 更新变量" ] }, { "cell_type": "code", "execution_count": 15, "id": "f731b4e4", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:30:35.293640Z", "start_time": "2023-08-12T12:30:35.288602Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "x = 5\n", "x += 2 # 等价于 x = x + 2\n", "print(x) # 7" ] }, { "cell_type": "code", "execution_count": 16, "id": "7a27cf1d", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:30:38.296374Z", "start_time": "2023-08-12T12:30:38.284352Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "35\n" ] } ], "source": [ "# 换成其他运算符也是同理\n", "y = 350\n", "y //= 10\n", "print(y) # 35" ] }, { "cell_type": "markdown", "id": "5e5231c6", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 多变量赋值" ] }, { "cell_type": "code", "execution_count": 17, "id": "a50d96fe", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:34:11.986967Z", "start_time": "2023-08-12T12:34:11.973246Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=2, b=2, c=2\n" ] } ], "source": [ "a = b = c = 2\n", "print(f\"a={a}, b={b}, c={c}\")" ] }, { "cell_type": "code", "execution_count": 18, "id": "7f38bbc0", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:34:23.383144Z", "start_time": "2023-08-12T12:34:23.373363Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=2, c=6\n" ] } ], "source": [ "a, b, c = 1, 2, 6\n", "print(f\"a={a}, b={b}, c={c}\")" ] }, { "cell_type": "markdown", "id": "e4e14cb5", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 函数 Functions\n", "\n", "> A function is a procedure (a sequence of statements) stored under a name that can be used repeatedly by calling the name.\n", "\n", "- 函数是一个名字,代表一串代码序列(流程、过程)\n", "- 函数由两个部分组成:**header** 和 **body**\n", " - `header` 用于定义函数接口(函数 **名称** 与 **参数**)\n", " - `body` 包含函数所需要执行的操作" ] }, { "cell_type": "markdown", "id": "93c8a4be", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## header\n", "\n", "`header` 用于定义函数的**名称**和**参数**\n", "\n", "- 当函数被**调用**时,参数将会作为变量被提供给函数的 `body` 部分\n", "- 可以提供多个参数(用逗号 `,` 分隔),也可以不提供参数(0 个)\n", "- `header` 以冒号(`:`)结尾,代表后面会跟着 `body` 部分\n", "\n", "函数的 `header` 的写法:" ] }, { "cell_type": "code", "execution_count": 19, "id": "30bbf674", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:44:35.531373Z", "start_time": "2023-08-12T12:44:35.501507Z" } }, "outputs": [], "source": [ "def functionName(parameters):\n", " pass # 函数的 body 部分,这里使用 pass 代替" ] }, { "cell_type": "markdown", "id": "82652b0a", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## body\n", "\n", "`body` 包含函数执行的语句(`statement`)\n", "\n", "- 语句需要**缩进**(由 Code Style Guide 决定)\n", "- 当语句**不再缩进,函数部分结束**\n", "- 一般会使用 `return` 语句,来让函数返回其结果,但不是必须的\n", "\n", "> 类似于用一个 `=` 来对多个变量赋值,函数的返回结果也可以不止一个(用逗号 `,` 分隔)" ] }, { "cell_type": "markdown", "id": "3b4ff1a6", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "下面我们用一个例子来解释函数的细节" ] }, { "cell_type": "code", "execution_count": 21, "id": "005e7401", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:45:31.276515Z", "start_time": "2023-08-12T12:45:31.263935Z" } }, "outputs": [], "source": [ "def double(x):\n", " print(\"我在一个名叫 “double” 函数里!\")\n", " return 2 * x" ] }, { "cell_type": "markdown", "id": "bd4ccc98", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- 我们使用**函数名**来调用函数\n", "- 函数名后紧跟**一对括号**\n", "- 括号中是我们设定的参数的**值**,一个不多,一个不少(这很重要)\n", "- 函数会**返回**设定的 `return` 语句的值\n", "\n", "> 调用示例函数 `double()` 会返回一个值(`2 * x`)" ] }, { "cell_type": "code", "execution_count": 23, "id": "61d0b8a0", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:45:42.899338Z", "start_time": "2023-08-12T12:45:42.886808Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我在一个名叫 “double” 函数里!\n", "4\n" ] } ], "source": [ "print(double(2)) # 会输出 4" ] }, { "cell_type": "code", "execution_count": 25, "id": "fe3fcc9f", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:46:20.335526Z", "start_time": "2023-08-12T12:46:20.319819Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我在一个名叫 “double” 函数里!\n", "10\n" ] } ], "source": [ "print(double(5)) # 会输出 10" ] }, { "cell_type": "code", "execution_count": 26, "id": "76e530c1", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:46:30.467380Z", "start_time": "2023-08-12T12:46:30.452758Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我在一个名叫 “double” 函数里!\n", "5\n" ] } ], "source": [ "print(double(1) + 3) # 会输出 5" ] }, { "cell_type": "markdown", "id": "7e18a170", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "函数可以有任意多个参数,也可以一个都没有" ] }, { "cell_type": "code", "execution_count": 27, "id": "feda057f", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:46:40.828257Z", "start_time": "2023-08-12T12:46:40.815199Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "# 三个参数\n", "def f(x, y, z):\n", " return x + y + z\n", "\n", "print(f(1, 3, 2)) # 返回 6" ] }, { "cell_type": "code", "execution_count": 28, "id": "5b3ec990", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:46:59.660415Z", "start_time": "2023-08-12T12:46:59.639326Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "# 无参数\n", "def g():\n", " return 42\n", "\n", "print(g()) # 返回 42" ] }, { "cell_type": "markdown", "id": "28cf3e87", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "可是如果参数**数目没有匹配**的话……Oops!" ] }, { "cell_type": "code", "execution_count": 29, "id": "53f6e955", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:09.276277Z", "start_time": "2023-08-12T12:47:08.551650Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "g() takes 0 positional arguments but 1 was given", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [29], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mg\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m)\n", "\u001b[1;31mTypeError\u001b[0m: g() takes 0 positional arguments but 1 was given" ] } ], "source": [ "print(g(2)) # 崩溃!" ] }, { "cell_type": "code", "execution_count": 30, "id": "c19b7603", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:17.250728Z", "start_time": "2023-08-12T12:47:17.224050Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "f() missing 1 required positional argument: 'z'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [30], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m)\n", "\u001b[1;31mTypeError\u001b[0m: f() missing 1 required positional argument: 'z'" ] } ], "source": [ "print(f(1, 2)) # 也会崩溃" ] }, { "cell_type": "markdown", "id": "e8da06ea", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 多返回值" ] }, { "cell_type": "code", "execution_count": 31, "id": "6910224a", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:26.396779Z", "start_time": "2023-08-12T12:47:26.381658Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def Multi_Return_Values():\n", " return 9, 2, 8" ] }, { "cell_type": "code", "execution_count": 32, "id": "1ad91cc5", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:27.985584Z", "start_time": "2023-08-12T12:47:27.967961Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=9, b=2, c=8\n" ] } ], "source": [ "a, b, c = Multi_Return_Values()\n", "print(f\"a={a}, b={b}, c={c}\")" ] }, { "cell_type": "markdown", "id": "28486716", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 语句与表达式 Statements and Expressions" ] }, { "cell_type": "markdown", "id": "d151b9a8", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "> An expression is a data value or an operation that evaluates to a value.\n", "\n", "对于表达式\n", "- 它本身是**值**\n", "- 它的**计算结果是值**" ] }, { "cell_type": "markdown", "id": "d72477ee", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "> Statements, by contrast, do not evaluate to a value, and we can't print them. Usually they perform some action, though.\n", "\n", "对于语句\n", "- 它不是值\n", "- 它不能打印\n", "- 但它能**执行一些操作**" ] }, { "cell_type": "markdown", "id": "bdfbc819", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "表达式的一些例子" ] }, { "cell_type": "code", "execution_count": 33, "id": "62aa19ae", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:42.101736Z", "start_time": "2023-08-12T12:47:42.080465Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4" ] }, { "cell_type": "code", "execution_count": 34, "id": "1dc1cad6", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:43.657970Z", "start_time": "2023-08-12T12:47:43.643405Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\"" ] }, { "cell_type": "code", "execution_count": 35, "id": "2db65c3b", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:44.966446Z", "start_time": "2023-08-12T12:47:44.952852Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 + 2" ] }, { "cell_type": "code", "execution_count": 36, "id": "1d854d2c", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:46.407892Z", "start_time": "2023-08-12T12:47:46.396211Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 37, "id": "950da285", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:47.921853Z", "start_time": "2023-08-12T12:47:47.913310Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 < 3) and (9 > 0)" ] }, { "cell_type": "markdown", "id": "a3d987c0", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Python 只能 print 值和表达式,如果你能用 `print()` 输出它,那它就是表达式" ] }, { "cell_type": "code", "execution_count": 38, "id": "718d4ce6", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:52.264718Z", "start_time": "2023-08-12T12:47:52.246775Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print((2 < 3) and (9 > 0))" ] }, { "cell_type": "markdown", "id": "70f51fd9", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "语句的一些例子" ] }, { "cell_type": "code", "execution_count": 39, "id": "59ed7d68", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:55.208041Z", "start_time": "2023-08-12T12:47:55.199000Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def f(x):\n", " return 5*x" ] }, { "cell_type": "code", "execution_count": 40, "id": "5c963056", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:47:57.090665Z", "start_time": "2023-08-12T12:47:57.078499Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 5 + 4" ] }, { "cell_type": "code", "execution_count": 42, "id": "cc3380e7", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:48:14.819663Z", "start_time": "2023-08-12T12:48:14.811075Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "if 10 > 5:\n", " y = 5 + 3" ] }, { "cell_type": "markdown", "id": "06873939", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 内置函数 Builtin Functions" ] }, { "cell_type": "markdown", "id": "31a03d25", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "就是 Python 自己带的函数啦🤗" ] }, { "cell_type": "markdown", "id": "6dbb9d07", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 类型转换" ] }, { "cell_type": "code", "execution_count": 43, "id": "6065476a", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:49:28.688072Z", "start_time": "2023-08-12T12:49:28.667738Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(bool(0)) # 转换为布尔类型(True or False)" ] }, { "cell_type": "code", "execution_count": 44, "id": "6cdb3bed", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:49:50.183255Z", "start_time": "2023-08-12T12:49:50.165610Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42.0\n" ] } ], "source": [ "print(float(42)) # 转换为浮点数" ] }, { "cell_type": "code", "execution_count": 45, "id": "d72f4e0f", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:50:14.256834Z", "start_time": "2023-08-12T12:50:14.239768Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(int(2.8)) # 转换为一个整数(舍弃小数点)" ] }, { "cell_type": "markdown", "id": "533940d3", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 一些基本数学函数\n", "但是它们不在 `math` 库中" ] }, { "cell_type": "code", "execution_count": 46, "id": "dae12973", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:50:22.364372Z", "start_time": "2023-08-12T12:50:22.348349Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(abs(-5)) # 绝对值" ] }, { "cell_type": "code", "execution_count": 47, "id": "0a9a57a4", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:50:32.682408Z", "start_time": "2023-08-12T12:50:32.664262Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "print(max(2,3)) # 返回最大值" ] }, { "cell_type": "code", "execution_count": 48, "id": "144c8585", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:50:40.060884Z", "start_time": "2023-08-12T12:50:40.041638Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(min(2,3)) # 返回最小值" ] }, { "cell_type": "code", "execution_count": 49, "id": "68df2a1a", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:51:33.055856Z", "start_time": "2023-08-12T12:51:33.049816Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1024\n" ] } ], "source": [ "print(pow(2,10)) # 次方运算,等价于 2**10" ] }, { "cell_type": "code", "execution_count": 50, "id": "6a7bb062", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T12:52:24.164161Z", "start_time": "2023-08-12T12:52:24.142042Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.35\n" ] } ], "source": [ "print(round(2.354, 2)) # 取最近的一个整数(并不完全是四舍五入,二进制精度丢失)" ] }, { "cell_type": "markdown", "id": "76250104", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 变量作用域 Variable Scope\n", "- 每个变量都有属于自己的作用范围\n", "- 超出作用范围后,变量不可见" ] }, { "cell_type": "markdown", "id": "379c51e9", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "我们设定一个函数 `f(x)`, 它的内部有 `x` 和 `y` 两个变量\n", "\n", "> 记得一定要重启 Jupyter Kernel!" ] }, { "cell_type": "code", "execution_count": 5, "id": "380c348c", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T13:02:03.373827Z", "start_time": "2023-08-12T13:02:03.357640Z" } }, "outputs": [], "source": [ "def f(x):\n", " print(\"x:\", x)\n", " y = 5\n", " print(\"y:\", y)\n", " return x + y" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba9e76fc", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T13:00:27.877293Z", "start_time": "2023-08-12T13:00:27.861846Z" }, "run_control": { "marked": false }, "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: 4\n", "y: 5\n", "9\n" ] } ], "source": [ "print(f(4))" ] }, { "cell_type": "code", "execution_count": 3, "id": "b8157e63", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T13:00:31.314737Z", "start_time": "2023-08-12T13:00:31.282789Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mx\u001b[49m)\n", "\u001b[1;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "print(x) # crash!" ] }, { "cell_type": "code", "execution_count": 4, "id": "ddcc02e5", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T13:00:33.400337Z", "start_time": "2023-08-12T13:00:33.378191Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "NameError", "evalue": "name 'y' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [4], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43my\u001b[49m)\n", "\u001b[1;31mNameError\u001b[0m: name 'y' is not defined" ] } ], "source": [ "print(y) # crash!" ] }, { "cell_type": "markdown", "id": "649135bb", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "函数内的变量具有**局部作用域**,它只存在于函数内部,与其他函数中的同名变量无关" ] }, { "cell_type": "code", "execution_count": 10, "id": "e823d1e0", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T13:03:57.978021Z", "start_time": "2023-08-12T13:03:57.967044Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In f, x = 4\n", "In g, x = 2\n", "In f, x = 6\n", "In g, x = 2\n", "20\n" ] } ], "source": [ "def f(x):\n", " print(\"In f, x =\", x)\n", " x += 5\n", " return x\n", "\n", "def g(x):\n", " y = f(x*2)\n", " print(\"In g, x =\", x)\n", " z = f(x*3)\n", " print(\"In g, x =\", x)\n", " return y + z\n", "\n", "print(g(2))" ] }, { "cell_type": "code", "execution_count": 1, "id": "f2f847ac", "metadata": { "ExecuteTime": { "end_time": "2023-08-12T13:04:02.813410Z", "start_time": "2023-08-12T13:04:02.790320Z" }, "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "