text: (window.webpackJsonp=window.webpackJsonp||[]).push([[41],{212:function(t,s,e){"use strict";e.r(s);var a=e(0),n=Object(a.a)({},(function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"content"},[t._m(0),t._v(" "),t._m(1),t._m(2),t._v(" "),t._m(3),t._v(" "),t._m(4),e("p",[t._v("由于Python源代码也是一个文本文件，所以，当你的源代码中包含中文的时候，在保存源代码时，就需要务必指定保存为UTF-8编码。当Python解释器读取源代码时，"),e("font",{staticStyle:{background:"pink"}},[t._v("为了让它按UTF-8编码读取，我们通常在文件开头写上这两行")]),t._v("：")],1),t._v(" "),t._m(5),t._m(6),t._v(" "),t._m(7),e("p",[t._v("和下面是一样的！")]),t._v(" "),t._m(8),e("ul",[e("li",[t._v("我观察到，"),e("font",{staticStyle:{background:"pink"}},[t._v("Python中print的通用程度，类似到C++中的"),e("code",[t._v("cout")])])],1)]),t._v(" "),t._m(9),t._v(" "),t._m(10),e("p",[t._v("但是，下面也可以？")]),t._v(" "),t._m(11),t._m(12),t._v(" "),t._m(13),t._v(" "),e("ul",[e("li",[t._v("每一行都是一个语句，"),e("font",{staticStyle:{background:"yellow"}},[t._v("当语句以冒号"),e("code",[t._v(":")]),t._v("结尾时，"),e("strong",[t._v("缩进")]),t._v("的语句视为代码块")]),t._v("。")],1),t._v(" "),e("li",[t._v("Python使用缩进来组织代码块，请务必遵守约定俗成的习惯，坚持使用4个空格的缩进。")]),t._v(" "),e("li",[t._v("在文本编辑器中，需要设置把Tab自动转换为4个空格，确保不混用Tab和空格。")])]),t._v(" "),t._m(14),t._v(" "),t._m(15),t._v(" "),t._m(16),t._v(" "),t._m(17),t._v(" "),t._m(18),t._v(" "),t._m(19),t._v(" "),t._m(20),t._v(" "),t._m(21),t._v(" "),t._m(22),t._v(" "),e("blockquote",[e("p",[t._v("注意："),e("font",{staticStyle:{background:"yellow"}},[t._v("Python的整数没有大小限制")]),t._v("，而某些语言的整数根据其存储长度是有大小限制的，例如Java对32位整数的范围限制在"),e("code",[t._v("-2147483648")]),t._v("-"),e("code",[t._v("2147483647")]),t._v("。")],1)]),t._v(" "),t._m(23),t._v(" "),e("ul",[t._m(24),t._v(" "),t._m(25),t._v(" "),e("li",[e("font",{staticStyle:{background:"yellow"}},[t._v("整数和浮点数在计算机内部存储的方式是不同的，整数运算永远是精确的（除法难道也是精确的？是的！），而浮点数运算则可能会有四舍五入的误差。")])],1)]),t._v(" "),t._m(26),t._v(" "),t._m(27),t._v(" "),e("ul",[e("li",[e("p",[e("font",{staticStyle:{background:"yellow"}},[t._v("字符串是以单引号"),e("code",[t._v("'")]),t._v("或双引号"),e("code",[t._v('"')]),t._v("括起来的任意文本")]),t._v("，比如"),e("code",[t._v("'abc'")]),t._v("，"),e("code",[t._v('"xyz"')]),t._v("等等。请注意，"),e("code",[t._v("''")]),t._v("或"),e("code",[t._v('""')]),t._v("本身只是一种表示方式，不是字符串的一部分")],1),t._v(" "),t._m(28)]),t._v(" "),e("li",[e("p",[e("font",{staticStyle:{background:"pink"}},[t._v("如果"),e("code",[t._v("'")]),t._v("本身也是一个字符，那就可以用"),e("code",[t._v('""')]),t._v("括起来")]),t._v("，比如"),e("code",[t._v('"I\'m OK"')]),t._v("包含的字符是"),e("code",[t._v("I")]),t._v("，"),e("code",[t._v("'")]),t._v("，"),e("code",[t._v("m")]),t._v("，空格，"),e("code",[t._v("O")]),t._v("，"),e("code",[t._v("K")]),t._v("这6个字符。")],1)]),t._v(" "),e("li",[e("p",[e("font",{staticStyle:{background:"pink"}},[t._v("如果字符串内部既包含"),e("code",[t._v("'")]),t._v("又包含"),e("code",[t._v('"')]),t._v("怎么办？可以用转义字符"),e("code",[t._v("\\")]),t._v("来标识，比如：")])],1),t._v(" "),t._m(29)]),t._v(" "),t._m(30),t._v(" "),e("li",[e("p",[t._v("如果字符串里面有很多字符都需要转义，就需要加很多"),e("code",[t._v("\\")]),t._v("，为了简化，"),e("font",{staticStyle:{background:"pink"}},[t._v("Python还允许用"),e("code",[t._v("r''")]),t._v("表示"),e("code",[t._v("''")]),t._v("内部的字符串默认不转义")])],1),t._v(" "),t._m(31)])]),t._v(" "),t._m(32),t._v(" "),e("ul",[e("li",[t._v("布尔值和布尔代数的表示完全一致，一个布尔值只有"),e("code",[t._v("True")]),t._v("、"),e("code",[t._v("False")]),e("font",{staticStyle:{background:"yellow"}},[t._v("两种")]),t._v("值，要么是"),e("code",[t._v("True")]),t._v("，要么是"),e("code",[t._v("False")]),t._v("，在Python中，可以"),e("font",{staticStyle:{background:"yellow"}},[t._v("直接用"),e("code",[t._v("True")]),t._v("、"),e("code",[t._v("False")]),t._v("表示布尔值（请注意大小写）")])],1),t._v(" "),t._m(33)]),t._v(" "),e("blockquote",[e("p",[e("font",{staticStyle:{background:"pink"}},[t._v("上面是和Java和C++不同的地方")])],1)]),t._v(" "),t._m(34),t._v(" "),t._m(35),t._v(" "),e("blockquote",[e("p",[e("font",{staticStyle:{background:"pink"}},[t._v("上面是和Java和C++不同的地方")])],1)]),t._v(" "),t._m(36),t._v(" "),t._m(37),t._v(" "),t._m(38),t._v(" "),t._m(39),t._v(" "),t._m(40),t._m(41),t._v(" "),t._m(42),t._v(" "),e("p",[t._v("静态语言在定义变量时必须指定变量类型，如果赋值的时候类型不匹配，就会报错。例如Java是静态语言。")]),t._v(" "),t._m(43),t._v(" "),e("ul",[e("li",[t._v("所谓常量就是不能变的变量，比如常用的数学常数π就是一个常量。"),e("font",{staticStyle:{background:"pink"}},[t._v("在Python中，通常用"),e("strong",[t._v("全部大写")]),t._v("的变量名表示常量")]),t._v(" "),e("ul",[e("li",[t._v("但事实上"),e("code",[t._v("PI")]),t._v("仍然是一个"),e("strong",[t._v("变量")]),t._v("，"),t._m(44),t._v("，所以，"),e("font",{staticStyle:{background:"yellow"}},[t._v("用全部大写的变量名表示常量"),e("strong",[t._v("只是一个习惯上的用法")]),t._v("，如果你一定要改变变量"),e("code",[t._v("PI")]),t._v("的值，也"),e("strong",[t._v("没人能拦住你")]),t._v("。")])],1)])],1)]),t._v(" "),t._m(45),t._v(" "),t._m(46),t._v(" "),t._m(47),t._v(" "),t._m(48),t._m(49),t._v(" "),t._m(50),e("blockquote",[e("p",[e("font",{staticStyle:{background:"pink"}},[t._v("上面是和Java和C++不同的地方")])],1)]),t._v(" "),t._m(51),t._v(" "),t._m(52),t._v(" "),t._m(53),t._m(54),t._v(" "),t._m(55),t._v(" "),e("ul",[t._m(56),t._v(" "),e("li",[e("p",[t._v("Unicode标准也在不断发展，但"),e("font",{staticStyle:{background:"yellow"}},[t._v("最常用的是UCS-16编码，用两个字节表示一个字符（如果要用到非常偏僻的字符，就需要4个字节）")]),t._v("。现代操作系统和大多数编程语言都直接支持Unicode。")],1),t._v(" "),t._m(57)]),t._v(" "),e("li",[e("p",[t._v("新的问题又出现了：如果统一成Unicode编码，乱码问题从此消失了。但是，如果你写的文本基本上全部是英文的话，用Unicode编码比ASCII编码需要多一倍的存储空间，在存储和传输上就十分不划算。")]),t._v(" "),t._m(58),t._v(" "),t._m(59),t._v(" "),e("ul",[e("li",[t._v("在计算机"),e("font",{staticStyle:{background:"yellow"}},[t._v("内存中，统一使用Unicode编码")]),t._v("，当需"),e("strong",[t._v("要保存到硬盘")]),t._v("或者"),e("strong",[t._v("需要传输")]),t._v("的时候，就转换为UTF-8编码。\n"),t._m(60)],1)])])]),t._v(" "),t._m(61),t._v(" "),e("ul",[e("li",[t._v("在最新的"),e("font",{staticStyle:{background:"pink"}},[t._v("Python 3版本中，字符串是以Unicode编码的")]),t._v("，也就是说，Python的字符串支持多语言，例如：")],1)]),t._v(" "),t._m(62),t._m(63),t._v(" "),t._m(64),e("p",[e("font",{staticStyle:{background:"pink"}},[t._v("由于Python的字符串类型是"),e("code",[t._v("str")]),t._v("，在内存中以Unicode表示，一个字符对应若干个字节")]),t._v("。如果要在网络上传输，或者保存到磁盘上，就需要把"),e("code",[t._v("str")]),t._v("变为以字节为单位的"),e("code",[t._v("bytes")]),t._v("。")],1),t._v(" "),t._m(65),t._v(" "),t._m(66),t._m(67),t._v(" "),t._m(68),t._v(" "),t._m(69),t._v(" "),t._m(70),t._m(71),t._v(" "),t._m(72),t._v(" "),t._m(73),t._v(" "),t._m(74),t._m(75),t._v(" "),t._m(76),t._m(77),t._v(" "),t._m(78),t._m(79),t._v(" "),t._m(80),t._m(81),t._v(" "),t._m(82),t._m(83),t._v(" "),t._m(84),t._v(" "),e("ul",[e("li",[t._v("在Python中，采用的格式化方式"),e("font",{staticStyle:{background:"yellow"}},[t._v("和C语言是一致的，用"),e("code",[t._v("%")]),t._v("实现")]),t._v(" "),t._m(85)],1)]),t._v(" "),t._m(86),t._m(87),t._m(88),e("p",[t._v("常见的占位符有：")]),t._v(" "),t._m(89),t._v(" "),t._m(90),t._v(" "),t._m(91),t._v(" "),t._m(92),t._m(93),t._v(" "),t._m(94),t._v(" "),t._m(95),t._m(96),t._v(" "),t._m(97),t._v(" "),t._m(98),t._v(" "),t._m(99),t._v(" "),t._m(100),e("p",[t._v("elif是else if的缩写，完全可以有多个elif")]),t._v(" "),e("ul",[e("li",[e("font",{staticStyle:{background:"yellow"}},[t._v("只要"),e("code",[t._v("x")]),t._v("是非零数值、非空字符串、非空list等")]),t._v("，就判断为"),e("code",[t._v("True")]),t._v("，否则为"),e("code",[t._v("False")]),t._v("。")],1)]),t._v(" "),t._m(101),t._v(" "),t._m(102),t._v(" "),t._m(103),t._v(" "),t._m(104),t._v(" "),t._m(105),t._m(106),t._v(" "),t._m(107),e("ul",[e("li",[e("font",{staticStyle:{background:"yellow"}},[t._v("原来"),e("code",[t._v("int()")]),t._v("函数发现一个字符串"),e("strong",[t._v("并不是合法的数字")]),t._v("时就会"),e("strong",[t._v("报错")]),t._v("，程序就退出了。")])],1)]),t._v(" "),e("p",[t._v("如何检查并捕获程序运行期的错误呢？后面的错误和调试会讲到。")]),t._v(" "),t._m(108),t._v(" "),t._m(109),t._v(" "),t._m(110),t._v(" "),e("ul",[e("li",[t._v("一种是for...in循环，依次把"),e("font",{staticStyle:{background:"yellow"}},[t._v("list或tuple")]),t._v("中的每个元素迭代出来\n"),t._m(111)],1),t._v(" "),t._m(112)]),t._v(" "),t._m(113),e("ul",[e("li",[t._v("如果要计算1-100的整数之和，从1写到100有点困难\n"),e("ul",[t._m(114),t._v(" "),e("li",[t._v("再通过"),e("code",[t._v("list()")]),t._v("函数可以转换为list。比如"),e("code",[t._v("range(5)")]),t._v("生成的序列是"),e("font",{staticStyle:{background:"yellow"}},[t._v("从0开始小于5")]),t._v("的整数：")],1)])])]),t._v(" "),t._m(115),t._m(116),t._m(117),t._v(" "),t._m(118),t._m(119),t._v(" "),t._m(120),t._m(121),t._v(" "),t._m(122),t._v(" "),t._m(123),t._v(" "),t._m(124),t._v(" "),t._m(125),t._v(" "),t._m(126),t._v(" "),t._m(127),t._v(" "),t._m(128),t._v(" "),t._m(129),t._v(" "),t._m(130),t._m(131),t._v(" "),t._m(132),t._m(133),t._v(" "),t._m(134),t._v(" "),t._m(135),t._m(136),t._v(" "),t._m(137),t._v(" "),t._m(138),t._v(" "),t._m(139),t._m(140),t._v(" "),t._m(141),t._v(" "),t._m(142),t._m(143),t._v(" "),t._m(144),t._v(" "),e("p",[e("font",{staticStyle:{background:"yellow"}},[t._v("调用函数时，如果参数个数不对，Python解释器会自动检查出来")]),t._v("，并抛出"),e("code",[t._v("TypeError")]),t._v("：")],1),t._v(" "),t._m(145),t._m(146),t._v(" "),t._m(147),t._m(148),t._v(" "),e("p",[t._v("让我们修改一下"),e("code",[t._v("my_abs")]),t._v("的定义，"),e("font",{staticStyle:{background:"yellow"}},[t._v("对参数类型做检查，"),e("strong",[t._v("只允许整数和浮点数类型")]),t._v("的参数。数据类型检查可以用内置函数"),e("code",[t._v("isinstance()")]),t._v("实现")]),t._v("：")],1),t._v(" "),t._m(149),e("p",[t._v("添加了参数检查后，如果传入错误的参数类型，函数就可以抛出一个错误：")]),t._v(" "),t._m(150),e("p",[t._v("错误和异常处理将在后续讲到。")]),t._v(" "),t._m(151),t._v(" "),e("p",[t._v("函数可以返回多个值吗？答案是肯定的。")]),t._v(" "),e("p",[t._v("比如在游戏中经常需要从一个点移动到另一个点，给出坐标、位移和角度，就可以计算出新的坐标：")]),t._v(" "),t._m(152),t._m(153),t._v(" "),e("p",[t._v("然后，我们就可以同时获得返回值：")]),t._v(" "),t._m(154),e("p",[t._v("但其实这只是一种假象，Python函数返回的仍然是单一值：")]),t._v(" "),t._m(155),e("p",[t._v("原来返回值是一个tuple！但是，在语法上，返回一个tuple可以省略括号，而多个变量可以同时接收一个tuple，按位置赋给对应的值，所以，Python的函数返回多值其实就是返回一个tuple，但写起来更方便。")]),t._v(" "),t._m(156),t._v(" "),e("ul",[e("li",[t._v("难点"),e("a",{attrs:{href:"https://www.liaoxuefeng.com/wiki/1016959663602400/1017261630425888",target:"_blank",rel:"noopener noreferrer"}},[t._v("传送门"),e("OutboundLink")],1)])]),t._v(" "),e("h3",{attrs:{id:"import的用法在后续模块一节中会详细介绍。"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#import的用法在后续模块一节中会详细介绍。"}},[t._v("#")]),t._v(" "),e("code",[t._v("import")]),t._v("的用法在后续"),e("a",{attrs:{href:"https://www.liaoxuefeng.com/wiki/1016959663602400/1017454145014176",target:"_blank",rel:"noopener noreferrer"}},[t._v("模块"),e("OutboundLink")],1),t._v("一节中会详细介绍。")]),t._v(" "),e("ul",[t._m(157),t._v(" "),e("li",[t._v("尾递归是指，在函数返回的时候，调用自身本身，并且，return语句不能包含表达式。这样，编译器或者解释器就可以把尾递归做优化，使递归本身无论调用多少次，都只占用一个栈帧，不会出现栈溢出的情况。")]),t._v(" "),e("li",[t._v("尾递归调用时，如果做了优化，栈不会增长，因此，无论多少次调用也不会导致栈溢出。")]),t._v(" "),e("li",[e("font",{staticStyle:{background:"yellow"}},[t._v("遗憾的是，"),e("strong",[t._v("大多数编程语言没有针对尾递归做优化")]),t._v("，Python解释器也没有做优化")]),t._v("，所以，即使把上面的"),e("code",[t._v("fact(n)")]),t._v("函数改成尾递归方式，也会导致栈溢出。")],1)]),t._v(" "),t._m(158),t._v(" "),t._m(159)])}),[function(){var t=this.$createElement,s=this._self._c||t;return s("h1",{attrs:{id:"python杂记"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#python杂记"}},[this._v("#")]),this._v(" Python杂记")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language-txt line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-txt"}},[s("code",[this._v('<font style="background:yellow">\n')])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"✅基础对比"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#✅基础对比"}},[this._v("#")]),this._v(" ✅基础对比")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_1-linux下的python"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-linux下的python"}},[this._v("#")]),this._v(" 1.Linux下的python")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#!/usr/bin/env python3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'111'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language-python line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-python"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[this._v("#!/usr/bin/env python3")]),this._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[this._v("# -*- coding: utf-8 -*-")]),this._v("\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_2-关于空格「这种没影响」和print"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-关于空格「这种没影响」和print"}},[this._v("#")]),this._v(" 2.关于空格「这种没影响」和print")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 下面有空格")]),t._v("\nname "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("input")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 下面没有空格，但是不影响这个函数的使用")]),t._v("\nname"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("input")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_3-python中的-和"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-python中的-和"}},[this._v("#")]),this._v(" 3.Python中的"),s("code",[this._v("''")]),this._v("和"),s("code",[this._v('""')])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("//")]),t._v("一般写成下面这样\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'111'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language-python line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-python"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[this._v("print")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[this._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[this._v('"111"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[this._v(")")]),this._v("\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_4-注释和linux的一致性"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-注释和linux的一致性"}},[this._v("#")]),this._v(" 4.注释和Linux的一致性"),s("code",[this._v("#")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_5-关于缩进「有影响了」"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_5-关于缩进「有影响了」"}},[this._v("#")]),this._v(" 5.关于缩进「有影响了」")])},function(){var t=this.$createElement,s=this._self._c||t;return s("blockquote",[s("p",[this._v("缩进有利有弊。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"好处"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#好处"}},[this._v("#")]),this._v(" 好处")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("好处是强迫你写出格式化的代码，但没有规定缩进是几个空格还是Tab。按照约定俗成的惯例，应该始终坚持使用"),s("em",[this._v("4个空格")]),this._v("的缩进。")]),this._v(" "),s("li",[this._v("另一个好处是强迫你写出缩进较少的代码，你会倾向于把一段很长的代码拆分成若干函数，从而得到缩进较少的代码。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"坏处"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#坏处"}},[this._v("#")]),this._v(" 坏处")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("“复制－粘贴”功能失效了，这是最坑爹的地方。当你重构代码时，粘贴过去的代码必须重新检查缩进是否正确。此外，IDE很难像格式化Java代码那样格式化Python代码")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("blockquote",[s("p",[this._v("Python程序和Java和C++一样是"),s("strong",[this._v("大小写敏感")]),this._v("的，如果写错了大小写，程序会报错。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_6-在python中，能够直接处理的数据类型有以下几种"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_6-在python中，能够直接处理的数据类型有以下几种"}},[this._v("#")]),this._v(" 6.在Python中，能够直接处理的数据类型有以下几种")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_6-1-整数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_6-1-整数"}},[this._v("#")]),this._v(" 6.1.整数")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("有时候用十六进制表示整数比较方便，十六进制用"),e("code",[t._v("0x")]),t._v("前缀和0-9，a-f表示")]),t._v(" "),e("li",[t._v("对于很大的数，例如"),e("code",[t._v("10000000000")]),t._v("，很难数清楚0的个数。Python允许在数字中间以"),e("code",[t._v("_")]),t._v("分隔")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_6-2-浮点数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_6-2-浮点数"}},[this._v("#")]),this._v(" 6.2.浮点数")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("对于很大或很小的浮点数，就必须用科学计数法表示，把10用e替代，1.23x109就是"),s("code",[this._v("1.23e9")]),this._v("，或者"),s("code",[this._v("12.3e8")]),this._v("，0.000012可以写成"),s("code",[this._v("1.2e-5")]),this._v("，等等")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("对于很大或很小的浮点数，就必须用科学计数法表示，把10用e替代，1.23x109就是"),s("code",[this._v("1.23e9")]),this._v("，或者"),s("code",[this._v("12.3e8")]),this._v("，0.000012可以写成"),s("code",[this._v("1.2e-5")]),this._v("，等等")])},function(){var t=this.$createElement,s=this._self._c||t;return s("blockquote",[s("p",[this._v("Python的浮点数也没有大小限制，但是超出一定范围就直接表示为"),s("code",[this._v("inf")]),this._v("（无限大）。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_6-3-字符串"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-字符串"}},[this._v("#")]),this._v(" 6.3.字符串")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("233：我要是用"),s("code",[this._v('" "')]),this._v("写，岂不是暴露我是"),s("code",[this._v("C++")]),this._v("技术栈出身的了，233")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language-python line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-python"}},[s("code",[s("span",{pre:!0,attrs:{class:"token string"}},[this._v("'I\\'m \\\"OK\\\"!'")]),this._v("\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("li",[e("p",[t._v("转义字符"),e("code",[t._v("\\")]),t._v("可以转义很多字符，比如"),e("code",[t._v("\\n")]),t._v("表示换行，"),e("code",[t._v("\\t")]),t._v("表示制表符，字符"),e("code",[t._v("\\")]),t._v("本身也要转义，所以"),e("code",[t._v("\\\\")]),t._v("表示的字符就是"),e("code",[t._v("\\")])])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("r'\\\\\\t\\\\'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\\\\\\t\\\\\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_6-4-布尔值"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_6-4-布尔值"}},[this._v("#")]),this._v(" 6.4.布尔值")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("布尔值可以用"),s("code",[this._v("and")]),this._v("、"),s("code",[this._v("or")]),this._v("和"),s("code",[this._v("not")]),this._v("运算。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_6-5-空值"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_6-5-空值"}},[this._v("#")]),this._v(" 6.5.空值")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("空值是Python里一个特殊的值，用"),e("code",[t._v("None")]),t._v("表示。"),e("code",[t._v("None")]),t._v("不能理解为"),e("code",[t._v("0")]),t._v("，因为"),e("code",[t._v("0")]),t._v("是有意义的，而"),e("code",[t._v("None")]),t._v("是一个特殊的空值。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("blockquote",[s("p",[this._v("此外，Python还提供了列表、字典等多种数据类型，还允许创建自定义数据类型，我们后面会继续讲到。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"变量"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#变量"}},[this._v("#")]),this._v(" 变量")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"变量名》"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#变量名》"}},[this._v("#")]),this._v(" 变量名》")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[e("p",[t._v("变量名必须是大小写英文、数字和"),e("code",[t._v("_")]),t._v("的组合，且不能用数字开头")])]),t._v(" "),e("li",[e("p",[t._v("在Python中，等号"),e("code",[t._v("=")]),t._v("是赋值语句，可以把任意数据类型赋值给变量，同一个变量可以反复赋值，而且可以是不同类型的变量")]),t._v(" "),e("ul",[e("li",[t._v("和静态语言相比，动态语言更灵活，就是这个原因。")])])])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[t._v("a "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("123")]),t._v(" \t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# a是整数")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\na "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ABC'")]),t._v(" \t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# a变为字符串")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 输入")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 123")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# ABC")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"python是动态语言"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#python是动态语言"}},[this._v("#")]),this._v(" Python是动态语言")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("这种变量本身类型不固定的语言称之为"),s("em",[this._v("动态语言")]),this._v("，与之对应的是"),s("em",[this._v("静态语言")]),this._v("。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_7-常量"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_7-常量"}},[this._v("#")]),this._v(" 7.常量")])},function(){var t=this.$createElement,s=this._self._c||t;return s("strong",[this._v("Python根本没有任何机制保证"),s("code",[this._v("PI")]),this._v("不会被改变")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_8-python中2种除法"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_8-python中2种除法"}},[this._v("#")]),this._v(" 8.Python中2种除法")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("最后解释一下整数的除法为什么也是精确的。")]),this._v(" "),s("li",[this._v("在Python中，有两种除法")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"一种除法是-："}},[s("a",{staticClass:"header-anchor",attrs:{href:"#一种除法是-："}},[this._v("#")]),this._v(" 一种除法是"),s("code",[this._v("/")]),this._v("：")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3.3333333333333335")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[s("code",[this._v("/")]),this._v("除法计算结果是浮点数，即使是两个整数恰好整除，结果也是浮点数")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("9")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3.0")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"还有一种除法是-，称为地板除"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#还有一种除法是-，称为地板除"}},[this._v("#")]),this._v(" 还有一种除法是"),s("code",[this._v("//")]),this._v("，称为地板除")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("两个整数的除法仍然是整数：")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("//")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("blockquote",[s("p",[this._v("联想：C语言中floor()函数：向下取整")]),this._v(" "),s("ul",[s("li",[this._v("单词，floor，n.地面；地板，楼层")])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"_9-字符串和编码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_9-字符串和编码"}},[this._v("#")]),this._v(" 9.字符串和编码")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[s("p",[this._v("因此，Unicode字符集应运而生。Unicode把所有语言都统一到一套编码里，这样就不会再有乱码问题了")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("现在，捋一捋ASCII编码和Unicode编码的区别：ASCII编码是1个字节，而Unicode编码通常是2个字节。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("所以，本着节约的精神，又出现了把Unicode编码转化为“可变长编码”的"),s("code",[this._v("UTF-8")]),this._v("编码。UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节，常用的英文字母被编码成1个字节，汉字通常是3个字节，只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符，用UTF-8编码就能节省空间：")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"我们就可以总结一下现在计算机系统通用的字符编码工作方式："}},[s("a",{staticClass:"header-anchor",attrs:{href:"#我们就可以总结一下现在计算机系统通用的字符编码工作方式："}},[this._v("#")]),this._v(" 我们就可以总结一下现在计算机系统通用的字符编码工作方式：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("用记事本编辑的时候，从文件读取的UTF-8字符被转换为Unicode字符到内存里，编辑完成后，保存的时候再把Unicode转换为UTF-8保存到文件：")]),this._v(" "),s("li",[this._v("浏览网页的时候，服务器会把动态生成的Unicode内容转换为UTF-8再传输到浏览器：")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"python的字符串"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#python的字符串"}},[this._v("#")]),this._v(" Python的字符串")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'包含中文的str'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n包含中文的"),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("str")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("对于单个字符的编码，Python提供了"),s("code",[this._v("ord()")]),this._v("函数获取字符的整数表示，"),s("code",[this._v("chr()")]),this._v("函数把编码转换为对应的字符：")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("ord")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'A'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("65")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("ord")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'中'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("20013")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("chr")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("66")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'B'")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("chr")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("25991")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'文'")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("Python对"),s("code",[this._v("bytes")]),this._v("类型的数据用带"),s("code",[this._v("b")]),this._v("前缀的单引号或双引号表示：")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v("x = b'ABC'\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("p",[t._v("要注意区分"),e("code",[t._v("'ABC'")]),t._v("和"),e("code",[t._v("b'ABC'")]),t._v("，前者是"),e("code",[t._v("str")]),t._v("，后者虽然内容显示得和前者一样，但"),e("code",[t._v("bytes")]),t._v("的每个字符都只占用一个字节。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"encode和decode函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#encode和decode函数"}},[this._v("#")]),this._v(" encode和decode函数")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("以Unicode表示的"),s("code",[this._v("str")]),this._v("通过"),s("code",[this._v("encode()")]),this._v("方法可以编码为指定的"),s("code",[this._v("bytes")]),this._v("，例如：")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ABC'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("encode"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ascii'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("b'ABC'")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'中文'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("encode"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'utf-8'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'中文'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("encode"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ascii'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\nTraceback "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("most recent call last"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n  File "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"<stdin>"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<")]),t._v("module"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("\nUnicodeEncodeError"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ascii'")]),t._v(" codec can't encode characters "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" position "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" ordinal "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("not")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("range")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("128")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("纯英文的"),e("code",[t._v("str")]),t._v("可以用"),e("code",[t._v("ASCII")]),t._v("编码为"),e("code",[t._v("bytes")]),t._v("，内容是一样的，含有中文的"),e("code",[t._v("str")]),t._v("可以用"),e("code",[t._v("UTF-8")]),t._v("编码为"),e("code",[t._v("bytes")]),t._v("。含有中文的"),e("code",[t._v("str")]),t._v("无法用"),e("code",[t._v("ASCII")]),t._v("编码，因为中文编码的范围超过了"),e("code",[t._v("ASCII")]),t._v("编码的范围，Python会报错。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("在"),s("code",[this._v("bytes")]),this._v("中，无法显示为ASCII字符的字节，用"),s("code",[this._v("\\x##")]),this._v("显示。")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("反过来，如果我们从网络或磁盘上读取了字节流，那么读到的数据就是"),e("code",[t._v("bytes")]),t._v("。要把"),e("code",[t._v("bytes")]),t._v("变为"),e("code",[t._v("str")]),t._v("，就需要用"),e("code",[t._v("decode()")]),t._v("方法：")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("b'ABC'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("decode"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ascii'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ABC'")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("decode"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'utf-8'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'中文'")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("如果"),s("code",[this._v("bytes")]),this._v("中包含无法解码的字节，"),s("code",[this._v("decode()")]),this._v("方法会报错：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v(">>> b'\\xe4\\xb8\\xad\\xff'.decode('utf-8')\nTraceback (most recent call last):\n  ...\nUnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 3: invalid start byte\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br"),s("span",{staticClass:"line-number"},[this._v("4")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("如果"),s("code",[this._v("bytes")]),this._v("中只有一小部分无效的字节，可以传入"),s("code",[this._v("errors='ignore'")]),this._v("忽略错误的字节：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v(">>> b'\\xe4\\xb8\\xad\\xff'.decode('utf-8', errors='ignore')\n'中'\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"len函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#len函数"}},[this._v("#")]),this._v(" len函数")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[t._v("要计算"),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("str")]),t._v("包含多少个字符，可以用"),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("函数：\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'ABC'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'中文'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("p",[e("code",[t._v("len()")]),t._v("函数计算的是"),e("code",[t._v("str")]),t._v("的字符数，如果换成"),e("code",[t._v("bytes")]),t._v("，"),e("code",[t._v("len()")]),t._v("函数就计算字节数：")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("b'ABC'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("6")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'中文'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("encode"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'utf-8'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("6")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[e("p",[t._v("由于Python源代码也是一个文本文件，所以，当你的源代码中包含中文的时候，在保存源代码时，就需要务必指定保存为UTF-8编码。当Python解释器读取源代码时，为了让它按UTF-8编码读取，我们通常在文件开头写上这两行：")]),t._v(" "),e("ul",[e("li",[e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#!/usr/bin/env python3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# -*- coding: utf-8 -*-")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])])])]),t._v(" "),e("li",[e("p",[t._v("第一行注释是为了告诉Linux/OS X系统，这是一个Python可执行程序，Windows系统会忽略这个注释；")])]),t._v(" "),e("li",[e("p",[t._v("第二行注释是为了告诉Python解释器，按照UTF-8编码读取源代码，否则，你在源代码中写的中文输出可能会有乱码。")])]),t._v(" "),e("li",[e("p",[t._v("申明了UTF-8编码并不意味着你的"),e("code",[t._v(".py")]),t._v("文件就是UTF-8编码的，必须并且要确保文本编辑器正在使用UTF-8 without BOM编码")])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"格式化"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#格式化"}},[this._v("#")]),this._v(" 格式化")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("但是用法和C语言有点不一样")]),t._v(" "),e("li",[t._v("顺序要对应好。如果只有一个"),e("code",[t._v("%d")]),t._v("、"),e("code",[t._v("%f")]),t._v("啥的，括号可以省略。")]),t._v(" "),e("li",[t._v("如果你不太确定应该用什么，"),e("code",[t._v("%s")]),t._v("永远起作用，它会把任何数据类型转换为字符串：\n"),e("ul",[e("li",[t._v("有些时候，字符串里面的"),e("code",[t._v("%")]),t._v("是一个普通字符怎么办？这个时候就需要转义，用"),e("code",[t._v("%%")]),t._v("来表示一个"),e("code",[t._v("%")]),t._v("：")])])])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Hello, %s'")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'world'")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Hello, world'")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Hi, %s, you have $%d.'")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Michael'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000000")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Hi, Michael, you have $1000000.'")]),t._v("\n\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%s 222"')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1111"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#输出1111 222")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[t._v("a"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("15")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%d + %x "')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("a"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#输出15 + f")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("table",[e("thead",[e("tr",[e("th",{staticStyle:{"text-align":"left"}},[t._v("占位符")]),t._v(" "),e("th",{staticStyle:{"text-align":"left"}},[t._v("替换内容")])])]),t._v(" "),e("tbody",[e("tr",[e("td",{staticStyle:{"text-align":"left"}},[t._v("%d")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("整数")])]),t._v(" "),e("tr",[e("td",{staticStyle:{"text-align":"left"}},[t._v("%f")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("浮点数")])]),t._v(" "),e("tr",[e("td",{staticStyle:{"text-align":"left"}},[t._v("%s")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("字符串")])]),t._v(" "),e("tr",[e("td",{staticStyle:{"text-align":"left"}},[t._v("%x")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("十六进制整数")])])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"format-「我不打算用这个」"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#format-「我不打算用这个」"}},[this._v("#")]),this._v(" format()「我不打算用这个」")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("另一种格式化字符串的方法是使用字符串的"),s("code",[this._v("format()")]),this._v("方法，它会用传入的参数依次替换字符串内的占位符"),s("code",[this._v("{0}")]),this._v("、"),s("code",[this._v("{1}")]),this._v("……，不过这种方式写起来比%要麻烦得多：")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Hello, {0}, 成绩提升了 {1:.1f}%'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("format")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'小明'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("17.125")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Hello, 小明, 成绩提升了 17.1%'")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"f-string「我也还是不常用这个」"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#f-string「我也还是不常用这个」"}},[this._v("#")]),this._v(" f-string「我也还是不常用这个」")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("最后一种格式化字符串的方法是使用以"),s("code",[this._v("f")]),this._v("开头的字符串，称之为"),s("code",[this._v("f-string")]),this._v("，它和普通字符串不同之处在于，字符串如果包含"),s("code",[this._v("{xxx}")]),this._v("，就会以对应的变量替换：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v(">>> r = 2.5\n>>> s = 3.14 * r ** 2\n>>> print(f'The area of a circle with radius {r} is {s:.2f}')\nThe area of a circle with radius 2.5 is 19.62\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br"),s("span",{staticClass:"line-number"},[this._v("4")]),s("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("p",[t._v("上述代码中，"),e("code",[t._v("{r}")]),t._v("被变量"),e("code",[t._v("r")]),t._v("的值替换，"),e("code",[t._v("{s:.2f}")]),t._v("被变量"),e("code",[t._v("s")]),t._v("的值替换，并且"),e("code",[t._v(":")]),t._v("后面的"),e("code",[t._v(".2f")]),t._v("指定了格式化参数（即保留两位小数），因此，"),e("code",[t._v("{s:.2f}")]),t._v("的替换结果是"),e("code",[t._v("19.62")]),t._v("。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"✅判断和循环"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#✅判断和循环"}},[this._v("#")]),this._v(" ✅判断和循环")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"条件判断"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#条件判断"}},[this._v("#")]),this._v(" 条件判断")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"if语句"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#if语句"}},[this._v("#")]),this._v(" if语句")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[t._v("age "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" age "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("18")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'adult'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("elif")]),t._v(" age "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("6")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'teenager'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("else")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'kid'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"跟其它语言有所区别，python中并没有switch-case语句"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#跟其它语言有所区别，python中并没有switch-case语句"}},[this._v("#")]),this._v(" 跟其它语言有所区别，Python中并没有Switch/Case语句!")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"input函数的纠偏"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#input函数的纠偏"}},[this._v("#")]),this._v(" input函数的纠偏")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("p",[t._v("因为"),e("code",[t._v("input()")]),t._v("返回的数据类型是"),e("code",[t._v("str")]),t._v("，"),e("code",[t._v("str")]),t._v("不能直接和整数比较，必须先把"),e("code",[t._v("str")]),t._v("转换成整数。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[s("p",[this._v("Python提供了"),s("code",[this._v("int()")]),this._v("函数来完成这件事情：")])]),this._v(" "),s("li",[s("p",[this._v("whoway：我觉得这个有点像C++11中的int强制类型转化？")])])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[t._v("s "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("input")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'birth: '")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\nbirth "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("s"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" birth "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'00前'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("else")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'00后'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("再次运行，就可以得到正确地结果。但是，如果输入"),s("code",[this._v("abc")]),this._v("呢？又会得到一个错误信息：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v("Traceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nValueError: invalid literal for int() with base 10: 'abc'\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"循环"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#循环"}},[this._v("#")]),this._v(" 循环")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("Python的循环有两种")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"for-in循环"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#for-in循环"}},[this._v("#")]),this._v(" for...in循环")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("所以"),s("code",[this._v("for x in ...")]),this._v("循环就是把每个元素代入变量"),s("code",[this._v("x")]),this._v("，然后执行缩进块的语句。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("whoway：\n"),s("ul",[s("li",[this._v("这种循环类似C++11中for( auto num : array )的循环")]),this._v(" "),s("li",[this._v("类似Java中的for each循环")])])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[t._v("names "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Michael'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Bob'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Tracy'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" name "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" names"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("幸好Python提供一个"),s("code",[this._v("range()")]),this._v("函数，可以生成一个整数序列")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" x "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("range")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" x\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 输出 3")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 0+0+1+2")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" x "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("6")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("8")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("9")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" x\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#输出55")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"while循环"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#while循环"}},[this._v("#")]),this._v(" while循环")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v("\nn "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("99")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("while")]),t._v(" n "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" n\n    n "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" n "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("sum")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("下面是死循环")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("while")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("True")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"yes"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h4",{attrs:{id:"关键字break和continue"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#关键字break和continue"}},[this._v("#")]),this._v(" 关键字break和continue")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("break在循环中，break语句可以提前退出循环")]),this._v(" "),s("li",[this._v("continue在循环过程中，也可以通过"),s("code",[this._v("continue")]),this._v("语句，跳过当前的这次循环，直接开始下一次循环。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"在-python-中没有-do-while-循环！"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#在-python-中没有-do-while-循环！"}},[this._v("#")]),this._v(" 在 Python 中没有 do..while 循环！")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"✅函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#✅函数"}},[this._v("#")]),this._v(" ✅函数")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("基本上所有的高级语言都支持函数，Python也不例外。Python不但能非常灵活地定义函数，而且本身内置了很多有用的函数，可以直接调用。")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"help函数的使用"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#help函数的使用"}},[this._v("#")]),this._v(" help函数的使用")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("也可以在交互式命令行通过"),s("code",[this._v("help(abs)")]),this._v("查看"),s("code",[this._v("abs")]),this._v("函数的帮助信息。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"数据类型转换"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#数据类型转换"}},[this._v("#")]),this._v(" 数据类型转换")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("Python内置的常用函数还包括数据类型转换函数，比如"),s("code",[this._v("int()")]),this._v("函数可以把其他数据类型转换为整数：")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v(">>> int('123')\n123\n>>> int(12.34)\n12\n>>> float('12.34')\n12.34\n>>> str(1.23)\n'1.23'\n>>> str(100)\n'100'\n>>> bool(1)\nTrue\n>>> bool('')\nFalse\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br"),e("span",{staticClass:"line-number"},[t._v("9")]),e("br"),e("span",{staticClass:"line-number"},[t._v("10")]),e("br"),e("span",{staticClass:"line-number"},[t._v("11")]),e("br"),e("span",{staticClass:"line-number"},[t._v("12")]),e("br"),e("span",{staticClass:"line-number"},[t._v("13")]),e("br"),e("span",{staticClass:"line-number"},[t._v("14")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("函数名其实就是指向一个"),s("strong",[this._v("函数对象")]),this._v("的引用，完全可以把函数名赋给一个变量，相当于给这个函数起了一个“别名”：\n"),s("ul",[s("li",[this._v("whoway：这个可以和C++中的「函数对象」类似")])])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v(">>> a = abs # 变量a指向abs函数\n>>> a(-1) # 所以也可以通过a调用abs函数\n1\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_1-定义函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-定义函数"}},[this._v("#")]),this._v(" 1.定义函数")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("在Python中，定义一个函数要使用"),e("code",[t._v("def")]),t._v("语句\n"),e("ul",[e("li",[t._v("依次写出函数名、括号、括号中的参数和冒号"),e("code",[t._v(":")])]),t._v(" "),e("li",[t._v("然后，在缩进块中编写函数体，函数的返回值用"),e("code",[t._v("return")]),t._v("语句返回。")])])])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-python line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-python"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("myabs")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" x "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n        "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" x\n    "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("else")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n        "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("x\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("print")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v(" myabs"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("11")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("在Python交互环境中定义函数时，注意Python会出现"),s("code",[this._v("...")]),this._v("的提示。函数定义结束后需要按两次回车重新回到"),s("code",[this._v(">>>")]),this._v("提示符下：")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h4",{attrs:{id:"_1-1-空函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-空函数"}},[this._v("#")]),this._v(" 1.1.空函数")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("如果想定义一个什么事也不做的空函数，可以用"),s("code",[this._v("pass")]),this._v("语句：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v("def nop():\n    pass\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[s("code",[this._v("pass")]),this._v("语句什么都不做，那有什么用？实际上"),s("code",[this._v("pass")]),this._v("可以用来作为占位符，比如现在还没想好怎么写函数的代码，就可以先放一个"),s("code",[this._v("pass")]),this._v("，让代码能运行起来。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[s("code",[this._v("pass")]),this._v("还可以用在其他语句里，比如：")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v("if age >= 18:\n    pass\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("缺少了"),s("code",[this._v("pass")]),this._v("，代码运行就会有语法错误。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h4",{attrs:{id:"_1-2-参数检查"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-2-参数检查"}},[this._v("#")]),this._v(" 1.2.参数检查")])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v('>>> my_abs(1, 2)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nTypeError: my_abs() takes 1 positional argument but 2 were given\n')])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br"),s("span",{staticClass:"line-number"},[this._v("4")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("但是如果参数类型不对，Python解释器就无法帮我们检查。试试"),s("code",[this._v("my_abs")]),this._v("和内置函数"),s("code",[this._v("abs")]),this._v("的差别：")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v(">>> my_abs('A')\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"<stdin>\", line 2, in my_abs\nTypeError: unorderable types: str() >= int()\n>>> abs('A')\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nTypeError: bad operand type for abs(): 'str'\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br"),e("span",{staticClass:"line-number"},[t._v("9")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("p",[t._v("当传入了不恰当的参数时，内置函数"),e("code",[t._v("abs")]),t._v("会检查出参数错误，而我们定义的"),e("code",[t._v("my_abs")]),t._v("没有参数检查，会导致"),e("code",[t._v("if")]),t._v("语句出错，出错信息和"),e("code",[t._v("abs")]),t._v("不一样。所以，这个函数定义不够完善。")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("def my_abs(x):\n    if not isinstance(x, (int, float)):\n        raise TypeError('bad operand type')\n    if x >= 0:\n        return x\n    else:\n        return -x\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v('>>> my_abs(\'A\')\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\n  File "<stdin>", line 3, in my_abs\nTypeError: bad operand type\n')])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br"),s("span",{staticClass:"line-number"},[this._v("4")]),s("br"),s("span",{staticClass:"line-number"},[this._v("5")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h4",{attrs:{id:"_1-3-返回多个值"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-返回多个值"}},[this._v("#")]),this._v(" 1.3.返回多个值")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("import math\n\ndef move(x, y, step, angle=0):\n    nx = x + step * math.cos(angle)\n    ny = y - step * math.sin(angle)\n    return nx, ny\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br")])])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("ul",[e("li",[t._v("=="),e("code",[t._v("import math")]),t._v("语句表示导入"),e("code",[t._v("math")]),t._v("包，并允许后续代码引用"),e("code",[t._v("math")]),t._v("包里的"),e("code",[t._v("sin")]),t._v("、"),e("code",[t._v("cos")]),t._v("等函数。==")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v(">>> x, y = move(100, 100, 60, math.pi / 6)\n>>> print(x, y)\n151.96152422706632 70.0\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"language- line-numbers-mode"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[this._v(">>> r = move(100, 100, 60, math.pi / 6)\n>>> print(r)\n(151.96152422706632, 70.0)\n")])]),this._v(" "),s("div",{staticClass:"line-numbers-wrapper"},[s("span",{staticClass:"line-number"},[this._v("1")]),s("br"),s("span",{staticClass:"line-number"},[this._v("2")]),s("br"),s("span",{staticClass:"line-number"},[this._v("3")]),s("br")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("h3",{attrs:{id:"_2-函数的参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-函数的参数"}},[this._v("#")]),this._v(" 2.函数的参数")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("解决递归调用栈溢出的方法是通过"),s("strong",[this._v("尾递归")]),this._v("优化，事实上尾递归和循环的效果是一样的，所以，把循环看成是一种特殊的尾递归函数也是可以的。")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"✅模块"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#✅模块"}},[this._v("#")]),this._v(" ✅模块")])},function(){var t=this.$createElement,s=this._self._c||t;return s("ul",[s("li",[this._v("为了编写可维护的代码，我们把很多函数分组，分别放到不同的文件里，这样，每个文件包含的代码就相对较少，很多编程语言都采用这种组织代码的方式。")]),this._v(" "),s("li",[s("strong",[this._v("在Python中，一个.py文件就称之为一个模块（Module）")]),this._v("。")])])}],!1,null,null,null);s.default=n.exports}}]);