文章列表


<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的公共方法。这些方法可以帮助开发人员更高效地编写代码,实现各种功能。下面是一些常用的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;">1. print()</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()是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="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(value, ..., sep=&#39; &#39;, end=&#39;\n&#39;, file=sys.stdout, flush=False)</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;">其中,value是要输出的值,sep是分隔符,默认为一个空格,end是行末字符,默认为一个换行符,file是输出文件对象,默认为sys.stdout,flush是一个布尔值,表示是否立即刷新输出缓冲区,默认为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;">2. input()</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()是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="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(prompt=None)</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;">其中,prompt是提示用户输入的字符串,默认为None。</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;">3. len()</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()是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="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;">len(s)</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;">其中,s是要计算长度的序列对象。</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;">4. range()</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()是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="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;">range(start, stop[, step])</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;">其中,start是序列的起始值,默认为0,stop是序列的终止值,不包括该值,step是序列的步长,默认为1。</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;">5. 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;">list()是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="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(iterable)</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;">其中,iterable是要转换为列表的可迭代对象。</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;">6. 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;">tuple()是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="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;">tuple(iterable)</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;">其中,iterable是要转换为元组的可迭代对象。</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;">7. set()</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;">set()是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="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;">set(iterable)</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;">其中,iterable是要初始化集合的可迭代对象。</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;">8. dict()</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;">dict()是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="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;">dict(**kwarg)</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;">dict(mapping, **kwarg)</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;">dict(iterable, **kwarg)</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;">其中,kwarg是一个关键字参数字典,mapping是一个映射对象,iterable是一个包含键值对的可迭代对象。</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;">9. sorted()</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;">sorted()是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="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;">sorted(iterable, key=None, reverse=False)</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="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 style="color: rgb(192, 80, 77);">更多Python相关学习,推荐小册:</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;"><br/><strong>Python基础 +&nbsp;Django +&nbsp;爬虫 +&nbsp;文档处理</strong></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;"><a href="https://www.maxiaoke.com/manual/python_jcddypc.html" target="_blank"><strong><img src="/uploads/images/20230218/03c2f5e077486036c3ad1cb49199370a.jpg" title="pyth.jpg" alt=""/></strong></a></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;"><strong><br/></strong></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;"><strong>点此查看:</strong></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;"><a href="https://www.maxiaoke.com/manual/python_jcddypc.html" target="_blank" style="text-decoration: underline; font-size: 18px; color: rgb(192, 80, 77);"><span style="font-size: 18px; color: rgb(192, 80, 77);"><strong>Python基础Django与爬虫</strong></span></a></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;"><strong><br/></strong></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中的字典(dictionary)是一种无序的数据结构,可以存储键值对。字典的键必须是唯一的,而值可以重复或不唯一。下面是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;">可以使用花括号 {} 或 dict() 函数来创建字典,如下所示:</p><pre class="brush:as3;toolbar:false">my_dict&nbsp;=&nbsp;{} my_dict&nbsp;=&nbsp;dict() my_dict&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;}</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">my_dict&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;} print(my_dict[&#39;name&#39;])&nbsp;&nbsp;#&nbsp;输出&nbsp;John print(my_dict.get(&#39;age&#39;))&nbsp;&nbsp;#&nbsp;输出&nbsp;30</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">my_dict&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;} my_dict[&#39;age&#39;]&nbsp;=&nbsp;40 print(my_dict)&nbsp;&nbsp;#&nbsp;输出&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;40,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;}</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;">可以使用 del 关键字来删除字典中的元素,如下所示:</p><pre class="brush:as3;toolbar:false">my_dict&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;} del&nbsp;my_dict[&#39;age&#39;] print(my_dict)&nbsp;&nbsp;#&nbsp;输出&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;}</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 循环来遍历字典中的元素,如下所示:</p><pre class="brush:as3;toolbar:false">my_dict&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;} for&nbsp;key&nbsp;in&nbsp;my_dict: &nbsp;&nbsp;&nbsp;&nbsp;print(key,&nbsp;my_dict[key]) #&nbsp;输出&nbsp;name&nbsp;John #&nbsp;输出&nbsp;age&nbsp;30 #&nbsp;输出&nbsp;city&nbsp;New&nbsp;York</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;">可以使用 keys() 函数获取字典的键,使用 values() 函数获取字典的值,如下所示:</p><pre class="brush:as3;toolbar:false">my_dict&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;} print(my_dict.keys())&nbsp;&nbsp;#&nbsp;输出&nbsp;dict_keys([&#39;name&#39;,&nbsp;&#39;age&#39;,&nbsp;&#39;city&#39;]) print(my_dict.values())&nbsp;&nbsp;#&nbsp;输出&nbsp;dict_values([&#39;John&#39;,&nbsp;30,&nbsp;&#39;New&nbsp;York&#39;])</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;">可以使用 update() 函数将一个字典的键值对合并到另一个字典中,如下所示:</p><pre class="brush:as3;toolbar:false">my_dict1&nbsp;=&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30} my_dict2&nbsp;=&nbsp;{&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;} my_dict1.update(my_dict2) print(my_dict1)&nbsp;&nbsp;#&nbsp;输出&nbsp;{&#39;name&#39;:&nbsp;&#39;John&#39;,&nbsp;&#39;age&#39;:&nbsp;30,&nbsp;&#39;city&#39;:&nbsp;&#39;New&nbsp;York&#39;}</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="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中的元组(tuple)是一种有序的数据结构,类似于列表,但是元组是不可变的,一旦创建就不能修改。下面是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;">可以使用圆括号 () 或 tuple() 函数来创建元组,如下所示:</p><pre class="brush:as3;toolbar:false">my_tuple&nbsp;=&nbsp;() my_tuple&nbsp;=&nbsp;tuple() my_tuple&nbsp;=&nbsp;(1,&nbsp;2,&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;">访问元组元素</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;">可以使用索引来访问元组中的元素,索引从0开始,如下所示:</p><pre class="brush:as3;toolbar:false">my_tuple&nbsp;=&nbsp;(1,&nbsp;2,&nbsp;3) print(my_tuple[0])&nbsp;&nbsp;#&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;">切片元组</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_tuple&nbsp;=&nbsp;(1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5) print(my_tuple[1:3])&nbsp;&nbsp;#&nbsp;输出&nbsp;(2,&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;">拼接元组</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_tuple1&nbsp;=&nbsp;(1,&nbsp;2,&nbsp;3) my_tuple2&nbsp;=&nbsp;(4,&nbsp;5,&nbsp;6) my_tuple3&nbsp;=&nbsp;my_tuple1&nbsp;+&nbsp;my_tuple2 print(my_tuple3)&nbsp;&nbsp;#&nbsp;输出&nbsp;(1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6)</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">my_tuple&nbsp;=&nbsp;(1,&nbsp;2,&nbsp;3) my_tuple2&nbsp;=&nbsp;my_tuple&nbsp;*&nbsp;2 print(my_tuple2)&nbsp;&nbsp;#&nbsp;输出&nbsp;(1,&nbsp;2,&nbsp;3,&nbsp;1,&nbsp;2,&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;">获取元组长度</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">my_tuple&nbsp;=&nbsp;(1,&nbsp;2,&nbsp;3) print(len(my_tuple))&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;">查找元素</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;">可以使用 in 关键字来查找元素是否存在于元组中,如下所示:</p><pre class="brush:as3;toolbar:false">my_tuple&nbsp;=&nbsp;(1,&nbsp;2,&nbsp;3) print(2&nbsp;in&nbsp;my_tuple)&nbsp;&nbsp;#&nbsp;输出&nbsp;True</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;;">Python</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中的列表(list)是一种有序的数据结构,可以存储任何类型的数据,包括数字、字符串、对象等。下面是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;">可以使用方括号 [] 或 list() 函数来创建列表,如下所示:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[] my_list&nbsp;=&nbsp;list() my_list&nbsp;=&nbsp;[1,&nbsp;2,&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;">访问列表元素</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;">可以使用索引来访问列表中的元素,索引从0开始,如下所示:</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;">my_list = [1, 2, 3]</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(my_list[0])<span class="Apple-converted-space">&nbsp; </span># 输出 1</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">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5] print(my_list[1:3])&nbsp;&nbsp;#&nbsp;输出&nbsp;[2,&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;">修改列表元素</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_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] my_list[0]&nbsp;=&nbsp;0 print(my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;[0,&nbsp;2,&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;">添加元素</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;">可以使用 append() 方法向列表末尾添加元素,如下所示:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] my_list.append(4) print(my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4]</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;">可以使用 insert() 方法在列表的任意位置插入元素,如下所示:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] my_list.insert(1,&nbsp;4) print(my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;4,&nbsp;2,&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;">删除元素</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;">可以使用 remove() 方法删除指定元素,或使用 pop() 方法删除指定位置的元素,如下所示:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] my_list.remove(2)&nbsp;&nbsp;#&nbsp;删除值为2的元素 print(my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;3] my_list.pop(1)&nbsp;&nbsp;#&nbsp;删除索引为1的元素 print(my_list)&nbsp;&nbsp;#&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;">获取列表长度</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">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] print(len(my_list))&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;">查找元素</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;">可以使用 in 关键字来查找元素是否存在于列表中,如下所示:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] print(2&nbsp;in&nbsp;my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;True</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><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 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字符串是一种常用的数据类型,表示由一个或多个字符组成的有序序列。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>