Python和C语言的语法区别(二)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 在python中,<span style="color: black;">倘若</span>循环体中不<span style="color: black;">必须</span><span style="color: black;">运用</span>到自定义的变量,<span style="color: black;">能够</span>将自定义的变量定义为“_”。如:</p># <span style="color: black;">Function</span><span style="color: black;">:Python</span>例程
# <span style="color: black;">Author</span>:老学长谈技术
# <span style="color: black;">Date</span><span style="color: black;">:2021</span>/<span style="color: black;">1</span>/<span style="color: black;">25</span> <span style="color: black;">22</span><span style="color: black;">:17</span>
<span style="color: black;">for</span> <span style="color: black;">_</span> <span style="color: black;">in</span> <span style="color: black;">range</span>(<span style="color: black;">3</span>):
<span style="color: black;">print</span>(<span style="color: black;">我爱Python!</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这句代码将会输出三次:我爱python!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 在python中<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">运用</span>break来结束<span style="color: black;">要求</span>判断和循环结构。<span style="color: black;">然则</span>与C语言<span style="color: black;">区别</span>的是:在python中,while…else语句是合法的,并且<span style="color: black;">能够</span>用break来进行流程<span style="color: black;">掌控</span>。同理,continue<span style="color: black;">亦</span>是<span style="color: black;">同样</span>的<span style="color: black;">运用</span><span style="color: black;">办法</span>。如:</p><span style="color: black;"># Function:Python例程</span>
<span style="color: black;"># Author:老学长谈技术</span>
<span style="color: black;"># Date:2021/1/25 22:20</span>
print(<span style="color: black;">----演示一个while循环代码---</span>)
i=<span style="color: black;">0</span>
<span style="color: black;">while</span> i<<span style="color: black;">3</span>:
<span style="color: black;"><span style="color: black;">要求</span>循环体</span>
pwd=input(<span style="color: black;">请输入<span style="color: black;">秘码</span>:</span>)
<span style="color: black;">if</span> pwd==<span style="color: black;">123456</span>:
<span style="color: black;">break</span>
<span style="color: black;">else</span>:
print(<span style="color: black;"><span style="color: black;">秘码</span>不正确!</span>)
i+=<span style="color: black;">1</span>
<span style="color: black;">else</span>:
print(<span style="color: black;">您的输入次数<span style="color: black;">已然</span><span style="color: black;">运用</span>完!</span>)
运行结果:
C:\Users\dell\PycharmProjects\pythonProject\venv\Scripts\python.exe <span style="color: black;">"E:/Code_Debug/OneDrive - 365vip.plus/Study/Python/pythonProject/Day1/Demo2.py"</span>
----演示一个<span style="color: black;">while</span>循环代码---
请输入<span style="color: black;">秘码</span>:<span style="color: black;">111</span>
<span style="color: black;">秘码</span>不正确!
请输入<span style="color: black;">秘码</span>:<span style="color: black;">111</span>
<span style="color: black;">秘码</span>不正确!
请输入<span style="color: black;">秘码</span>:<span style="color: black;">11</span>
<span style="color: black;">秘码</span>不正确!
您的输入次数<span style="color: black;">已然</span><span style="color: black;">运用</span>完!<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. else语句与C语言中的<span style="color: black;">运用</span><span style="color: black;">办法</span>除了能跟while<span style="color: black;">一块</span>用用以外还能跟for<span style="color: black;">一块</span>用。以下是它的三种<span style="color: black;">状况</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">else</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if.....else:if<span style="color: black;">要求</span>表达式不成立时执行else</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">while....else和for...else:<span style="color: black;">无</span>碰到break时执行到else。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对while的代码进行改造:</p><span style="color: black;">print</span>(<span style="color: black;">----改造while代码---</span>)
<span style="color: black;">for</span> item <span style="color: black;">in</span> range(3):
<span style="color: black;">pwd</span> = input(<span style="color: black;">请输入<span style="color: black;">秘码</span>:</span>)
<span style="color: black;">if</span> <span style="color: black;">pwd</span>==<span style="color: black;">123456</span>:
<span style="color: black;">break</span>
<span style="color: black;">else</span>:
<span style="color: black;">print</span>(<span style="color: black;">您输入的<span style="color: black;">秘码</span>不正确!</span>)
<span style="color: black;">else</span>:
<span style="color: black;">print</span>(<span style="color: black;">您的输入次数<span style="color: black;">已然</span><span style="color: black;">运用</span>完!</span>)
运行结果:
----改造<span style="color: black;">while</span>代码---
请输入<span style="color: black;">秘码</span>:
您输入的<span style="color: black;">秘码</span>不正确!
请输入<span style="color: black;">秘码</span>:111
您输入的<span style="color: black;">秘码</span>不正确!
请输入<span style="color: black;">秘码</span>:123
您输入的<span style="color: black;">秘码</span>不正确!
您的输入次数<span style="color: black;">已然</span><span style="color: black;">运用</span>完!<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">列表<span style="color: black;">实质</span>上不是Python的首创,在C++和Java中均存在列表。<span style="color: black;">由于</span>三者之间都有一个<span style="color: black;">一起</span>点,那<span style="color: black;">便是</span>都是面向对象的语言,<span style="color: black;">然则</span>三者之间各有<span style="color: black;">优良</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在python中,列表相当于是一个容器,它<span style="color: black;">能够</span>装<span style="color: black;">非常多</span>的元素,并且元素的类型<span style="color: black;">能够</span>不尽相同。它相当于C语言中的数组的概念。在C语言中不存在列表。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">列表的操作:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1) 列表的创建:</p><span style="color: black;">运用</span>中括号创建<span style="color: black;">运用</span>内置函数list()函数进行创建<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:在list()内进行<span style="color: black;">运用</span>时,需要加方括号,否则报错。</span></p> <span style="color: black;"># Function:Python例程</span>
<span style="color: black;"># Author:老学长谈技术</span>
<span style="color: black;"># Date:2021/1/25 22:52</span>
<span style="color: black;">#创建列表</span>
Lst1=[<span style="color: black;">1</span>,<span style="color: black;">2</span>,<span style="color: black;">"hello"</span>,<span style="color: black;">"python"</span>]
Lst2=<span style="color: black;">list</span>([<span style="color: black;">1</span>,<span style="color: black;">2</span>,<span style="color: black;">"hello"</span>,<span style="color: black;">"python"</span>])
<span style="color: black;">print</span>(<span style="color: black;">Lst1:</span>,Lst1)<span style="color: black;">print</span>(<span style="color: black;">Lst2:</span>,Lst2)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2) 列表的<span style="color: black;">查找</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Index()函数:</p><span style="color: black;">倘若</span>列表中存在N个相同的元素,只返回相同的元素中的<span style="color: black;">第1</span>个元素的索引。<span style="color: black;">倘若</span><span style="color: black;">查找</span>的元素在列表中不存在,则会抛出ValueError<span style="color: black;">反常</span>。还<span style="color: black;">能够</span>在指定的start和Stop中进行<span style="color: black;">查询</span>。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">获取列表中的单个元素:</p>正向索引到列表中的0到N-1个元素。 如:lst逆向索引从-N到-1. 如lst[-N]指定的索引不存在,则抛出IndexError<span style="color: black;">反常</span><span style="color: black;"># Function:Python例程</span>
<span style="color: black;"># Author:老学长谈技术</span>
<span style="color: black;"># Date:2021/1/25 22:52</span>
<span style="color: black;">#创建列表</span>
Lst1=[<span style="color: black;">1</span>,<span style="color: black;">2</span>,<span style="color: black;">"hello"</span>,<span style="color: black;">"python"</span>]
Lst2=<span style="color: black;">list</span>([<span style="color: black;">1</span>,<span style="color: black;">2</span>,<span style="color: black;">"hello"</span>,<span style="color: black;">"python"</span>])
<span style="color: black;">print</span>(<span style="color: black;">Lst1:</span>,Lst1)
<span style="color: black;">print</span>(<span style="color: black;">Lst2:</span>,Lst2)
<span style="color: black;">print</span>(Lst1.index(<span style="color: black;">1</span>,<span style="color: black;">1</span>,<span style="color: black;">3</span>))
Traceback (most recent call last):
File <span style="color: black;">"E:\Code_Debug\OneDrive - 365vip.plus\Study\Python\pythonProject\Day1\demo3.py"</span>, line <span style="color: black;">13</span>, in <module>
<span style="color: black;">print</span>(Lst1.index(<span style="color: black;">1</span>,<span style="color: black;">1</span>,<span style="color: black;">3</span>))
ValueError: <span style="color: black;">1</span> is not in <span style="color: black;">list</span>Process finished with<span style="color: black;">exit</span> code <span style="color: black;">1</span>
列表的切片:语法格式 列表名。切片的结果是对原列表中的片段进行拷贝。<span style="color: black;">能够</span>用 in <span style="color: black;">或</span>not in 进行判断元素<span style="color: black;">是不是</span>在列表中。<span style="color: black;">或</span>用遍历的方式进行<span style="color: black;">查找</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3) 列表元素的<span style="color: black;">增多</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>:</p>append()函数:在列表的末尾添加一个元素。extend()函数:在列表的末尾添加多个元素。inser()函数:在列表的任意位置添加一个元素。切片:在列表的任意位置添加多个元素。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4) 列表元素的删除</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>:</p>remove()函数:一次删除一个元素;重复的元素只删除<span style="color: black;">第1</span>个元素。元素不存在时会抛出一个ValueError<span style="color: black;">反常</span>。pop()函数:删除一个指定位置上的元素;指定索引不存在时,会抛出一个IndexError<span style="color: black;">反常</span>,不指定索引时会删除列表的最后一个元素。切片:删除多个元素。clear()函数:清空列表del:删除列表。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(5) 列表元素的修改</p>为指定的索引的元素赋予一个新值。为指定的切片赋予一个新的值。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(6) 列表元素的排序</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">元素的排序<span style="color: black;">通常</span>有两种方式,分别是:</p>调用sort()函数进行排序。默认是从小到大排序。<span style="color: black;">能够</span><span style="color: black;">经过</span>sort(revers=true)来进行翻转。调用内置函数sorted()函数进行排序,默认是从大到小排序。<span style="color: black;">能够</span><span style="color: black;">经过</span>sort(revers=true)来进行翻转。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(7) 列表元素的生成公式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语法格式:。其中:i*i<span style="color: black;">暗示</span>的是列表元素的表达式。I是自定义变量,range(1,10)是可迭代变量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:<span style="color: black;">暗示</span>列表元素的表达式中<span style="color: black;">一般</span><span style="color: black;">包含</span>了自定义变量。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">列表的特点:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1) 列表元素<span style="color: black;">根据</span><span style="color: black;">次序</span>有序进行<span style="color: black;">摆列</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2) 索引对应的是<span style="color: black;">独一</span> 一个元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3) 列表内<span style="color: black;">能够</span>重复存储数据,<span style="color: black;">然则</span>在<span style="color: black;">查找</span>时只会<span style="color: black;">查找</span>出<span style="color: black;">第1</span>个。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4) 任意数据类型混存。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(5) 它是<span style="color: black;">按照</span><span style="color: black;">必须</span>动态分配和回收内存的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">文案</span>中的代码只演示了一部分~,<span style="color: black;">爱好</span>的话<span style="color: black;">能够</span>自己尝试一下哦 ~ 。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后续将<span style="color: black;">连续</span>更新,<span style="color: black;">爱好</span>请转发关注多交流哦~</p>
你字句如珍珠,我珍藏这份情。 外链发布论坛学习网络优化SEO。 期待楼主的下一次分享!” 一看到楼主的气势,我就觉得楼主同在社区里灌水。 在遇到你之前,我对人世间是否有真正的圣人是怀疑的。
页:
[1]