{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "13ba9829",
   "metadata": {},
   "source": [
    "# 第 1 讲｜Python 编程基础：代码规范、变量与输入输出\n",
    "\n",
    "**对象：** Python 零基础学生  \n",
    "**课堂目标：** 从代码格式走到变量、类型、输入输出，再完成购物小票与植树证书两个小作品。  \n",
    "**参考：** 《Python 快速编程入门（第 3 版）》第 2.1-2.4 节。\n",
    "\n",
    "> 学习主线：让解释器读懂代码 → 让名称准确指向数据 → 让输入变成可用值 → 让输出可检查。\n",
    "\n",
    "\n",
    "> **课堂节奏：** 每个知识点先运行“课堂演示（样本代码）”，再由学生在紧随其后的“课堂练习（学生填写）”中补全答案。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b4a1b24",
   "metadata": {},
   "source": [
    "## 1. 先诊断：下面的程序为什么不可靠？\n",
    "\n",
    "    price = \"19.9\"\n",
    "    quantity = 3\n",
    "    print(\"销售额：\", price * quantity)\n",
    "\n",
    "代码不会报错，但会得到字符串重复。程序能运行不等于结果正确。\n",
    "\n",
    "本节第一次使用两个工具：\n",
    "\n",
    "- float(文本)：把符合格式的数字文本转换为小数，例如 float(\"19.9\") 得到 19.9。\n",
    "- type(值)：查看一个值的类型，例如 type(\"19.9\") 会显示它是字符串。\n",
    "\n",
    "今天每一步都要同时问：格式是否符合语法、名称是否表达含义、值的类型是否能完成运算、输出是否足以检查结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f6024f8",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "555da981",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.145510Z",
     "iopub.status.busy": "2026-07-22T11:56:51.145304Z",
     "iopub.status.idle": "2026-07-22T11:56:51.154972Z",
     "shell.execute_reply": "2026-07-22T11:56:51.154430Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始结果： 19.919.919.9\n"
     ]
    }
   ],
   "source": [
    "price = \"19.9\"\n",
    "quantity = 3\n",
    "print(\"原始结果：\", price * quantity)\n",
    "# 修正\n",
    "# print(\"修正结果：\", float(price) * quantity)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bf1f7799",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.7"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(\"4.7\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d583baa6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.157414Z",
     "iopub.status.busy": "2026-07-22T11:56:51.157213Z",
     "iopub.status.idle": "2026-07-22T11:56:51.162179Z",
     "shell.execute_reply": "2026-07-22T11:56:51.161794Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"a\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fe92a1a",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "242633cd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.164260Z",
     "iopub.status.busy": "2026-07-22T11:56:51.164082Z",
     "iopub.status.idle": "2026-07-22T11:56:51.166103Z",
     "shell.execute_reply": "2026-07-22T11:56:51.165675Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：把单价文本转换为数值，计算 4 件商品的销售额。\n",
    "unit_price_text = \"29.9\"\n",
    "print(type(unit_price_text))\n",
    "quantity = 4\n",
    "unit_price =   float(unit_price_text)           # 请填写转换代码\n",
    "total =   quantity*      unit_price          # 请填写计算代码\n",
    "print(\"练习销售额：\", total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe1bc5bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "quantity = 4\n",
    "type(quantity)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aadf0d02",
   "metadata": {},
   "source": [
    "## 2. 注释：写为什么，不复述做了什么\n",
    "\n",
    "# 后面的内容不执行。好的注释解释业务口径、边界或特殊原因。\n",
    "\n",
    "本节还会用到函数。函数是把一段可重复使用的代码取一个名字：\n",
    "\n",
    "- def 函数名(参数)：定义函数；\n",
    "- 参数是调用函数时传入的数据；\n",
    "- return 结果：把计算结果交回给调用处。\n",
    "\n",
    "三引号字符串常用于函数的说明文档；它本质上是字符串，不应把所有三引号内容都简单理解为注释。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7f0a153",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecb61f9f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.168630Z",
     "iopub.status.busy": "2026-07-22T11:56:51.168443Z",
     "iopub.status.idle": "2026-07-22T11:56:51.171631Z",
     "shell.execute_reply": "2026-07-22T11:56:51.171140Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "净销售数量： 117\n"
     ]
    }
   ],
   "source": [
    "# 函数：计算净销售数量\n",
    "def net_quantity(sales_quantity, return_quantity):\n",
    "    \"\"\"\n",
    "    计算扣除退单后的净销售数量。\n",
    "    \"\"\"\n",
    "    return sales_quantity - return_quantity\n",
    "\n",
    "\n",
    "print(\"净销售数量：\", net_quantity(125, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "884ec3e7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.173650Z",
     "iopub.status.busy": "2026-07-22T11:56:51.173507Z",
     "iopub.status.idle": "2026-07-22T11:56:51.177198Z",
     "shell.execute_reply": "2026-07-22T11:56:51.176790Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "117"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 125\n",
    "r = 8\n",
    "\n",
    "net_quantity(s, r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9df86352",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b6429162",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.179038Z",
     "iopub.status.busy": "2026-07-22T11:56:51.178834Z",
     "iopub.status.idle": "2026-07-22T11:56:51.180982Z",
     "shell.execute_reply": "2026-07-22T11:56:51.180616Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：为下列业务规则写一条解释“为什么”的注释。\n",
    "# return_quantity = 2\n",
    "# sales_quantity = 10\n",
    "# 请在下一行写注释：为什么要扣除退单？\n",
    "#\n",
    "# net_quantity = sales_quantity - return_quantity\n",
    "# print(\"净销售数量：\", net_quantity)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7aacb732",
   "metadata": {},
   "source": [
    "## 3. 缩进：空格在 Python 中表示代码层级\n",
    "\n",
    "Python 通常使用 4 个空格作为一级缩进。同一代码块必须对齐，不混用 Tab 与空格。\n",
    "\n",
    "本节借助已经介绍过的函数观察缩进：函数名后的冒号表示代码块开始；向右缩进的两行都属于函数。\n",
    "\n",
    "错误示例不在课堂中运行。看到 IndentationError 时，先检查同一代码块的空格数量是否一致。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb2ca2c4",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7756d7e0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.183090Z",
     "iopub.status.busy": "2026-07-22T11:56:51.182890Z",
     "iopub.status.idle": "2026-07-22T11:56:51.185321Z",
     "shell.execute_reply": "2026-07-22T11:56:51.184879Z"
    }
   },
   "outputs": [],
   "source": [
    "quantity = 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c6b2eef8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.186927Z",
     "iopub.status.busy": "2026-07-22T11:56:51.186779Z",
     "iopub.status.idle": "2026-07-22T11:56:51.189256Z",
     "shell.execute_reply": "2026-07-22T11:56:51.188863Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前库存： 4\n",
      "可以继续计算销售额\n"
     ]
    }
   ],
   "source": [
    "def show_stock(quantity):\n",
    "    print(\"当前库存：\", quantity)\n",
    "    print(\"可以继续计算销售额\")\n",
    "\n",
    "\n",
    "show_stock(quantity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d6521ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "show_stock = func()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "612282a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 做一个乘法计算器，两个参数， a 和b， 通过这个函数，计算 两个数相乘的结果\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81820921",
   "metadata": {},
   "source": [
    "课堂提示：缩进不一致会触发 IndentationError。修复时不要盲目删空格，\n",
    "而要先判断每一行属于哪一个代码块，再统一使用 4 个空格。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b8ffe92",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5477e2f0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.191329Z",
     "iopub.status.busy": "2026-07-22T11:56:51.191179Z",
     "iopub.status.idle": "2026-07-22T11:56:51.193535Z",
     "shell.execute_reply": "2026-07-22T11:56:51.193171Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：补全 if 语句的缩进，使库存大于 0 时输出“可以发货”。\n",
    "stock = 5\n",
    "if stock > 0:\n",
    "    # 请填写输出语句\n",
    "    #"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3910641",
   "metadata": {},
   "source": [
    "## 4. 语句换行：优先使用括号\n",
    "\n",
    "一行过长时，推荐在小括号、中括号或大括号内部自然换行。这样比反斜杠更不容易因行尾空格出错。\n",
    "\n",
    "```python\n",
    "is_target = (\n",
    "    sales_quantity >= 100\n",
    "    and unit_price < 200\n",
    "    and return_quantity <= 10\n",
    ")\n",
    "```\n",
    "\n",
    "换行后要让同一层级对齐，使条件结构一眼可见。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60382187",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ab2bbc82",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.195200Z",
     "iopub.status.busy": "2026-07-22T11:56:51.195068Z",
     "iopub.status.idle": "2026-07-22T11:56:51.197870Z",
     "shell.execute_reply": "2026-07-22T11:56:51.197429Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否为目标商品： True\n"
     ]
    }
   ],
   "source": [
    "sales_quantity = 125\n",
    "unit_price = 199.0\n",
    "return_quantity = 8\n",
    "\n",
    "is_target = (\n",
    "    sales_quantity >= 100\n",
    "    and unit_price < 200\n",
    "    and return_quantity <= 10\n",
    ")\n",
    "print(\"是否为目标商品：\", is_target)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f69981e",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbfe78c2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.199706Z",
     "iopub.status.busy": "2026-07-22T11:56:51.199539Z",
     "iopub.status.idle": "2026-07-22T11:56:51.201572Z",
     "shell.execute_reply": "2026-07-22T11:56:51.201224Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：使用小括号把多条件判断分成多行。\n",
    "sales_quantity = 120\n",
    "unit_price = 99.0\n",
    "return_quantity = 3\n",
    "is_target = (\n",
    "    # 请填写第一个条件\n",
    "    # 请填写后续条件\n",
    ")\n",
    "print(\"是否目标商品：\", is_target)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bae67d06",
   "metadata": {},
   "source": [
    "## 5. 标识符：先合法，再清楚\n",
    "\n",
    "合法名称只能由字母、数字、下划线组成，不能以数字开头，不能使用关键字，并区分大小写。\n",
    "\n",
    "| 名称 | 判断 | 原因 |\n",
    "|---|---|---|\n",
    "| `sales_quantity` | 推荐 | 合法且含义明确 |\n",
    "| `2price` | 非法 | 以数字开头 |\n",
    "| `class` | 非法 | Python 关键字 |\n",
    "| `Sales` / `sales` | 都合法但不同 | 区分大小写 |\n",
    "\n",
    "变量和函数通常使用 `snake_case`；常量约定使用全大写。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ca6dcab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sales = 100\n",
    "Sales = 110"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "84aa46ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 110\n"
     ]
    }
   ],
   "source": [
    "print(sales, Sales)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6fa0cc4",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0f2d48ae",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.203218Z",
     "iopub.status.busy": "2026-07-22T11:56:51.203086Z",
     "iopub.status.idle": "2026-07-22T11:56:51.205279Z",
     "shell.execute_reply": "2026-07-22T11:56:51.204883Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sales_quantity：合法且含义清楚\n",
      "2price：非法，不能以数字开头\n",
      "class：非法，不能使用关键字\n",
      "Sales 与 sales：都合法，但代表两个不同名称\n"
     ]
    }
   ],
   "source": [
    "print(\"sales_quantity：合法且含义清楚\")\n",
    "print(\"2price：非法，不能以数字开头\")\n",
    "print(\"class：非法，不能使用关键字\")\n",
    "print(\"Sales 与 sales：都合法，但代表两个不同名称\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaa496ad",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9d64f078",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.207053Z",
     "iopub.status.busy": "2026-07-22T11:56:51.206863Z",
     "iopub.status.idle": "2026-07-22T11:56:51.208794Z",
     "shell.execute_reply": "2026-07-22T11:56:51.208355Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：为“本月净销售额”设计一个合法、清楚的 snake_case 名称。\n",
    "# monthly_net_sales =        # 请填写一个数值\n",
    "# print(monthly_net_sales)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98b06813",
   "metadata": {},
   "source": [
    "## 6. 变量：名称在当前时刻指向一个值\n",
    "\n",
    "```python\n",
    "quantity = 125\n",
    "quantity = 130\n",
    "```\n",
    "\n",
    "`=` 是赋值；`==` 才是比较是否相等。Python 是动态类型语言，名称可以改指向另一种类型，但业务代码应尽量保持同一变量含义与类型稳定。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ea79162",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7bd0fe71",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.210308Z",
     "iopub.status.busy": "2026-07-22T11:56:51.210180Z",
     "iopub.status.idle": "2026-07-22T11:56:51.212644Z",
     "shell.execute_reply": "2026-07-22T11:56:51.212211Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "125 <class 'int'>\n",
      "130 <class 'int'>\n",
      "130 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "quantity = 125\n",
    "print(quantity, type(quantity))\n",
    "\n",
    "quantity = 130\n",
    "print(quantity, type(quantity))\n",
    "\n",
    "quantity_text = \"130\"\n",
    "print(quantity_text, type(quantity_text))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c058047",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3c4bc8ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.214300Z",
     "iopub.status.busy": "2026-07-22T11:56:51.214150Z",
     "iopub.status.idle": "2026-07-22T11:56:51.215974Z",
     "shell.execute_reply": "2026-07-22T11:56:51.215687Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前库存： 112\n"
     ]
    }
   ],
   "source": [
    "# 目标：模拟库存先入库 20 件、再出库 8 件。\n",
    "stock = 100\n",
    "stock = stock +20                # 请填写入库后的赋值\n",
    "stock =  stock -8                # 请填写出库后的赋值\n",
    "print(\"当前库存：\", stock)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1f7ad7d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock= 100\n",
    "stock = stock + 20\n",
    "stock"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e3d437d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock= 100\n",
    "stock += 20\n",
    "stock"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8e7e171",
   "metadata": {},
   "source": [
    "## 7. 先认识六类常见数据\n",
    "\n",
    "| 类型 | 示例 | 典型业务含义 |\n",
    "|---|---|---|\n",
    "| `int` | `125` | 数量、件数 |\n",
    "| `float` | `199.0` | 价格、比例 |\n",
    "| `str` | `\"双肩包\"` | 商品名、店铺名 |\n",
    "| `bool` | `True` | 条件是否成立 |\n",
    "| `list` | `[19.9, 29.9]` | 一组有序数据 |\n",
    "| `dict` | `{\"name\": \"双肩包\"}` | 带字段名的一条记录 |\n",
    "\n",
    "本节先会辨认；列表和字典的深入操作在后续章节学习。\n",
    "\n",
    "\n",
    "本节继续使用已经介绍过的 type(值) 观察类型；它只显示类型，不改变原来的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6092119c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'苹果'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruit = [\"苹果\",\"西瓜\"]\n",
    "\n",
    "fruit[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b4437a6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['奥迪', '宝马']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars= {\"电车\":[\"特斯拉\",\"小米\"], \"油车\":[\"奥迪\",\"宝马\"]}\n",
    "\n",
    "cars[\"油车\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6997933",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b83eb7a6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.218034Z",
     "iopub.status.busy": "2026-07-22T11:56:51.217890Z",
     "iopub.status.idle": "2026-07-22T11:56:51.220705Z",
     "shell.execute_reply": "2026-07-22T11:56:51.220394Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "125 -> <class 'int'>\n",
      "199.0 -> <class 'float'>\n",
      "双肩包 -> <class 'str'>\n",
      "True -> <class 'bool'>\n",
      "[19.9, 29.9] -> <class 'list'>\n",
      "{'name': '双肩包', 'price': 199.0} -> <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(125, \"->\", type(125))\n",
    "print(199.0, \"->\", type(199.0))\n",
    "print(\"双肩包\", \"->\", type(\"双肩包\"))\n",
    "print(True, \"->\", type(True))\n",
    "print([19.9, 29.9], \"->\", type([19.9, 29.9]))\n",
    "print({\"name\": \"双肩包\", \"price\": 199.0}, \"->\", type({\"name\": \"双肩包\"}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d579fae",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4a4f2ffe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.222327Z",
     "iopub.status.busy": "2026-07-22T11:56:51.222189Z",
     "iopub.status.idle": "2026-07-22T11:56:51.224009Z",
     "shell.execute_reply": "2026-07-22T11:56:51.223718Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 目标：创建一个商品字典，并输出它的类型。\n",
    "product = {\n",
    "    \"name\":   \"蓝牙耳机\"   ,        # 请填写商品名\n",
    "    \"price\":    500   ,      # 请填写价格\n",
    "    \"brand\": \"xiaomi\"\n",
    "}\n",
    "print(type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ba57853",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = {\"name\":\"蓝牙耳机\",\n",
    "           \"price\":500,\n",
    "           \"brand\": \"xiaomi\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "6eef67b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '蓝牙耳机', 'price': 500, 'brand': 'xiaomi'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9500f997",
   "metadata": {},
   "source": [
    "## 8. input() 总是返回字符串\n",
    "\n",
    "input(\"提示文字\") 会暂停程序，等待用户从键盘输入；它返回的一定是字符串。\n",
    "需要计算时，用前面介绍过的 int(文本) 或 float(文本) 转换。Notebook 为了可重复执行，\n",
    "下面用预设文本模拟一次输入，不在课堂运行时等待键盘输入。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd11fab1",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1a24f20a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.225832Z",
     "iopub.status.busy": "2026-07-22T11:56:51.225656Z",
     "iopub.status.idle": "2026-07-22T11:56:51.228507Z",
     "shell.execute_reply": "2026-07-22T11:56:51.228205Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入类型： <class 'str'> <class 'str'>\n",
      "转换后类型： <class 'float'> <class 'int'>\n",
      "销售额： 59.699999999999996\n"
     ]
    }
   ],
   "source": [
    "price_text = \"19.9\"  # 模拟 input() 的返回值\n",
    "quantity_text = \"3\"\n",
    "\n",
    "price = float(price_text)\n",
    "quantity = int(quantity_text)\n",
    "total = price * quantity\n",
    "\n",
    "print(\"输入类型：\", type(price_text), type(quantity_text))\n",
    "print(\"转换后类型：\", type(price), type(quantity))\n",
    "print(\"销售额：\", total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83a6bab2",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b11ec5b9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.230377Z",
     "iopub.status.busy": "2026-07-22T11:56:51.230237Z",
     "iopub.status.idle": "2026-07-22T11:56:51.232168Z",
     "shell.execute_reply": "2026-07-22T11:56:51.231798Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：模拟输入文本，转换后计算销售额。\n",
    "# price_text = \"12.5\"\n",
    "# quantity_text = \"6\"\n",
    "# price =                  # 请填写 float 转换\n",
    "# quantity =               # 请填写 int 转换\n",
    "# total =                  # 请填写计算\n",
    "# print(f\"销售额：{total:.2f} 元\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db6a02f3",
   "metadata": {},
   "source": [
    "## 9. `print()`：把关键中间结果说清楚\n",
    "\n",
    "- 多个对象用逗号分隔；\n",
    "- `sep` 设置对象之间的分隔符；\n",
    "- `end` 设置结尾，默认换行；\n",
    "- f-string 适合把变量嵌入输出，并控制小数位。\n",
    "\n",
    "调试时打印“标签 + 值 + 类型”，比只打印一个孤立数字更容易检查。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e3b377e",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8b4e978a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.234307Z",
     "iopub.status.busy": "2026-07-22T11:56:51.234175Z",
     "iopub.status.idle": "2026-07-22T11:56:51.236701Z",
     "shell.execute_reply": "2026-07-22T11:56:51.236411Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "双肩包 | 3 | 199.0\n",
      "商品： 双肩包；销售额：597.00 元\n",
      "quantity 的类型： <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "product = \"双肩包\"\n",
    "quantity = 3\n",
    "unit_price = 199.0\n",
    "\n",
    "print(product, quantity, unit_price, sep=\" | \")\n",
    "print(\"商品：\", product, end=\"；\")\n",
    "print(f\"销售额：{quantity * unit_price:.2f} 元\")\n",
    "print(\"quantity 的类型：\", type(quantity))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e271347",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e673ed2a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.238397Z",
     "iopub.status.busy": "2026-07-22T11:56:51.238249Z",
     "iopub.status.idle": "2026-07-22T11:56:51.240064Z",
     "shell.execute_reply": "2026-07-22T11:56:51.239780Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：使用 sep 和 f-string 输出一行清楚的商品信息。\n",
    "# product = \"笔记本\"\n",
    "# quantity = 2\n",
    "# unit_price = 8.5\n",
    "# print(                  )  # 请填写 sep 输出\n",
    "# print(f\"合计：{              :.2f} 元\")  # 请填写表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f29b17d",
   "metadata": {},
   "source": [
    "## 10. 实训一：输出购物小票\n",
    "\n",
    "先分三步：保存数据、完成计算、按固定结构输出。格式化只负责展示，不应改变原始数值。\n",
    "\n",
    "下面把这些步骤放进 print_receipt 函数中。调用函数时传入商品、数量和单价；\n",
    "函数内部计算总额，return 把总额交回给后续代码使用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bd80fbf",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e0ca20f3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.241991Z",
     "iopub.status.busy": "2026-07-22T11:56:51.241830Z",
     "iopub.status.idle": "2026-07-22T11:56:51.244749Z",
     "shell.execute_reply": "2026-07-22T11:56:51.244448Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==============================\n",
      "       商务数据训练商店\n",
      "------------------------------\n",
      "商品：数据分析入门书\n",
      "数量：2\n",
      "单价：39.80 元\n",
      "合计：79.60 元\n",
      "==============================\n",
      "返回值用于后续统计： 79.6\n"
     ]
    }
   ],
   "source": [
    "def print_receipt(product, quantity, unit_price):\n",
    "    total = quantity * unit_price\n",
    "    print(\"=\" * 30)\n",
    "    print(\"       商务数据训练商店\")\n",
    "    print(\"-\" * 30)\n",
    "    print(f\"商品：{product}\")\n",
    "    print(f\"数量：{quantity}\")\n",
    "    print(f\"单价：{unit_price:.2f} 元\")\n",
    "    print(f\"合计：{total:.2f} 元\")\n",
    "    print(\"=\" * 30)\n",
    "    return total\n",
    "\n",
    "receipt_total = print_receipt(\"数据分析入门书\", 2, 39.8)\n",
    "print(\"返回值用于后续统计：\", receipt_total)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fb4e121",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "525065b7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.246355Z",
     "iopub.status.busy": "2026-07-22T11:56:51.246229Z",
     "iopub.status.idle": "2026-07-22T11:56:51.248162Z",
     "shell.execute_reply": "2026-07-22T11:56:51.247820Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：调用上方 print_receipt 函数，输出“水杯”3 件、单价 25.0 元的小票。\n",
    "# practice_total = print_receipt(\n",
    "#     # 请填写 product、quantity、unit_price\n",
    "# )\n",
    "# print(\"练习合计：\", practice_total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62f34811",
   "metadata": {},
   "source": [
    "## 11. 实训二：输出植树证书\n",
    "\n",
    "证书的关键是字段完整、结构稳定。函数的三个参数分别是昵称、植物名称和证书编号；\n",
    "调用时必须按顺序提供这三个值，避免把变化数据写死在函数内部。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f09ecc35",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c8d2ee99",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.249928Z",
     "iopub.status.busy": "2026-07-22T11:56:51.249775Z",
     "iopub.status.idle": "2026-07-22T11:56:51.252703Z",
     "shell.execute_reply": "2026-07-22T11:56:51.252381Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "╔════════════════════════════════╗\n",
      "║          植 树 证 书           ║\n",
      "╠════════════════════════════════╣\n",
      "  申请人：小数同学\n",
      "  植物：  梭梭树\n",
      "  编号：  TREE-2026-0722\n",
      "  行动：  让每一次选择都有记录\n",
      "╚════════════════════════════════╝\n"
     ]
    }
   ],
   "source": [
    "def print_tree_certificate(nickname, plant_name, certificate_id):\n",
    "    print(\"╔\" + \"═\" * 32 + \"╗\")\n",
    "    print(\"║          植 树 证 书           ║\")\n",
    "    print(\"╠\" + \"═\" * 32 + \"╣\")\n",
    "    print(f\"  申请人：{nickname}\")\n",
    "    print(f\"  植物：  {plant_name}\")\n",
    "    print(f\"  编号：  {certificate_id}\")\n",
    "    print(\"  行动：  让每一次选择都有记录\")\n",
    "    print(\"╚\" + \"═\" * 32 + \"╝\")\n",
    "\n",
    "\n",
    "print_tree_certificate(\"小数同学\", \"梭梭树\", \"TREE-2026-0722\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79d8a9b8",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dc6bfb80",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.254373Z",
     "iopub.status.busy": "2026-07-22T11:56:51.254197Z",
     "iopub.status.idle": "2026-07-22T11:56:51.256128Z",
     "shell.execute_reply": "2026-07-22T11:56:51.255792Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：调用上方证书函数，为“李华”与“银杏”输出一张证书。\n",
    "# print_tree_certificate(\n",
    "#     # 请填写 nickname 与 plant_name\n",
    "# )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3ba6d45",
   "metadata": {},
   "source": [
    "## 12. 常见错误：按格式、名称、类型、输出定位\n",
    "\n",
    "| 现象 | 常见原因 | 第一检查点 |\n",
    "|---|---|---|\n",
    "| IndentationError | 缩进层级不一致 | 同一代码块是否对齐 |\n",
    "| SyntaxError | 非法名称、漏引号、中文符号 | 当前行及上一行 |\n",
    "| NameError | 名称未定义或拼错 | 赋值是否先执行 |\n",
    "| TypeError | 不兼容类型参与运算 | 先查看 type(值) |\n",
    "| 结果重复文本 | 数字仍是字符串 | 是否调用 int 或 float |\n",
    "\n",
    "报错不是程序坏了，而是解释器指出第一个无法继续的位置。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "133c609e",
   "metadata": {},
   "source": [
    "### 课堂演示（样本代码）\n",
    "\n",
    "先由教师运行下方样本代码，带领学生观察输入、变量变化和输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6c058b5c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.257987Z",
     "iopub.status.busy": "2026-07-22T11:56:51.257805Z",
     "iopub.status.idle": "2026-07-22T11:56:51.260014Z",
     "shell.execute_reply": "2026-07-22T11:56:51.259736Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "错误定位顺序：格式 -> 名称 -> 类型 -> 输出\n",
      "示例 1：2price = 19.9 会触发 SyntaxError，因为名称不能以数字开头。\n",
      "示例 2：price = '19.9' 后直接计算 price * 3 会重复文本。\n",
      "示例 3：缩进不一致时，先检查同一代码块是否都使用 4 个空格。\n"
     ]
    }
   ],
   "source": [
    "print(\"错误定位顺序：格式 -> 名称 -> 类型 -> 输出\")\n",
    "print(\"示例 1：2price = 19.9 会触发 SyntaxError，因为名称不能以数字开头。\")\n",
    "print(\"示例 2：price = '19.9' 后直接计算 price * 3 会重复文本。\")\n",
    "print(\"示例 3：缩进不一致时，先检查同一代码块是否都使用 4 个空格。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "977f15aa",
   "metadata": {},
   "source": [
    "### 课堂练习（学生填写）\n",
    "\n",
    "先根据刚才的样本代码独立补全下方空位；完成后再运行并检查结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "53c68606",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-22T11:56:51.261470Z",
     "iopub.status.busy": "2026-07-22T11:56:51.261345Z",
     "iopub.status.idle": "2026-07-22T11:56:51.263084Z",
     "shell.execute_reply": "2026-07-22T11:56:51.262704Z"
    }
   },
   "outputs": [],
   "source": [
    "# 目标：阅读下列代码，先写出最先检查的位置，再修正它。\n",
    "# 代码：2price = 19.9\n",
    "# 第一检查点：名称不能以数字开头。\n",
    "# 修正：unit_price = 19.9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ad5c5f1",
   "metadata": {},
   "source": [
    "## 13. 课堂挑战与离场检验\n",
    "\n",
    "不复制前面的函数，独立完成：\n",
    "\n",
    "1. 设计 `store_name`、`product_name`、`quantity`、`unit_price` 四个变量；\n",
    "2. 用模拟输入文本完成类型转换；\n",
    "3. 计算合计并输出两位小数；\n",
    "4. 至少写一条解释业务规则的注释；\n",
    "5. 用一句话说明 `=` 与 `==` 的差别。\n",
    "\n",
    "**完成标准：** 代码可从上到下一次运行；名称可读；金额可算；输出可检查。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vix",
   "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.8"
  },
  "updated_for": "移除未讲解的高级用法，并在首次使用前补充说明"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
