{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27e564a7-f1b6-4f28-8bb5-45cf1af8cd77",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "\n",
    "# 一、变量与数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8304a462-4ccd-49e7-9aa1-21ec7547032e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:26:44.214268Z",
     "iopub.status.busy": "2025-06-13T07:26:44.213935Z",
     "iopub.status.idle": "2025-06-13T07:26:44.218360Z",
     "shell.execute_reply": "2025-06-13T07:26:44.217829Z",
     "shell.execute_reply.started": "2025-06-13T07:26:44.214246Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'str'>\n",
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "# 1.1 基本数据类型\n",
    "# --------------------\n",
    "# 整数\n",
    "age = 25\n",
    "# 浮点数\n",
    "price = 19.99\n",
    "# 字符串\n",
    "name = \"Alice\"\n",
    "# 布尔值\n",
    "is_student = True\n",
    "\n",
    "\n",
    "print(type(age))     # <class 'int'>\n",
    "print(type(price))   # <class 'float'>\n",
    "print(type(name))    # <class 'str'>\n",
    "print(type(is_student))  # <class 'bool'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2838594d-3224-4ba8-b1f1-3f10ab9325a3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:28:18.424575Z",
     "iopub.status.busy": "2025-06-13T07:28:18.424250Z",
     "iopub.status.idle": "2025-06-13T07:28:18.427507Z",
     "shell.execute_reply": "2025-06-13T07:28:18.426996Z",
     "shell.execute_reply.started": "2025-06-13T07:28:18.424553Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 1.2 类型转换\n",
    "# --------------------\n",
    "num_str = \"123\"\n",
    "num_int = int(num_str)  # 字符串转整数\n",
    "num_float = float(num_str)  # 字符串转浮点数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5beb9f4-393b-4c3d-bcb0-30c33c5c1a9b",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 二、运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cfbd8761-be4d-43a7-b92c-ce9891b192a8",
   "metadata": {
    "ExecutionIndicator": {
     "show": false
    },
    "execution": {
     "iopub.execute_input": "2025-06-13T07:30:20.570726Z",
     "iopub.status.busy": "2025-06-13T07:30:20.570415Z",
     "iopub.status.idle": "2025-06-13T07:30:20.574589Z",
     "shell.execute_reply": "2025-06-13T07:30:20.573849Z",
     "shell.execute_reply.started": "2025-06-13T07:30:20.570701Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.3333333333333335\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 3\n",
    "\n",
    "# 2.1 算术运算符\n",
    "print(a / b)  # 13\n",
    "print(a ** b) # 1000 (幂运算)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fe12c728-7c20-4ef7-9207-fa756539a768",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:30:40.544749Z",
     "iopub.status.busy": "2025-06-13T07:30:40.544430Z",
     "iopub.status.idle": "2025-06-13T07:30:40.547959Z",
     "shell.execute_reply": "2025-06-13T07:30:40.547462Z",
     "shell.execute_reply.started": "2025-06-13T07:30:40.544725Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 2.2 比较运算符\n",
    "print(a == b)  # False\n",
    "print(a >= b)   # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7cd3f4ae-da24-4cf6-a5bd-be3c18ce9c51",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-13T07:32:41.777472Z",
     "iopub.status.busy": "2025-06-13T07:32:41.777152Z",
     "iopub.status.idle": "2025-06-13T07:32:41.780854Z",
     "shell.execute_reply": "2025-06-13T07:32:41.780188Z",
     "shell.execute_reply.started": "2025-06-13T07:32:41.777444Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 2.3 逻辑运算符\n",
    "print(True and False)  # False\n",
    "print(False or True and 1==2)   # True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95f9681b-e7a7-41dc-8fac-14a774af323f",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 三、控制结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "84b0df2b-037c-4864-a7d7-862109783abb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:34:04.667821Z",
     "iopub.status.busy": "2025-06-13T07:34:04.667482Z",
     "iopub.status.idle": "2025-06-13T07:34:13.498860Z",
     "shell.execute_reply": "2025-06-13T07:34:13.498359Z",
     "shell.execute_reply.started": "2025-06-13T07:34:04.667800Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入成绩: 80\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合格\n"
     ]
    }
   ],
   "source": [
    "# 3.1 条件语句\n",
    "score = int(input(\"请输入成绩:\"))\n",
    "if score >= 90:\n",
    "    print(\"优秀\")\n",
    "elif score >= 60:\n",
    "    print(\"合格\")\n",
    "else:\n",
    "    print(\"不及格\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7302a7ca-cc0b-471e-9b24-4213e7bbeeab",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-13T07:35:04.454937Z",
     "iopub.status.busy": "2025-06-13T07:35:04.454602Z",
     "iopub.status.idle": "2025-06-13T07:35:04.458647Z",
     "shell.execute_reply": "2025-06-13T07:35:04.458085Z",
     "shell.execute_reply.started": "2025-06-13T07:35:04.454917Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "Hello\n",
      "Hello\n",
      "Hello\n"
     ]
    }
   ],
   "source": [
    "# 3.2 循环语句\n",
    "# for循环\n",
    "for i in [0,1,2,3,4]:  # 0-4\n",
    "    print(i)\n",
    "\n",
    "# while循环\n",
    "count = 0\n",
    "while count < 3:\n",
    "    print(\"Hello\")\n",
    "    count += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4973b036-174a-44b7-9eca-aaa0d16e9123",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 四、函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b1c0711b-51ae-4230-8d85-7fb294fb9924",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-13T07:40:11.089827Z",
     "iopub.status.busy": "2025-06-13T07:40:11.089490Z",
     "iopub.status.idle": "2025-06-13T07:40:11.094296Z",
     "shell.execute_reply": "2025-06-13T07:40:11.093753Z",
     "shell.execute_reply.started": "2025-06-13T07:40:11.089804Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "1.4142135623730951\n"
     ]
    }
   ],
   "source": [
    "def add_numbers(a, b):\n",
    "    \"\"\"返回两个数的和\"\"\"\n",
    "    return a + b\n",
    "import math\n",
    "\n",
    "def distance(p1,p2):\n",
    "    return math.sqrt(add_numbers((p1[0]-p2[0])**2,(p1[1]-p2[1])**2))\n",
    "\n",
    "\n",
    "\n",
    "result = add_numbers(5, 3)\n",
    "print(result)  # 8\n",
    "\n",
    "print(distance([2,3],[3,4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdf3bde2-7674-4827-80f1-5f5274e0a735",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 五、列表操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5ccaaabc-17b2-4971-9ba0-d74a09eb4116",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:41:37.131738Z",
     "iopub.status.busy": "2025-06-13T07:41:37.131396Z",
     "iopub.status.idle": "2025-06-13T07:41:37.134654Z",
     "shell.execute_reply": "2025-06-13T07:41:37.134160Z",
     "shell.execute_reply.started": "2025-06-13T07:41:37.131716Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "# 添加元素\n",
    "fruits.append(\"orange\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "abbb4999-f5c2-4faf-86ff-974b5c7473df",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-13T07:42:35.362952Z",
     "iopub.status.busy": "2025-06-13T07:42:35.362499Z",
     "iopub.status.idle": "2025-06-13T07:42:35.365910Z",
     "shell.execute_reply": "2025-06-13T07:42:35.365413Z",
     "shell.execute_reply.started": "2025-06-13T07:42:35.362929Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cherry\n"
     ]
    }
   ],
   "source": [
    "# 访问元素\n",
    "print(fruits[2])  # banana"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c1e659f3-4929-47ee-8b1d-d29c0388e4e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:42:48.137931Z",
     "iopub.status.busy": "2025-06-13T07:42:48.137593Z",
     "iopub.status.idle": "2025-06-13T07:42:48.141129Z",
     "shell.execute_reply": "2025-06-13T07:42:48.140537Z",
     "shell.execute_reply.started": "2025-06-13T07:42:48.137910Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "# 切片操作\n",
    "print(fruits[1:3])  # ['banana', 'cherry']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "769b3425-583a-4c6f-9cd3-c96739b9a0be",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 六、字符串处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "01c47720-9165-40b6-bf2e-371fe3d70f05",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:47:22.088213Z",
     "iopub.status.busy": "2025-06-13T07:47:22.087876Z",
     "iopub.status.idle": "2025-06-13T07:47:22.092259Z",
     "shell.execute_reply": "2025-06-13T07:47:22.091715Z",
     "shell.execute_reply.started": "2025-06-13T07:47:22.088192Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python Programming\n",
      " python programming \n",
      " PYTHON PROGRAMMING \n",
      " Jython Jrogramming \n",
      "Bob is 30 years old\n"
     ]
    }
   ],
   "source": [
    "text = \" Python Programming \"\n",
    "\n",
    "# 常用方法\n",
    "print(text.strip())        # 去除两端空格\n",
    "print(text.lower())        # 转小写\n",
    "print(text.upper())        # 转大写\n",
    "print(text.replace(\"P\", \"J\"))  # 替换字符\n",
    "\n",
    "\n",
    "# 格式化输出\n",
    "name = \"Bob\"\n",
    "age = 30\n",
    "print(f\"{name} is {age} years old\")  # f-string格式化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6866bf39-b9de-409a-9730-20d51281d113",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 七、文件操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "61f546ee-c5a4-4dab-b9e3-96816aec3b0f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:48:56.944191Z",
     "iopub.status.busy": "2025-06-13T07:48:56.943862Z",
     "iopub.status.idle": "2025-06-13T07:48:56.960055Z",
     "shell.execute_reply": "2025-06-13T07:48:56.959549Z",
     "shell.execute_reply.started": "2025-06-13T07:48:56.944172Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "# 写入文件\n",
    "with open(\"test.txt\", \"w\") as f:\n",
    "    f.write(\"Hello World!\")\n",
    "\n",
    "# 读取文件\n",
    "with open(\"test.txt\", \"r\") as f:\n",
    "    content = f.read()\n",
    "    print(content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a2cb307-d25b-46c7-8a63-dc34e2104b7f",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 八、异常处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a66cb485-2c49-4a37-aeeb-c28eca74b530",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    print(10 / 0)\n",
    "except ZeroDivisionError:\n",
    "    print(\"不能除以零！\")\n",
    "finally:\n",
    "    print(\"执行完毕\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ece773-87f1-41ac-b249-74840f40992c",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# 九、常见代码报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2c97da50-f8b5-4e9e-a25f-fb4d27f218b1",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print(...)? (522289400.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[2], line 4\u001b[0;36m\u001b[0m\n\u001b[0;31m    print \"Hello, world!\"  # Python 3 中需要括号\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(...)?\n"
     ]
    }
   ],
   "source": [
    "# 1. SyntaxError: invalid syntax\n",
    "# 当Python遇到它无法理解的语法时抛出。\n",
    "\n",
    "print \"Hello, world!\"  # Python 3 中需要括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16d9c035-834f-4ae7-b6b9-270c52b0da76",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "expected an indented block after function definition on line 3 (664197369.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[3], line 4\u001b[0;36m\u001b[0m\n\u001b[0;31m    print(\"Hello!\")  # 不正确的缩进\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block after function definition on line 3\n"
     ]
    }
   ],
   "source": [
    "# 2. IndentationError: unexpected indent\n",
    "# 不正确的缩进会导致此错误。\n",
    "def my_function():\n",
    "print(\"Hello!\")  # 不正确的缩进"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f40d05c4-dfa4-49bc-9136-5e15ae820c70",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'y' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[4], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 3. NameError: name 'variable' is not defined\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;66;03m# 尝试使用未定义的变量。\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43my\u001b[49m)  \u001b[38;5;66;03m# x 没有被定义\u001b[39;00m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'y' is not defined"
     ]
    }
   ],
   "source": [
    "# 3. NameError: name 'variable' is not defined\n",
    "# 尝试使用未定义的变量。\n",
    "print(y)  # x 没有被定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f951a6a5-8c47-416c-99f5-cd547ca41168",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 4. TypeError: unsupported operand type(s)\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;66;03m# 操作数类型不支持的操作。\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m5\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m5\u001b[39;49m  \u001b[38;5;66;03m# 字符串和整数不能直接相加\u001b[39;00m\n",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "# 4. TypeError: unsupported operand type(s)\n",
    "# 操作数类型不支持的操作。\n",
    "\"5\" + 5  # 字符串和整数不能直接相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f75ef591-3b3e-464e-b7ac-68bc92d3cdc1",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[6], line 5\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 5. IndexError: list index out of range\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;66;03m# 访问列表中不存在的索引。\u001b[39;00m\n\u001b[1;32m      4\u001b[0m my_list \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m]\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmy_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m)  \u001b[38;5;66;03m# 最大索引是2\u001b[39;00m\n",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# 5. IndexError: list index out of range\n",
    "# 访问列表中不存在的索引。\n",
    "\n",
    "my_list = [1, 2, 3]\n",
    "print(my_list[3])  # 最大索引是2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "72c85b66-abf5-4b4c-a3a0-9e3a5ce76c6e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:54:39.675208Z",
     "iopub.status.busy": "2025-06-13T07:54:39.674882Z",
     "iopub.status.idle": "2025-06-13T07:54:39.739864Z",
     "shell.execute_reply": "2025-06-13T07:54:39.739189Z",
     "shell.execute_reply.started": "2025-06-13T07:54:39.675187Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'age'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mKeyError\u001b[39m                                  Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[40]\u001b[39m\u001b[32m, line 4\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# 6. KeyError\u001b[39;00m\n\u001b[32m      2\u001b[39m \u001b[38;5;66;03m# 尝试访问字典中不存在的键。\u001b[39;00m\n\u001b[32m      3\u001b[39m my_dict = {\u001b[33m\"\u001b[39m\u001b[33mname\u001b[39m\u001b[33m\"\u001b[39m: \u001b[33m\"\u001b[39m\u001b[33mAlice\u001b[39m\u001b[33m\"\u001b[39m}\n\u001b[32m----> \u001b[39m\u001b[32m4\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmy_dict\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mage\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m)  \u001b[38;5;66;03m# 键 \"age\" 不存在\u001b[39;00m\n",
      "\u001b[31mKeyError\u001b[39m: 'age'"
     ]
    }
   ],
   "source": [
    "# 6. KeyError\n",
    "# 尝试访问字典中不存在的键。\n",
    "my_dict = {\"name\": \"Alice\"}\n",
    "print(my_dict[\"age\"])  # 键 \"age\" 不存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1a7507dd-3ab7-4a09-908c-f94823dcfd83",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-13T07:55:07.458596Z",
     "iopub.status.busy": "2025-06-13T07:55:07.458286Z",
     "iopub.status.idle": "2025-06-13T07:55:07.479538Z",
     "shell.execute_reply": "2025-06-13T07:55:07.478857Z",
     "shell.execute_reply.started": "2025-06-13T07:55:07.458576Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'len'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mAttributeError\u001b[39m                            Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[41]\u001b[39m\u001b[32m, line 4\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# 7. AttributeError: 'type' object has no attribute\u001b[39;00m\n\u001b[32m      2\u001b[39m \u001b[38;5;66;03m# 尝试访问对象上不存在的属性。\u001b[39;00m\n\u001b[32m      3\u001b[39m my_list = []\n\u001b[32m----> \u001b[39m\u001b[32m4\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmy_list\u001b[49m\u001b[43m.\u001b[49m\u001b[43mlen\u001b[49m())  \u001b[38;5;66;03m# 正确方法是 len(my_list)\u001b[39;00m\n",
      "\u001b[31mAttributeError\u001b[39m: 'list' object has no attribute 'len'"
     ]
    }
   ],
   "source": [
    "# 7. AttributeError: 'type' object has no attribute\n",
    "# 尝试访问对象上不存在的属性。\n",
    "my_list = []\n",
    "print(my_list.len())  # 正确方法是 len(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "693aefcd-3881-495b-8a63-d3d01f8ed932",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 习题部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d77a854a-8f05-48bf-b8a5-474bdfe48613",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "习题 1: 变量与数据类型\n",
    "编写程序计算圆的面积（半径r=5，π取3.14）\n",
    "要求：\n",
    "1. 使用变量存储半径和π值\n",
    "2. 计算结果保留两位小数\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "习题 2: 控制结构\n",
    "编写程序判断一个年份是否是闰年\n",
    "条件：\n",
    "1. 能被4整除但不能被100整除\n",
    "2. 或能被400整除\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "习题 3: 函数与列表\n",
    "编写一个函数，接收数字列表，返回：\n",
    "1. 列表中的最大值\n",
    "2. 所有偶数的平均值\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "习题 4: 文件与异常\n",
    "编写程序：\n",
    "1. 尝试读取不存在的文件（test2.txt）\n",
    "2. 捕获文件不存在的异常\n",
    "3. 打印友好提示信息\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b718a9da-6d10-4504-bfc5-51eacbb44a7a",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-13T08:04:07.414000Z",
     "iopub.status.busy": "2025-06-13T08:04:07.413378Z",
     "iopub.status.idle": "2025-06-13T08:04:07.421740Z",
     "shell.execute_reply": "2025-06-13T08:04:07.421249Z",
     "shell.execute_reply.started": "2025-06-13T08:04:07.413978Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "圆的面积是：78.5\n",
      "2024是闰年\n",
      "(9, 0)\n",
      "(8, 5.0)\n",
      "文件不存在，请检查文件名！\n"
     ]
    }
   ],
   "source": [
    "# ====================\n",
    "# 习题答案\n",
    "# ====================\n",
    "\n",
    "# 习题 1 答案\n",
    "r = 5\n",
    "pi = 3.14\n",
    "area = round(pi * r ** 2, 2)\n",
    "print(f\"圆的面积是：{area}\")\n",
    "\n",
    "# 习题 2 答案\n",
    "year = 2024\n",
    "if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:\n",
    "    print(f\"{year}是闰年\")\n",
    "else:\n",
    "    print(f\"{year}不是闰年\")\n",
    "\n",
    "# 习题 3 答案\n",
    "def list_operations(numbers):\n",
    "    max_num = max(numbers)\n",
    "    even_nums=[]\n",
    "    for num in numbers:\n",
    "        if num % 2 == 0:\n",
    "            even_nums.append(num)\n",
    "    avg_even = sum(even_nums)/len(even_nums) if even_nums else 0\n",
    "    return max_num, avg_even\n",
    "\n",
    "# 测试用例\n",
    "print(list_operations([1, 3, 5, 7, 9]))        # (9, 0)\n",
    "print(list_operations([2, 4, 6, 8]))           # (8, 5.0)\n",
    "\n",
    "# 习题 4 答案\n",
    "try:\n",
    "    with open(\"test2.txt\", \"r\") as f:\n",
    "        content = f.read()\n",
    "except FileNotFoundError:\n",
    "    print(\"文件不存在，请检查文件名！\")"
   ]
  }
 ],
 "metadata": {
  "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
