{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/python3-fundamentals/",
  "atlas": "https://www.pystone.net/?node=python3-fundamentals#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/python3-fundamentals.md",
  "context": "https://www.pystone.net/notes/python3-fundamentals.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:python3-fundamentals",
  "slug": "python3-fundamentals",
  "title": "【Python】Python3基础",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "【Python】Python3基础",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "17241b22eed8db70a8a3b46df02d01c109e1c245581488eafeba8cd676147f81",
  "assets": [
    {
      "reference": "assets/1620810729480.png",
      "url": "/media/01442ff8e1c51afd31ad.png",
      "mediaType": "image/png",
      "contentHash": "01442ff8e1c51afd31ad6d911268ed57bf139ad8d4c909ba803faf7889293e1c",
      "byteLength": 723175,
      "width": 1920,
      "height": 1248
    },
    {
      "reference": "assets/1621500827997.png",
      "url": "/media/43cd92e26bdd3feec2b3.png",
      "mediaType": "image/png",
      "contentHash": "43cd92e26bdd3feec2b3736a0f870b2b107f909a86455ad2214cd0f2dbd08f74",
      "byteLength": 6478,
      "width": 408,
      "height": 61
    },
    {
      "reference": "assets/1624939455283.png",
      "url": "/media/f61b7a3af4919db06019.png",
      "mediaType": "image/png",
      "contentHash": "f61b7a3af4919db060196b2919b9dc44167e0793fb0ad9fe5c8d8ccce07e0df4",
      "byteLength": 15911,
      "width": 424,
      "height": 487
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "【Python】Python3基础",
      "anchor": "pythonpython3基础",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#pythonpython3%E5%9F%BA%E7%A1%80"
    },
    {
      "depth": 2,
      "text": "知识结构与资料",
      "anchor": "知识结构与资料",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E7%9F%A5%E8%AF%86%E7%BB%93%E6%9E%84%E4%B8%8E%E8%B5%84%E6%96%99"
    },
    {
      "depth": 2,
      "text": "基础语法特征",
      "anchor": "基础语法特征",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95%E7%89%B9%E5%BE%81"
    },
    {
      "depth": 3,
      "text": "string",
      "anchor": "string",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#string"
    },
    {
      "depth": 3,
      "text": "list",
      "anchor": "list",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#list"
    },
    {
      "depth": 4,
      "text": "增删改查",
      "anchor": "增删改查",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"
    },
    {
      "depth": 4,
      "text": "排序，反转，计算长度",
      "anchor": "排序反转计算长度",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E6%8E%92%E5%BA%8F%E5%8F%8D%E8%BD%AC%E8%AE%A1%E7%AE%97%E9%95%BF%E5%BA%A6"
    },
    {
      "depth": 4,
      "text": "遍历，创建，计算",
      "anchor": "遍历创建计算",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E9%81%8D%E5%8E%86%E5%88%9B%E5%BB%BA%E8%AE%A1%E7%AE%97"
    },
    {
      "depth": 5,
      "text": "for…in",
      "anchor": "forin",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#forin"
    },
    {
      "depth": 5,
      "text": "range",
      "anchor": "range",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#range"
    },
    {
      "depth": 5,
      "text": "计算",
      "anchor": "计算",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E8%AE%A1%E7%AE%97"
    },
    {
      "depth": 5,
      "text": "简单的遍历并处理",
      "anchor": "简单的遍历并处理",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E7%AE%80%E5%8D%95%E7%9A%84%E9%81%8D%E5%8E%86%E5%B9%B6%E5%A4%84%E7%90%86"
    },
    {
      "depth": 5,
      "text": "截取一部分",
      "anchor": "截取一部分",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E6%88%AA%E5%8F%96%E4%B8%80%E9%83%A8%E5%88%86"
    },
    {
      "depth": 3,
      "text": "元组",
      "anchor": "元组",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%85%83%E7%BB%84"
    },
    {
      "depth": 3,
      "text": "字典",
      "anchor": "字典",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%AD%97%E5%85%B8"
    },
    {
      "depth": 4,
      "text": "遍历",
      "anchor": "遍历",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E9%81%8D%E5%8E%86"
    },
    {
      "depth": 3,
      "text": "逻辑运算",
      "anchor": "逻辑运算",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97"
    },
    {
      "depth": 3,
      "text": "输入：",
      "anchor": "输入",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E8%BE%93%E5%85%A5"
    },
    {
      "depth": 3,
      "text": "if-else",
      "anchor": "if-else",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#if-else"
    },
    {
      "depth": 3,
      "text": "while",
      "anchor": "while",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#while"
    },
    {
      "depth": 3,
      "text": "函数",
      "anchor": "函数",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%87%BD%E6%95%B0"
    },
    {
      "depth": 3,
      "text": "class",
      "anchor": "class",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#class"
    },
    {
      "depth": 3,
      "text": "导入",
      "anchor": "导入",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%AF%BC%E5%85%A5"
    },
    {
      "depth": 4,
      "text": "函数导入",
      "anchor": "函数导入",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%87%BD%E6%95%B0%E5%AF%BC%E5%85%A5"
    },
    {
      "depth": 4,
      "text": "类导入",
      "anchor": "类导入",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E7%B1%BB%E5%AF%BC%E5%85%A5"
    },
    {
      "depth": 3,
      "text": "文件",
      "anchor": "文件",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E6%96%87%E4%BB%B6"
    },
    {
      "depth": 4,
      "text": "读取整个文件",
      "anchor": "读取整个文件",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E8%AF%BB%E5%8F%96%E6%95%B4%E4%B8%AA%E6%96%87%E4%BB%B6"
    },
    {
      "depth": 4,
      "text": "逐行读取",
      "anchor": "逐行读取",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E9%80%90%E8%A1%8C%E8%AF%BB%E5%8F%96"
    },
    {
      "depth": 4,
      "text": "写入文件",
      "anchor": "写入文件",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6"
    },
    {
      "depth": 4,
      "text": "json",
      "anchor": "json",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#json"
    },
    {
      "depth": 3,
      "text": "异常",
      "anchor": "异常",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%BC%82%E5%B8%B8"
    },
    {
      "depth": 2,
      "text": "python3与python2的区别",
      "anchor": "python3与python2的区别",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#python3%E4%B8%8Epython2%E7%9A%84%E5%8C%BA%E5%88%AB"
    },
    {
      "depth": 3,
      "text": "除法",
      "anchor": "除法",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E9%99%A4%E6%B3%95"
    },
    {
      "depth": 2,
      "text": "进阶",
      "anchor": "进阶",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E8%BF%9B%E9%98%B6"
    },
    {
      "depth": 3,
      "text": "arbitrary number of arguments",
      "anchor": "arbitrary-number-of-arguments",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#arbitrary-number-of-arguments"
    },
    {
      "depth": 4,
      "text": "kwargs",
      "anchor": "kwargs",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#kwargs"
    },
    {
      "depth": 3,
      "text": "os.system & os.popen & subprocess",
      "anchor": "ossystem-ospopen-subprocess",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#ossystem-ospopen-subprocess"
    },
    {
      "depth": 4,
      "text": "os.system",
      "anchor": "ossystem",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#ossystem"
    },
    {
      "depth": 4,
      "text": "os.popen",
      "anchor": "ospopen",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#ospopen"
    },
    {
      "depth": 4,
      "text": "subprocess.Popen",
      "anchor": "subprocesspopen",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#subprocesspopen"
    },
    {
      "depth": 4,
      "text": "对比",
      "anchor": "对比",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%AF%B9%E6%AF%94"
    },
    {
      "depth": 3,
      "text": "Lambda",
      "anchor": "lambda",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#lambda"
    },
    {
      "depth": 3,
      "text": "Map",
      "anchor": "map",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#map"
    },
    {
      "depth": 3,
      "text": "filter",
      "anchor": "filter",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#filter"
    },
    {
      "depth": 3,
      "text": "Iterator & Generator",
      "anchor": "iterator-generator",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#iterator-generator"
    },
    {
      "depth": 4,
      "text": "Iterator",
      "anchor": "iterator",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#iterator"
    },
    {
      "depth": 4,
      "text": "Generator",
      "anchor": "generator",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#generator"
    },
    {
      "depth": 5,
      "text": "Building Generators With Generator Expressions",
      "anchor": "building-generators-with-generator-expressions",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#building-generators-with-generator-expressions"
    },
    {
      "depth": 4,
      "text": "深入理解",
      "anchor": "深入理解",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3"
    },
    {
      "depth": 4,
      "text": "Profiling Generator Performance",
      "anchor": "profiling-generator-performance",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#profiling-generator-performance"
    },
    {
      "depth": 2,
      "text": "工具与应用",
      "anchor": "工具与应用",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#%E5%B7%A5%E5%85%B7%E4%B8%8E%E5%BA%94%E7%94%A8"
    },
    {
      "depth": 3,
      "text": "Sublime Build Python",
      "anchor": "sublime-build-python",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#sublime-build-python"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/python3-fundamentals/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:software-engineering-and-quality",
      "title": "软件工程与质量保障",
      "url": "https://www.pystone.net/notes/software-engineering-and-quality/",
      "atlas": "https://www.pystone.net/?node=software-engineering-and-quality#knowledge-atlas",
      "label": "软件工程与质量保障",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "CI-CD与质量保障/CI-CD工具链中的“【Python】Python3基础”导航项",
      "citation": "https://www.pystone.net/notes/software-engineering-and-quality/#ci-cd%E4%B8%8E%E8%B4%A8%E9%87%8F%E4%BF%9D%E9%9A%9Cci-cd%E5%B7%A5%E5%85%B7%E9%93%BE"
    }
  ],
  "contentMarkdown": "# 【Python】Python3基础\n\n> 创建时间：2021/5/11 15:02\n\n  * 【Python】Python3基础\n    * 知识结构与资料\n    * 基础语法特征\n      * string\n      * list\n        * 增删改查\n        * 排序，反转，计算长度\n        * 遍历，创建，计算\n          * for…in\n          * range\n          * 计算\n          * 简单的遍历并处理\n          * 截取一部分\n      * 元组\n      * 字典\n        * 遍历\n      * 逻辑运算\n      * 输入：\n      * if-else\n      * while\n      * 函数\n      * class\n      * 导入\n        * 函数导入\n        * 类导入\n      * 文件\n        * 读取整个文件\n        * 逐行读取\n        * 写入文件\n        * json\n      * 异常\n    * python3与python2的区别\n      * 除法\n    * 进阶\n      * arbitrary number of arguments\n        * **kwargs\n      * os.system & os.popen & subprocess\n        * os.system\n        * os.popen\n        * subprocess.Popen\n        * 对比\n      * Lambda\n      * Map\n      * filter\n      * Iterator & Generator\n        * Iterator\n        * Generator\n          * Building Generators With Generator Expressions\n        * 深入理解\n        * Profiling Generator Performance\n    * 工具与应用\n      * Sublime Build Python\n    * Ref\n\n## 知识结构与资料\n\n![Alt text](assets/1620810729480.png)\n\n<https://docs.python.org/3/>\n<https://docs.pythontab.com/python/python3.5/index.html>\n<https://www.liaoxuefeng.com/wiki/1016959663602400>\n<https://www.runoob.com/python3/python3-tutorial.html>\n\n快速教程：\n<https://juejin.cn/post/6844903765410070535>\n<https://www.cnblogs.com/wmyskxz/p/12550276.html>\n\n## 基础语法特征\n\n### string\n\n用引号括起的都是字符串\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n加号(+)来合并字符串\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\ntitle() - 将每个单词的首字母都改为大写。\nupper()、lower()\n\nrstrip() 删除右侧空白\nlstrip() 删除左侧空白\nstrip() 删除两端空白\nstrip() 不改变原来的字符串\n\nstr() - 将数字转换为字符串再进行拼接\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### list\n\n方括号([])来表示列表，并用逗号来分隔其中的元素\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 增删改查\n\n修改元素直接用索引修改\n\n在末尾添加：append\n\n插入：insert\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\ndel：按索引删除\npop()：删除列表最后一个元素并返回最后一个元素的值。\npop(index) - 传索引删除任意位置的值并返回\nremove()：按值删除第一个元素\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n> list[-2] 是获取倒数第二个元素\n\n#### 排序，反转，计算长度\n\n改变原列表：\nlist.sort()\nlist.sort(reverse=True) - 反转\n\n不影响原始排列顺序：\nsorted(list)\nsorted(list, reverse=True)\n\nlist.reverse()\n\nlen(list)\n\n#### 遍历，创建，计算\n\n##### for…in\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n##### range\n\nrange(1,6) - 前闭后开\n\nrange(0,11,2) - 指定步长为2\n\n##### 计算\n\nmin()：计算最小值\nmax()：计算最大值\nsum()：计算总和\n\n##### 简单的遍历并处理\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n##### 截取一部分\n\n前闭后开\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n如果使用 names3=names，则只是把引用（地址）赋值过去，指向的是用一份数据。\n\n### 元组\n\n不可变的列表被称为元组\n圆括号标识\n访问和遍历方式与列表一致\n\n### 字典\n\n可将任何Python对象用作字典中的值\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 遍历\n\n遍历所有键值对\nitems()\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n遍历所有键\nfor k in cats.keys()\nfor k in cats\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n没有顺序\n\n遍历所有值 values()\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n遍历并去除重复项 set()\nfor value in set(cat.values())\n\n> 可以在列表中嵌套字典、在字典中嵌套列表以及在字典中嵌套字典。\n\n### 逻辑运算\n\n  * and\n\n  * or\n\n  * in - 判断列表是否包含某元素\n\n  * not in - 判断不包含\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### 输入：\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### if-else\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### while\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\nfor…in\nelse\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\npass 不做任何事情，一般用做占位语句\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### 函数\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n传递任意数量的实参\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n  * 函数名应只包含小写字母和下划线\n\n  * 给形参指定默认值时，等号两边不要有空格\n\n### class\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### 导入\n\n#### 函数导入\n\n> cat.py\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\nimport cat\nimport cat as c\n\nfrom cat import eat, run\nfrom cat import eat as cat_eat\n\nfrom cat import * #不推荐。避免同名函数覆盖\n\n要么只导入你需要使用的函数，要么导入整个模块并使用句点表示法。\n\n#### 类导入\n\n假如 car.py 包含了三个类 Car ， Battery 和 ElectricCar 。\n\nimport car\nfrom car import Car, ElectricCar\nfrom module_name import * #不推荐\n\n### 文件\n\nwith - 在不再需要访问文件后将其关闭。有了 with 你只管打开文件，并在需要时使用它，Python自会 在合适的时候自动将其关闭。\nopen() - 用于打开一个文件，参数为文件的路径。文件路径可以是相对路径，也可以是绝对路径。\n\n#### 读取整个文件\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n> 相比于原始文件，该输出唯一不同的地方是末尾多了一个空行。为何会多出这个空行呢?因为 read() 到达文件末尾时返回一个空字符串，而将这个空字符串显示出来时就是一个空行。要删除多出来的空行，可在print语句中使用 rstrip()。\n\n#### 逐行读取\n\n要以每次一行的方式检查文件，可对文件对象使用for循环。\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 写入文件\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n  * r ：只读。\n\n  * w : 只写。如果文件不存在则创建，如果文件存在则先清空，再写入。\n\n  * a ：附加模式，写入的内容追加到原始文件后面。如果文件不存在则创建。\n\n  * r+ ：可读可写。\n\n#### json\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### 异常\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n## python3与python2的区别\n\n### 除法\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n## 进阶\n\n### arbitrary number of arguments\n\nThe **non keyword arguments** are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk `*`.\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### **kwargs\n\nThe arguments are passed as a dictionary\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### os.system & os.popen & subprocess\n\n#### os.system\n\n**同步调用（阻塞）**\n\n  * os.system方法是os模块最基础的方法，其它的方法一般在该方法基础上封装完成。\n\n  * 创建一个子进程在系统上执行命令行，子进程的执行结果无法影响主进程。\n\n  * 返回值是脚本的退出状态码，0表示成功，其他均为失败\n\n坑点：\n\n  1. 连续几个system命令执行，是启动了多个子进程来运行，上一条 cd 的效果不会在下一个子进程中生效。\n\n![Alt text](assets/1621500827997.png)\n\n  2. os.system(command) 在调用完shell脚本后，返回一个16位的二进制数，低位为杀死所调用脚本的信号号码，高位为脚本的退出状态码，即脚本中“exit 1”的代码执行后，os.system函数返回值的高位数则是1，如果低位数是0的情况下，则函数的返回值是0x0100,换算为十进制得到256。\n\n#### os.popen\n\n**非阻塞**\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\ncommand – 使用的命令。\nmode – 模式权限可以是 ‘r’(默认) 或 ‘w’。\nbufsize – 指明了文件需要的缓冲大小：0意味着无缓冲；1意味着行缓冲；\n\n返回一个文件描述符号为fd的打开的文件对象。\n\n**read()、eadlines()、close()** 会产生阻塞效果。\n**文件用完就应该关闭** 。使用如下方法会自动关闭\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n像调用”ls”这样的shell命令，应该使用popen的方法来获得内容，对比如下：\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### subprocess.Popen\n\n可以用 **wait** 来等待命令执行完成\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n当test.txt大于64k时，会阻塞住。原因是，子进程产生一些数据，他们会被buffer起来，当buffer满了，会写到子进程的标准输出和标准错误输出，这些东西通过管道发送给父进程。当管道满了之后，子进程就停止写入，于是就卡住了。\n\n解决方法：\n\n  1. 在wait前，读取管道，这样管道就不会满\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n  2. 使用communicate\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 对比\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### Lambda\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### Map\n\n将函数应用于各种数据结构中的元素\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### filter\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### Iterator & Generator\n\n#### Iterator\n\nAn iterator is an object that can be iterated upon, meaning that you can traverse through all the values.\n\n**Iterator vs Iterable：** Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from.\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\nLooping Through an Iterator: We can also use a for loop to iterate through an iterable object.\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\nCreate an Iterator\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### Generator\n\n在 Python 中，使用了 yield 的函数被称为生成器函数（generator functions）。\nIntroduced with PEP 255, generator functions are a special kind of function that return **a lazy iterator**. These are objects that you can loop over like a list. However, unlike lists, lazy iterators **do not store their contents in memory**.\n\n在调用生成器运行的过程中，每次遇到 yield 时函数会暂停并保存当前所有的运行信息，返回 yield 的值, 并在下一次执行 next() 方法时从当前位置继续运行。\n\nExample: Generating an Infinite Sequence\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n##### Building Generators With Generator Expressions\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 深入理解\n\npython的yield关键字与C#类似。函数中有yield语句时，调用该函数，会返回一个迭代器，并不会真正执行该函数。首次，对该函数调用next时，函数开始执行，会尝试执行到第一次yield语句，获取返回值。如果在调用到yield语句之前，函数返回，则会抛出异常 StopIteration 。如果成功执行到yield语句并返回，再次调用next函数时，会从这个yield语句的下一个语句开始执行。\n\n示例如下，调用recv，会返回一个generator对象，调用next时，函数开始执行。而起初在调用时，会执行到break并返回，无法执行到yield，next函数会抛出异常，无法继续遍历。直到满足条件，可以执行到yield时，调用next才会获取到返回值 cnt ，下次调用next时，本次yield语句后的一个语句开始执行。\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n输出如下：\n\n![Alt text](assets/1624939455283.png)\n\n#### Profiling Generator Performance\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n## 工具与应用\n\n<https://www.zhihu.com/question/24590883/answer/1220720307>\n\n### Sublime Build Python\n\n<https://blog.csdn.net/github_36601823/article/details/69218197>\n\n## Ref\n\n<https://www.zhihu.com/question/300985609/answer/1225638751>\n<https://juejin.cn/post/6844903765410070535>\n<https://www.cnblogs.com/yu97271486/p/12497622.html>\n<https://www.jianshu.com/p/62f0b9a3dd3e>\n<https://www.w3schools.com/python/python_iterators.asp>\n<https://realpython.com/introduction-to-python-generators/>\n"
}
