From 11d0a1bb055dfde03de2166ca2406786e0af4697 Mon Sep 17 00:00:00 2001 From: "Matt.Mabx" Date: Mon, 21 Oct 2024 15:54:23 +0800 Subject: [PATCH] Update --- Note.ipynb | 286 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 0 2 files changed, 286 insertions(+) create mode 100644 Note.ipynb delete mode 100644 main.py diff --git a/Note.ipynb b/Note.ipynb new file mode 100644 index 0000000..c9517bd --- /dev/null +++ b/Note.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python不需要显式的指定变量和函数的类型" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = '1' # a为str类型\n", + "b = '2' # b为str类型\n", + "b = 3 # b可以被直接修改为int类型\n", + "\n", + "print(a+b) # 报错:不同数据类型之间不能运算,说明python是强类型的\n", + "\n", + "# 函数定义也不需要声明返回值和参数的类型\n", + "def plus(a, b):\n", + " return a+b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python的基础数据类型\n", + "**注意:基础数据类型均为保留关键字,不要这些关键字作为变量名!**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 1 # 合理\n", + "print(int)\n", + "int = 1 # 不合理但也不报错\n", + "print(int)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. 整数类型(`int`)\n", + "## 2. 浮点数类型(`float`)\n", + "## 3. 布尔类型(`bool`)\n", + "## 4. 列表类型(`list`)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_list = [1, 2, 3, \"hello\"]\n", + "print(type(my_list)) # 输出: \n", + "\n", + "# 访问元素\n", + "print(my_list[0]) # 输出: 1\n", + "print(my_list[-1]) # 输出: \"hello\"\n", + "print(my_list[0:2]) # 输出: [1, 2],要头不要尾\n", + "\n", + "# 修改元素\n", + "my_list[0] = 'modified'\n", + "print(my_list) # ['modified', 2, 3, \"hello\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 添加元素\n", + "my_list.append('append') \n", + "print(\"my_list.append:\", my_list) # ['modified', 2, 3, \"hello\", 'append']\n", + "my_list.insert(1, 'insert')\n", + "print(\"my_list.insert:\", my_list) # ['modified', 'insert', 2, 3, \"hello\", 4]\n", + "\n", + "# 删除元素\n", + "my_list.remove('modified')\n", + "print(\"my_list.remove:\", my_list) # ['insert', 2, 3, \"hello\", 4]\n", + "my_list.pop(3)\n", + "print(\"my_list.pop:\", my_list) # ['insert', 2, 3, 4]\n", + "\n", + "# 查找元素\n", + "print(\"2 in my_list:\")\n", + "print(2 in my_list) # 输出:True\n", + "print(\"9 in my_list:\")\n", + "print(9 in my_list) # 输出:False\n", + "print(\"my_list.index(2):\")\n", + "print(my_list.index(2)) # 输出:1\n", + "print(\"my_list.index(9):\")\n", + "print(my_list.index(9)) # 直接抛异常" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 5. 字符串类型(`str`)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "name = \"Python\"\n", + "name = 'Python' # 单引号和双引号在python里都表示字符串,没有任何区别" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 6. 元组类型(`tuple`)\n", + "## 7. 集合类型(`set`)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_set = {'one', 'two', 'three', 'three', 'four'}\n", + "print(my_set) # 输出: {'one', 'two', 'three', 'four'}?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_set.add('five')\n", + "print(my_set) # 输出: {'one', 'two', 'three', 'four', 'five'}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_set.remove('five')\n", + "print(my_set) # 输出: {'one', 'two', 'three', 'four'}?\n", + "my_set.pop()\n", + "print(my_set) # 输出: {'two', 'three', 'four'}?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(my_set[0]) \t # 直接报错:集合是无序的,不能按下标取值\n", + "print('two' in my_set) # 输出:True。集合通常就是这么用的,用来判断一个值在不在集合里。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 8. 字典类型(`dict`)\n", + "键-值对的无序集合,且**键不可重复**。类似于哈希表。" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "my_dict = {\"name\": \"Alice\", \"age\": 25}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 9. None 类型" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 条件控制语句\n", + "## 1. 任何版本都通用:if语句" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def positive_or_negative(a, b):\n", + " if a > 0:\n", + " print(\"It's positive.\")\n", + " if a == 0:\n", + " print(\"It's zero!\")\n", + " else:\n", + " print(\"It's negative.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 下面是一段错误代码\n", + "def describe_temperature(temp):\n", + " if temp >= 30:\n", + " print(\"It's a hot day!\")\n", + " if temp >= 20:\n", + " print(\"It's a warm day.\")\n", + " if temp >= 10:\n", + " print(\"It's a cool day.\")\n", + " else:\n", + " print(\"It's a cold day.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2. Python 3.10+ 新特性:match-case语句" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fruit_description(fruit):\n", + " match fruit:\n", + " case \"apple\":\n", + " print(\"This is a red or green fruit, usually sweet.\")\n", + " case \"banana\":\n", + " print(\"This is a long, yellow fruit, and it is soft.\")\n", + " case \"orange\":\n", + " print(\"This is a round orange fruit, and it is juicy\")\n", + " case \"grape\":\n", + " print(\"This is a small fruit.\")\n", + " case _:\n", + " print(\"I don't know this fruit.\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python", + "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.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/main.py b/main.py deleted file mode 100644 index e69de29..0000000