{ "cells": [ { "cell_type": "markdown", "id": "2a9bbd2c", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 聪明办法学 Python 2nd Edition\n", "## Chapter 1 启航 Getting Started\n", "---\n", "聪明办法学 Python 教学团队\n", "\n", "

learn.python.the.smart.way@gmail.com

" ] }, { "cell_type": "markdown", "id": "5c127a52", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 欢迎大家来到 P2S!\n", "\n", "迈出成为 AI 训练大师的第一步!\n", "\n", "![AI Master](../resources/slides/chap1/pic/chap1_1_AIMaster.jpg)" ] }, { "cell_type": "markdown", "id": "29684035", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 第一行代码\n", "Language C\n", "```c\n", "#include \n", "int main(){\n", " printf(\"Hello, World\");\n", " return 0;\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 3, "id": "9b187328", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:06:00.541312Z", "start_time": "2023-08-01T12:06:00.522313Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "聪明办法学Python\n" ] } ], "source": [ "print(\"聪明办法学Python\") " ] }, { "cell_type": "markdown", "id": "dd326c5d", "metadata": { "cell_style": "split", "slideshow": { "slide_type": "subslide" } }, "source": [ "# Hello World 的由来\n", "\n", "\"Brian\n", "\n", "```c\n", "main( ) {\n", " printf(\"hello, word\");\n", "}\n", "```" ] }, { "cell_type": "markdown", "id": "ed2f3d24", "metadata": { "cell_style": "split" }, "source": [ "Brian Wilson Kernighan, 1972\n", "\n", "*A Tutorial Introduction to the Language B*\n", "\n", "Language B Version:\n", "```B\n", "main( ) {\n", " extern a, b, c;\n", " putchar(a); putchar(b); putchar(c); putchar('!*n');\n", "}\n", " \n", "a 'hell';\n", "b 'o, w';\n", "c 'orld';\n", "```" ] }, { "cell_type": "markdown", "id": "98ab00f4", "metadata": { "ExecuteTime": { "end_time": "2022-10-07T15:24:26.944438Z", "start_time": "2022-10-07T15:24:26.925296Z" }, "slideshow": { "slide_type": "slide" } }, "source": [ "# 注释 Comment\n", "\n", "分类:\n", "- 单行注释,使用 `#` 开头\n", "- 多行注释,使用 `'''` 或 `\"\"\"` 包裹起来\n", "\n", "作用:\n", "- 注释主要是用于对代码进行**解释和说明**,可以提升代码的可读性\n", "- 注释并**不会被当做代码处理** # magic comment 除外\n", "\n", "> 程序员最讨厌的 10 件事:0. 别人的代码不写注释。 1. 给自己的代码写注释\n", "\n", "> 当初写这段代码的时候只有上帝和我知道它是干嘛的,现在只有上帝知道\n", "\n" ] }, { "cell_type": "markdown", "id": "07edb7b0", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "单行注释\n", "\n", "使用 `#` 开头,`#` 后面的内容不会被当做代码,只能写在一行中" ] }, { "cell_type": "code", "execution_count": 4, "id": "b2e587f6", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:17:35.632974Z", "start_time": "2023-08-01T12:17:35.628973Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Datawhale\n" ] } ], "source": [ "print(\"Datawhale\") # for the learner,和学习者一起成长 \n", "# output 输出, print 打印" ] }, { "cell_type": "code", "execution_count": 7, "id": "57fe02d3", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:20:20.770933Z", "start_time": "2023-08-01T12:20:20.758933Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p2s\n" ] } ], "source": [ "# learn python the smart way v2\n", "print(\"p2s\")\n", "# print(\"prepare to be smart\")" ] }, { "cell_type": "markdown", "id": "e32c85b4", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "多行注释\n", "\n", "使用 `'''` 或 `\"\"\"` 包裹起来(头和尾都是 3 个),**单引号(')与双引号(\")在 Python 中并无太大区别**" ] }, { "cell_type": "code", "execution_count": null, "id": "ea217736", "metadata": { "ExecuteTime": { "end_time": "2022-10-29T11:07:32.399815Z", "start_time": "2022-10-29T11:07:32.380815Z" }, "run_control": { "marked": false }, "scrolled": false }, "outputs": [], "source": [ "print(\"人生苦短,我用 Python\")\n", "'''\n", "Python is powerful... and fast;\n", "plays well with others;\n", "runs everywhere;\n", "is friendly & easy to learn;\n", "is Open.\n", "'''" ] }, { "cell_type": "markdown", "id": "1f66b079", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 基础的控制台输出 Basic Console Output" ] }, { "cell_type": "code", "execution_count": 9, "id": "3b45f07c", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:25:45.236203Z", "start_time": "2023-08-01T12:25:45.219685Z" }, "run_control": { "marked": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Datawhale\n" ] } ], "source": [ "print(\"Datawhale\") # for the learner,和学习者一起成长" ] }, { "cell_type": "markdown", "id": "0efdfb86", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "`print()` 的作用是将填入的内容显示在 Console 中,默认每次输入后会换行(等价于按了一次回车,或者 `\\n`)\n", "\n", "控制结尾的参数是 `end`" ] }, { "cell_type": "code", "execution_count": 10, "id": "b3b799e6", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:26:49.085898Z", "start_time": "2023-08-01T12:26:49.075379Z" }, "slideshow": { "slide_type": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data\n", "whale\n" ] } ], "source": [ "print(\"Data\")\n", "print(\"whale\")" ] }, { "cell_type": "code", "execution_count": 14, "id": "034f73ac", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:31:24.973652Z", "start_time": "2023-08-01T12:31:24.962653Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data*whale\n" ] } ], "source": [ "print(\"Data\", end=\"*\")\n", "print(\"whale\")" ] }, { "cell_type": "markdown", "id": "2814590d", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "`print()` 一次也可以输出多个内容,默认以空格分隔\n", "\n", "控制分隔的参数是 `sep`" ] }, { "cell_type": "code", "execution_count": 16, "id": "4b9f9952", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:32:36.527785Z", "start_time": "2023-08-01T12:32:36.522778Z" }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data whale\n" ] } ], "source": [ "print(\"Data\",\"whale\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "2081dc12", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:33:04.772658Z", "start_time": "2023-08-01T12:33:04.758660Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data*whale\n" ] } ], "source": [ "print(\"Data\", \"whale\", sep=\"*\")" ] }, { "cell_type": "markdown", "id": "227a467f", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "你甚至可以做**加法**和**乘法**!" ] }, { "cell_type": "code", "execution_count": 18, "id": "ebcf08a0", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:34:03.764234Z", "start_time": "2023-08-01T12:34:03.746227Z" }, "cell_style": "split", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p2sp2s////datadatadata\n" ] } ], "source": [ "print(\"p2s\"*2,\"data\"*3, sep=\"/\"*4)" ] }, { "cell_type": "code", "execution_count": 19, "id": "2d733e71", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:35:00.447015Z", "start_time": "2023-08-01T12:35:00.433016Z" }, "cell_style": "split", "scrolled": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DatawhaleP2S\n" ] } ], "source": [ "print(\"Data\"+\"whale\"+\"P2S\")" ] }, { "cell_type": "markdown", "id": "53f2133a", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 一些更好玩的做法\n", "\n", "我们假设," ] }, { "cell_type": "code", "execution_count": 21, "id": "e8c41160", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:37:20.558650Z", "start_time": "2023-08-01T12:37:20.549650Z" }, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "x = 1\n", "y = 2" ] }, { "cell_type": "code", "execution_count": 24, "id": "e778f892", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:38:20.497468Z", "start_time": "2023-08-01T12:38:20.478473Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "一个简单的数学问题:\"1 + 2 = ?\",答案是 3!\n" ] } ], "source": [ "print(f\"一个简单的数学问题:\\\"{x} + {y} = ?\\\",答案是 {x+y}!\") # f-strings" ] }, { "cell_type": "markdown", "id": "080d4b9b", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "为了课程演示,我们编写了下面这些代码,你并不需要弄清楚它具体内容是什么,享受游戏的过程就好了 ;-)" ] }, { "cell_type": "code", "execution_count": 25, "id": "a158be07", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:41:03.824406Z", "start_time": "2023-08-01T12:41:03.732883Z" }, "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "23e5157e31b4492f845e8057829941d8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=10, description='x', max=30, min=-10), IntSlider(value=20, description='…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from ipywidgets import interact\n", "import ipywidgets as widgets\n", "def f(x, y):\n", " print(f\"A simple math question: \\\"{x} + {y} = ?\\\", the answer is {x + y}!\")\n", "interact(f,x=10, y=20)" ] }, { "cell_type": "markdown", "id": "ad9a6841", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "如果我想**一次性输出很多行**?" ] }, { "cell_type": "code", "execution_count": 26, "id": "a9562933", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:44:06.600042Z", "start_time": "2023-08-01T12:44:06.584694Z" }, "run_control": { "marked": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Python is powerful... and fast;\n", "plays well with others;\n", "runs everywhere;\n", "is friendly & easy to learn;\n", "is Open.\n", "\n" ] } ], "source": [ "print(\"\"\"\n", "Python is powerful... and fast;\n", "plays well with others;\n", "runs everywhere;\n", "is friendly & easy to learn;\n", "is Open.\n", "\"\"\")" ] }, { "cell_type": "markdown", "id": "100469fa", "metadata": { "ExecuteTime": { "end_time": "2022-10-09T09:56:42.481166Z", "start_time": "2022-10-09T09:56:42.468288Z" }, "slideshow": { "slide_type": "subslide" } }, "source": [ "# 如何秒杀马里奥题\n", "\n", "[洛谷 P1000 超级玛丽游戏](https://www.luogu.com.cn/problem/P1000)\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "abdc08f7", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T11:53:00.223277Z", "start_time": "2023-08-01T11:53:00.207370Z" }, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame('https://www.luogu.com.cn/problem/P1000', width=1300, height=600)" ] }, { "cell_type": "markdown", "id": "433baaef", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 错误 Error\n", "- 语法错误 Syntax Errors,不符合语法规范,代码根本**没有开始运行**\n", "- “运行时”错误 Runtime Errors,代码在**运行过程中出错**,也就是常说的“崩溃”(Crash)\n", "- 逻辑错误 Logical Errors,代码能够运行,且运行过程中没有出错,但是**不是想要的结果**" ] }, { "cell_type": "code", "execution_count": 1, "id": "b245992c", "metadata": { "ExecuteTime": { "end_time": "2023-10-05T07:35:24.269818Z", "start_time": "2023-10-05T07:35:24.263877Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "unterminated string literal (detected at line 2) (3098959924.py, line 2)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[1], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m print(\"哦不!) # Error! 缺少结尾引号\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unterminated string literal (detected at line 2)\n" ] } ], "source": [ "# 语法错误(在编译时出错,Python 并没有开始运行代码)\n", "print(\"哦不!) # Error! 缺少结尾引号" ] }, { "cell_type": "code", "execution_count": 28, "id": "0f31e579", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:53:07.202812Z", "start_time": "2023-08-01T12:53:07.177670Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [28], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# “运行时”错误(Python 开始运行代码,但是遇到了些问题)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m)\n", "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "# “运行时”错误(Python 开始运行代码,但是遇到了些问题)\n", "print(1/0) # Error! 0 被作为除数" ] }, { "cell_type": "code", "execution_count": 29, "id": "308ae46e", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:54:59.412864Z", "start_time": "2023-08-01T12:54:59.398865Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2+2=5\n" ] } ], "source": [ "# Logical Errors (Compiles and runs, but is wrong!)\n", "# 逻辑错误(能编译,能运行,但不是想要的结果)\n", "print(\"2+2=5\") # Error! 算错了!!!\n", "# 我们想要:4!" ] }, { "cell_type": "markdown", "id": "977bb358", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 基础的控制台输入 Basic Console Input\n", "\n", "`input()` 可以接收 Console 的输入,并**以字符串的形式返回**,你可以给定个字符串参数,它会先输出到 Console,再接收输入" ] }, { "cell_type": "code", "execution_count": 31, "id": "c18d8f87", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T12:58:30.742431Z", "start_time": "2023-08-01T12:58:13.142527Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入你的名字:龙的传人\n", "あなたの名前は 龙的传人 です\n" ] } ], "source": [ "name = input(\"输入你的名字:\")\n", "print(\"あなたの名前は\", name, \"です\")" ] }, { "cell_type": "markdown", "id": "56608fe8", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 注意!返回的格式是字符串" ] }, { "cell_type": "code", "execution_count": 33, "id": "37d10260", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:01:39.378965Z", "start_time": "2023-08-01T13:01:37.411203Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入一个数字10\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for /: 'str' and 'int'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [33], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m输入一个数字\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(x, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m的一半等于\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43mx\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m)\n", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'int'" ] } ], "source": [ "x = input(\"输入一个数字\")\n", "print(x, \"的一半等于\", x/2) # Error!" ] }, { "cell_type": "code", "execution_count": 34, "id": "06f9303d", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:02:15.292524Z", "start_time": "2023-08-01T13:02:13.262597Z" }, "run_control": { "marked": false }, "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入一个数字10\n", "10 的一半等于 5.0\n" ] } ], "source": [ "x = input(\"输入一个数字\")\n", "x = int(x) # 类型转换 float(x)\n", "x = int(input(\"输入一个数字:\")) # f(g(x))\n", "print(x, \"的一半等于\", x/2) # 对味啦" ] }, { "cell_type": "markdown", "id": "99cb167e", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 一行多个输入值\n", "\n", "1 -> a,2 -> b\n", "\n", "可以在结尾加上 `split()`,默认分隔参数是空格,可以更改,如:`split(\",\")`" ] }, { "cell_type": "code", "execution_count": 38, "id": "418507a6", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:05:46.387622Z", "start_time": "2023-08-01T13:05:44.333972Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*2\n", "a = 1, b = 2\n" ] } ], "source": [ "a, b = input().split(\"*\")\n", "print(f\"a = {a}, b = {b}\")" ] }, { "cell_type": "markdown", "id": "185616e3", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 导入模块\n", "Python 中有许多强大的工具箱,我们把它们叫做**“库”(Library)**,课程后期会介绍更多强大的工具\n", "\n", "库需要使用 `import` 来导入,并且使用 `xx.yy`的方式来调用,我们今天只作粗略介绍" ] }, { "cell_type": "markdown", "id": "68f87dda", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "以 Python 内置数学库 `math` 为例:" ] }, { "cell_type": "code", "execution_count": 39, "id": "c75a0539", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:07:18.025310Z", "start_time": "2023-08-01T13:07:18.008310Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "ename": "NameError", "evalue": "name 'math' 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 [39], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 阶乘 factorial\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmath\u001b[49m\u001b[38;5;241m.\u001b[39mfactorial(\u001b[38;5;241m20\u001b[39m))\n", "\u001b[1;31mNameError\u001b[0m: name 'math' is not defined" ] } ], "source": [ "# 阶乘 factorial\n", "print(math.factorial(20))" ] }, { "cell_type": "code", "execution_count": 43, "id": "02ce973d", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:08:26.743036Z", "start_time": "2023-08-01T13:08:26.710031Z" }, "run_control": { "marked": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ValueError", "evalue": "Exceeds the limit (4300) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [43], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmath\u001b[39;00m \u001b[38;5;66;03m# 使用库前要先导入!\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfactorial\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m20000\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;31mValueError\u001b[0m: Exceeds the limit (4300) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit" ] } ], "source": [ "import math # 使用库前要先导入!\n", "print(math.factorial(20000))" ] }, { "cell_type": "code", "execution_count": 48, "id": "2794774d", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:09:36.887379Z", "start_time": "2023-08-01T13:09:36.878388Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.718281828459045\n" ] } ], "source": [ "# Euler 常数\n", "print(math.e)" ] }, { "cell_type": "code", "execution_count": 49, "id": "cf497dd5", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:10:06.054647Z", "start_time": "2023-08-01T13:10:06.034651Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# gcd 最大公约数\n", "math.gcd(12, 36)" ] }, { "cell_type": "code", "execution_count": 50, "id": "1d91622c", "metadata": { "ExecuteTime": { "end_time": "2023-08-01T13:10:15.575953Z", "start_time": "2023-08-01T13:10:15.550955Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a84925a6025541bc87fa2c7f526b0c0d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=30, description='x', max=90, min=-30), Output()), _dom_classes=('widget-…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x):\n", " a = x*(math.pi/180)\n", " print(math.sin(a))\n", "interact(f,x=30)" ] }, { "cell_type": "markdown", "id": "48832482", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "# 补充资料\n", "0.1+0.2≠0.3?? 无可避免的浮点误差:https://www.bilibili.com/video/BV1xq4y1D7Ep" ] }, { "cell_type": "markdown", "id": "0b79d003", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 总结\n", "\n", "- 写注释是个好习惯\n", "- 调整输入输出的参数来控制其呈现效果\n", "- 大部分错误类型可以归为:语法错误、运行时错误和逻辑错误\n", "- Python 的库能让很多操作变方便" ] }, { "cell_type": "markdown", "id": "a0bea848", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Thank You ;-)\n", "Datawhale 聪明办法学 Python 教学团队出品\n", "\n", "## 关注我们\n", "Datawhale 是一个专注 AI 领域的开源组织,以“for the learner,和学习者一起成长”为愿景,构建对学习者最有价值的开源学习社区。关注我们,一起学习成长。\n", "
" ] } ], "metadata": { "celltoolbar": "Slideshow", "gist": { "data": { "description": "slide/chapter_1-Getting_Started.ipynb", "public": false }, "id": "" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "rise": { "overlay": "
" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }