文章列表


<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python字符串是一种常用的数据类型,表示由一个或多个字符组成的有序序列。Python提供了许多操作来处理字符串,包括以下内容:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建字符串</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,可以使用单引号、双引号或三重引号来创建字符串。例如:</p><pre class="brush:as3;toolbar:false">string1&nbsp;=&nbsp;&#39;hello&#39; string2&nbsp;=&nbsp;&quot;world&quot; string3&nbsp;=&nbsp;&quot;&quot;&quot;Hello, World!&quot;&quot;&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在三重引号中创建的字符串可以跨越多行,这在编写多行文本时很有用。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">访问字符串中的字符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用索引来访问字符串中的字符。在Python中,字符串的索引从0开始。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&quot; print(string[0])&nbsp;#输出&quot;h&quot; print(string[1])&nbsp;#输出&quot;e&quot; print(string[-1])&nbsp;#输出&quot;o&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在第二个示例中,我们使用负索引访问字符串中的最后一个字符。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串切片</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">除了使用索引访问单个字符之外,还可以使用切片访问字符串的子字符串。切片由起始索引和结束索引组成,中间用冒号分隔。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&quot; print(string[1:3])&nbsp;#输出&quot;el&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">切片操作返回从起始索引到结束索引之间的子字符串。如果省略了起始索引或结束索引,则将其替换为字符串的开头或结尾。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&quot; print(string[:3])&nbsp;#输出&quot;hel&quot; print(string[1:])&nbsp;#输出&quot;ello&quot; print(string[:])&nbsp;#输出&quot;hello&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串拼接</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用加号运算符将两个字符串拼接在一起。例如:</p><pre class="brush:as3;toolbar:false">string1&nbsp;=&nbsp;&quot;hello&quot; string2&nbsp;=&nbsp;&quot;world&quot; string3&nbsp;=&nbsp;string1&nbsp;+&nbsp;&quot;&nbsp;&quot;&nbsp;+&nbsp;string2 print(string3)&nbsp;#输出&quot;hello&nbsp;world&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串重复</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用乘号运算符重复字符串。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&quot; print(string&nbsp;*&nbsp;3)&nbsp;#输出&quot;hellohellohello&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串长度</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用len()函数获取字符串的长度。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&quot; print(len(string))&nbsp;#输出5</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串查找和替换</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用find()函数在字符串中查找子字符串,并返回第一个匹配的位置。如果未找到子字符串,则返回-1。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&nbsp;world&quot; print(string.find(&quot;world&quot;))&nbsp;#输出6 print(string.find(&quot;python&quot;))&nbsp;#输出-1</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用replace()函数将字符串中的一个子字符串替换为另一个字符串。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&nbsp;world&quot; new_string&nbsp;=&nbsp;string.replace(&quot;world&quot;,&nbsp;&quot;python&quot;) print(new_string)&nbsp;#输出&quot;hello&nbsp;python&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串分割和连接</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用split()函数将字符串分割为子字符串列表。例如:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello&nbsp;world&quot; list&nbsp;=&nbsp;string.split(&quot;&nbsp;&quot;) print(list)&nbsp;#输出[&quot;hello&quot;,&nbsp;&quot;world&quot;]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用join()函数将字符串列表连接为单个字符串。例如:</p><pre class="brush:as3;toolbar:false">list&nbsp;=&nbsp;[&quot;hello&quot;,&nbsp;&quot;world&quot;] string&nbsp;=&nbsp;&quot;&nbsp;&quot;.join(list) print(string)&nbsp;#</pre><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的for循环语句是一种常用的迭代控制结构,它允许我们遍历任意序列(例如字符串、列表、元组等)中的每个元素,并对它们进行操作。for循环语句的一般形式如下:</p><pre class="brush:as3;toolbar:false">for&nbsp;变量&nbsp;in&nbsp;序列: &nbsp;&nbsp;&nbsp;&nbsp;循环体语句</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">其中,变量表示序列中的每个元素,序列可以是任何可迭代对象,包括字符串、列表、元组、字典等。循环体语句是需要重复执行的代码块,它可以包含任意数量的语句,甚至可以嵌套其他的控制结构。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面我们逐一介绍for循环语句的各种应用:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">遍历序列</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">最常见的用途就是遍历序列,下面是一个遍历列表的例子:</p><pre class="brush:as3;toolbar:false">fruits&nbsp;=&nbsp;[&quot;apple&quot;,&nbsp;&quot;banana&quot;,&nbsp;&quot;cherry&quot;] for&nbsp;fruit&nbsp;in&nbsp;fruits: &nbsp;&nbsp;&nbsp;&nbsp;print(fruit)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个例子中,我们定义了一个列表fruits,然后使用for循环遍历其中的每个元素,并将其打印出来。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">遍历字符串</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">由于字符串也是序列类型的对象,因此可以使用for循环遍历其中的每个字符:</p><pre class="brush:as3;toolbar:false">string&nbsp;=&nbsp;&quot;hello,&nbsp;world!&quot; for&nbsp;char&nbsp;in&nbsp;string: &nbsp;&nbsp;&nbsp;&nbsp;print(char)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个例子中,我们遍历了一个字符串,将其中的每个字符逐一打印出来。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">遍历数字序列</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用range()函数生成一个数字序列,然后使用for循环遍历其中的每个数字。例如,下面的代码可以打印出0到4的所有整数:</p><pre class="brush:as3;toolbar:false">for&nbsp;i&nbsp;in&nbsp;range(5): &nbsp;&nbsp;&nbsp;&nbsp;print(i)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">嵌套循环</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">for循环还可以嵌套在其他的for循环或其他的控制结构中,以实现更加复杂的功能。例如,下面的代码可以打印出一个5x5的乘法表:</p><pre class="brush:as3;toolbar:false">for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;6): &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;j&nbsp;in&nbsp;range(1,&nbsp;6): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(i&nbsp;*&nbsp;j,&nbsp;end=&quot;\t&quot;) &nbsp;&nbsp;&nbsp;&nbsp;print()</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个例子中,我们嵌套了两个for循环,一个用于遍历行,一个用于遍历列,然后在内层循环中打印每个元素,并在每行末尾添加一个换行符。外层循环在每行的末尾也打印一个换行符,以使下一行从新的一行开始。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的while循环语句也是一种常用的迭代控制结构,它允许我们在满足某个条件时重复执行某段代码。while循环语句的一般形式如下:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">while 条件表达式:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><span class="Apple-converted-space">&nbsp; &nbsp; </span>循环体语句</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">其中,条件表达式是一个返回布尔值的表达式,当它为True时,循环体语句会被执行。循环体语句可以包含任意数量的语句,甚至可以嵌套其他的控制结构。在每次循环执行完毕后,条件表达式会被重新计算,直到它变为False时循环才会终止。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面我们介绍while循环语句的嵌套应用:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">嵌套while循环</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">while循环可以嵌套在其他的while循环或其他的控制结构中,以实现更加复杂的功能。例如,下面的代码可以打印出一个5x5的乘法表:</p><pre class="brush:as3;toolbar:false">i&nbsp;=&nbsp;1 while&nbsp;i&nbsp;&lt;=&nbsp;5: &nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;j&nbsp;&lt;=&nbsp;5: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(i&nbsp;*&nbsp;j,&nbsp;end=&quot;\t&quot;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;+=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;print() &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;+=&nbsp;1</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个例子中,我们嵌套了两个while循环,一个用于遍历行,一个用于遍历列,然后在内层循环中打印每个元素,并在每行末尾添加一个制表符。外层循环在每行的末尾也打印一个换行符,以使下一行从新的一行开始。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">控制循环流程</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在循环执行过程中,有时需要使用break语句或continue语句来控制循环的流程。其中,break语句可以用于在循环中跳出整个循环,而continue语句可以用于跳过当前迭代并进入下一次迭代。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面是一个嵌套while循环中使用break和continue的例子:</p><pre class="brush:as3;toolbar:false">i&nbsp;=&nbsp;1 while&nbsp;i&nbsp;&lt;=&nbsp;10: &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;==&nbsp;5: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;+=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue &nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;i&nbsp;==&nbsp;8: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break &nbsp;&nbsp;&nbsp;&nbsp;print(i) &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;+=&nbsp;1</pre><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">在这个例子中,我们使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">while</span>循环从<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">1</span>到<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">10</span>遍历数字,但是当<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">i</span>等于<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">5</span>时,我们使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">continue</span>语句跳过当前迭代,进入下一次迭代;当<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">i</span>等于<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">8</span>时,我们使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">break</span>语句跳出整个循环。在每次循环执行完毕后,我们都打印出当前的<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">i</span>的值。注意,当<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">i</span>等于<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">5</span>时,我们不会打印任何值,因为这次循环被跳过了。</p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,if语句可以嵌套,允许在if语句块中嵌套其他if语句块。嵌套的if语句结构与单个if语句的结构类似,只是它们包含在其他if语句块中。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面是一个简单的if嵌套语句的例子:</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;10 y&nbsp;=&nbsp;5 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;x&nbsp;&gt;&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;positive&nbsp;and&nbsp;greater&nbsp;than&nbsp;y&quot;) &nbsp;&nbsp;&nbsp;&nbsp;else: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;negative&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;y&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上面的例子中,我们首先检查x是否大于y。如果x大于y,则进入嵌套的if语句块。在嵌套的if语句块中,我们检查x是否大于0。如果是,则输出“x是正数且大于y”,否则输出“x是负数”。如果x小于或等于y,则不执行嵌套的if语句块,并输出“x小于或等于y”。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,可以嵌套多个if语句块,但是嵌套过多可能会导致代码难以理解和维护。因此,应该尽量避免过度嵌套if语句块,并考虑使用其他控制结构,例如循环或函数。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,elif是if语句的一个可选部分,可以用来测试多个条件。elif在if和else之间使用,用于测试前面的条件不满足时的另一种情况。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">以下是一个简单的示例,演示了如何使用elif:</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;10 y&nbsp;=&nbsp;5 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) elif&nbsp;x&nbsp;==&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;equal&nbsp;to&nbsp;y&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;y&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上面的例子中,首先检查x是否大于y。如果是,则输出“x大于y”。如果x不大于y,则检查x是否等于y。如果x等于y,则输出“x等于y”。否则,输出“x小于y”。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">需要注意的是,在if-elif-else结构中,只有一个代码块会被执行。当if语句的条件为真时,if代码块将被执行,并且elif和else代码块都不会执行。如果if语句的条件为假,则检查下一个elif条件,如果为真,则执行elif代码块,否则继续检查下一个elif条件,直到找到一个为真的条件为止。如果所有的elif条件都为假,则执行else代码块。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面是另一个示例,演示如何在elif语句中测试多个条件:</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;10 y&nbsp;=&nbsp;5 if&nbsp;x&nbsp;&gt;&nbsp;0&nbsp;and&nbsp;y&nbsp;&gt;&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Both&nbsp;x&nbsp;and&nbsp;y&nbsp;are&nbsp;positive&quot;) elif&nbsp;x&nbsp;&gt;&nbsp;0&nbsp;or&nbsp;y&nbsp;&gt;&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;At&nbsp;least&nbsp;one&nbsp;of&nbsp;x&nbsp;and&nbsp;y&nbsp;is&nbsp;positive&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Neither&nbsp;x&nbsp;nor&nbsp;y&nbsp;is&nbsp;positive&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上面的例子中,首先检查x和y是否都大于0。如果是,则输出“x和y都是正数”。如果不是,则检查x和y是否至少有一个大于0。如果至少有一个大于0,则输出“x和y中至少有一个正数”。否则,输出“x和y都不是正数”。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的条件语句和循环语句是控制流程的重要组成部分,可以帮助我们实现不同的逻辑和算法。下面分别介绍一下Python中常见的条件语句和循环语句。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">条件语句</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中常见的条件语句包括if语句和if-else语句。if语句用于测试一个条件,如果条件为真则执行一段代码块,否则不执行。if-else语句在if语句的基础上增加了一个else分支,用于在条件为假时执行另一段代码块。</p><pre class="brush:as3;toolbar:false">#&nbsp;if语句示例 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) #&nbsp;if-else语句示例 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;y&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中还支持使用elif关键字来增加多个条件分支。elif语句在if语句和else语句之间使用,用于测试前面的条件不满足时的另一种情况。</p><pre class="brush:as3;toolbar:false">#&nbsp;if-elif-else语句示例 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) elif&nbsp;x&nbsp;==&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;equal&nbsp;to&nbsp;y&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;y&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">循环语句</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中常见的循环语句包括for循环和while循环。for循环用于遍历一个序列或迭代器中的元素,而while循环用于在条件为真时重复执行一段代码块。</p><pre class="brush:as3;toolbar:false">#&nbsp;for循环示例 for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;11): &nbsp;&nbsp;&nbsp;&nbsp;print(i) #&nbsp;while循环示例 x&nbsp;=&nbsp;10 while&nbsp;x&nbsp;&gt;&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;print(x) &nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;-=&nbsp;1</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中还支持使用break和continue关键字来控制循环的执行。break语句用于在循环中终止执行,而continue语句用于跳过当前迭代并继续执行下一次迭代。</p><pre class="brush:as3;toolbar:false">#&nbsp;break语句示例 for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;11): &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;==&nbsp;5: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break &nbsp;&nbsp;&nbsp;&nbsp;print(i) #&nbsp;continue语句示例 for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;11): &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue &nbsp;&nbsp;&nbsp;&nbsp;print(i)</pre><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的逻辑运算符包括and、or和not,用于连接和操作布尔值。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">and运算符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">and运算符用于连接两个布尔表达式,当两个表达式都为真时,and运算符的结果为真,否则结果为假。如果第一个表达式的结果为假,则不会计算第二个表达式。</p><pre class="brush:as3;toolbar:false">#&nbsp;and运算符示例 x&nbsp;=&nbsp;5 y&nbsp;=&nbsp;10 z&nbsp;=&nbsp;15 if&nbsp;x&nbsp;&lt;&nbsp;y&nbsp;and&nbsp;y&nbsp;&lt;&nbsp;z: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;y&nbsp;is&nbsp;between&nbsp;x&nbsp;and&nbsp;z&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">or运算符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">or运算符用于连接两个布尔表达式,当两个表达式中至少有一个为真时,or运算符的结果为真,否则结果为假。如果第一个表达式的结果为真,则不会计算第二个表达式。</p><pre class="brush:as3;toolbar:false">#&nbsp;or运算符示例 x&nbsp;=&nbsp;5 y&nbsp;=&nbsp;10 z&nbsp;=&nbsp;15 if&nbsp;x&nbsp;&lt;&nbsp;y&nbsp;or&nbsp;y&nbsp;&gt;&nbsp;z: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;y&nbsp;or&nbsp;y&nbsp;is&nbsp;greater&nbsp;than&nbsp;z&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">not运算符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">not运算符用于对一个布尔表达式取反,即如果表达式的结果为真,则not运算符的结果为假,反之亦然。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"># not运算符示例</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;5 y&nbsp;=&nbsp;10 if&nbsp;not&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;not&nbsp;greater&nbsp;than&nbsp;y&quot;)</pre><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">需要注意的是,逻辑运算符在表达式中的优先级低于比较运算符和算术运算符,因此需要使用括号来明确运算的顺序。</p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的输入输出使用内置函数input()和print()来实现。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">input()函数用于从控制台读取用户输入的数据,并将其作为字符串返回。例如:</p><pre class="brush:as3;toolbar:false">name&nbsp;=&nbsp;input(&quot;请输入您的姓名:&quot;) print(&quot;您好,&quot;&nbsp;+&nbsp;name&nbsp;+&nbsp;&quot;!&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">输出:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">请输入您的姓名:Tom 您好,Tom!</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print()函数用于将指定的内容输出到控制台。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print(&quot;Hello, world!&quot;)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">输出:</p><pre class="brush:as3;toolbar:false">Copy&nbsp;code Hello,&nbsp;world!</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,可以使用数学运算符来执行各种数学运算。下面是一些常用的数学运算符:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">加法:+</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">减法:-</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">乘法:*</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">除法:/</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">取模:%</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">取整除://</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">例如:</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;10 y&nbsp;=&nbsp;3 print(x&nbsp;+&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;13 print(x&nbsp;-&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;7 print(x&nbsp;*&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;30 print(x&nbsp;/&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;3.3333333333333335 print(x&nbsp;%&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;1 print(x&nbsp;//&nbsp;y)&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;3</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">还有一些其他的数学函数可以使用,这些函数都是在Python的数学模块(math)中定义的,例如:</p><pre class="brush:as3;toolbar:false">import&nbsp;math print(math.sqrt(16))&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;4.0 print(math.pow(2,&nbsp;3))&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;8.0 print(math.pi)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;3.141592653589793</pre><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python标识符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,标识符是指用于命名变量、函数、类或模块等对象的名称。标识符可以包含字母、数字和下划线(_),但是不能以数字开头。标识符还需要遵循以下规则:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">标识符必须是一个有效的标识符名称,不能与Python中的关键字重复(如if、else、for等)。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">标识符区分大小写,因此myVar和myvar是不同的标识符。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">标识符应该使用驼峰命名法或下划线命名法,以提高代码可读性。例如,my_variable或MyVariable。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面是一些有效的标识符示例:</p><pre class="brush:as3;toolbar:false">my_variable&nbsp;=&nbsp;42 myClass&nbsp;=&nbsp;MyClass() my_function&nbsp;=&nbsp;lambda&nbsp;x:&nbsp;x&nbsp;*&nbsp;2 Python关键字</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的关键字是一组保留的标识符,用于标识语言的关键部分。这些关键字不能用于命名变量、函数、类或模块等对象。以下是Python的关键字列表:</p><pre class="brush:as3;toolbar:false">False,&nbsp;None,&nbsp;True,&nbsp;and,&nbsp;as,&nbsp;assert,&nbsp;async,&nbsp;await,&nbsp;break, &nbsp;class,&nbsp;continue,&nbsp;def,&nbsp;del,&nbsp;elif,&nbsp;else,&nbsp;except,&nbsp;finally,&nbsp; &nbsp;for,&nbsp;from,&nbsp;global,&nbsp;if,&nbsp;import,&nbsp;in,&nbsp;is,&nbsp;lambda,&nbsp;nonlocal, &nbsp;&nbsp;not,&nbsp;or,&nbsp;pass,&nbsp;raise,&nbsp;return,&nbsp;try,&nbsp;while,&nbsp;with,&nbsp;yield</pre><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python是一种解释型的高级编程语言,它的数据类型包括整型、浮点型、字符串、列表、元组、字典和集合等。在这里,我们将详细介绍Python数据类型列表和元组以及它们的转换。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表(List)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表是Python中最常用的数据类型之一。它是一个有序的可变序列,可以容纳多个不同类型的元素。列表用方括号([])表示,其中的元素用逗号隔开。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits&nbsp;=&nbsp;[&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;]</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表中的元素可以是任何类型的数据,包括整型、浮点型、字符串、甚至是其他的列表。列表是可变的,这意味着我们可以随时添加、删除或修改列表中的元素。例如:</p><pre class="brush:as3;toolbar:false">fruits.append(&#39;pear&#39;)&nbsp;&nbsp;#&nbsp;在列表末尾添加元素 fruits.insert(2,&nbsp;&#39;grape&#39;)&nbsp;&nbsp;#&nbsp;在列表的第三个位置插入元素 fruits.remove(&#39;kiwi&#39;)&nbsp;&nbsp;#&nbsp;删除指定的元素 fruits[0]&nbsp;=&nbsp;&#39;pineapple&#39;&nbsp;&nbsp;#&nbsp;修改列表中的元素</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组(Tuple)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组也是一个有序的序列,但它是不可变的,一旦创建就不能再修改。元组用圆括号(())表示,其中的元素用逗号隔开。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits&nbsp;=&nbsp;(&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组的访问方式和列表相同,但是我们不能对元组进行修改。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print(fruits[0])&nbsp;&nbsp;#&nbsp;输出元组中的第一个元素</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits[0]&nbsp;=&nbsp;&#39;pineapple&#39;&nbsp;&nbsp;#&nbsp;报错:元组不支持修改操作</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">由于元组是不可变的,所以它在某些情况下比列表更加安全。例如,在函数返回多个值时,可以使用元组来返回这些值,避免意外地修改这些值。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">转换</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表和元组之间可以相互转换,这可以通过使用list()和tuple()函数来实现。例如,将一个元组转换为列表:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits&nbsp;=&nbsp;(&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruit_list&nbsp;=&nbsp;list(fruits)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print(fruit_list)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">将一个列表转换为元组:</p><pre class="brush:as3;toolbar:false">fruits&nbsp;=&nbsp;[&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;] fruit_tuple&nbsp;=&nbsp;tuple(fruits) print(fruit_tuple)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这些函数对于将数据从一个数据结构转换为另一个数据结构非常有用,特别是在处理不同类型的数据时。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 2px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 17px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><p><br/></p>