本文实例讲述了Python中for循环控制语句用法。分享给大家供大家参考。具体分析如下:
第一个:求 50 - 100 之间的质数
import math for i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: break else: print i
输出如下:
53 59 61 67 71 73 79 83 89 97
第二个:把else的位置与if处于同一缩进。
import math for i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: break else: print i
第三个:在else后加一个break语句。
import math for i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: break else: print i break
说明:
for语句是python中的循环控制语句。可用来遍历某一对象,还具有一个附带的可选的else块,主要用于处理for语句中包含的break语句。
如果for循环未被break终止,则执行else块中的语句。
break 在需要时终止for循环
continue 跳过位于其后的语句,开始下一轮循环。
for语句的格式如下:
>>>for in :
... if :
... break
... if :
... continue
...
...else:
...
...
关于第一个程序
在这里,我解释一下为何导入math模块:导入math模块就是为了开方。
如果导入了math模块,然后对 i 进行开方,可以减少运算次数。
求一个数是否质数。只需对它进行这样的运算:
将这个数n,循环与 2 到 这个n的开平方 进行相除
如果这个区间内的所有整数不能整除n,则n为质数。
这样,就节省了运算 ‘大于n的开平方 小于n' 之间这段运算的时间。
第二,我解释一下那‘+1':
int(math.sqrt(i)) 输出的是比 i的开平方 小 的最大整数。
比如说:math.sqrt(51) 结果比7大一点,而 int(math.sqrt(51)) 输出的是7
而且在range(m, n)这里,range()函数产生的是一个从 m至n-1的整数列表,因而需要‘+1',使运算完整。
顺便提一下range()函数。
range([start,] stop [, step])
# start 可选参数,起始数
#stop 终止数,如果 range 只有一个参数x,则产生一个包含 0 至 x-1 的整数列表
#step 可选参数,步长
第二个程序
else那行不对,如果else放在那个地方的话,一旦有某个数遇到不能整除自己的数,就会输出i,直道找到一个整除自己等于0的数。那样就会连续输出这个数。
例如:i = 77,他不是质数,但是也会连续输出5次77,懂不?
只不过,只是自己不明白当else与if位于同一缩进的话,它是怎样运行的。
你解释得很详细,用‘茅塞顿开'来形容一点都不过分。
而且,我必觉得画图是理解循环一个非常好的办法。
希望本文所述对大家的Python程序设计有所帮助。

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具