python有哪几个循环结构(学习知多少python篇)

python有哪几个循环结构(学习知多少python篇)(1)

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard学苑。今天小编为大家带来“学习知多少(python篇):循环结构”,欢迎您的访问。

Share interests, spread happiness, increase knowledge, and leave a good legacy!

Dear you, this is The LearningYard Academy. Today Xiaobian brings you "Learn how much to know (python):Circular structure”, welcome your visit.

在代码编写中,我们使用最多的就是选择结构和循环结构,接下来,小编为大家浅浅的介绍一下python循环结构。

In code writing, we use the most is to select the structure and loop structure, next, Xiaobian for you to introduce the Python loop structure in a shallow way.

1、for······in······

格式:

for参数in循环体:

pass

上述格式中,可以做循环体的内容有很多,如元组、列表、字符串等等。只要可以遍历、可循环的的内容均可作为循环体存在。其中的参数,主要是用来存放每次循环体送来的单个元素,实现循环的作用。在实际使用中,常和if判断语句联合使用。

1、for······ in······

Format:

For parameter in loop body:

pass

In the above format, there are many contents that can be used as loop bodies, such as tuples, lists, strings, and so on. Content that can be traversed and loopable can exist as a loop. The parameters are mainly used to store the individual elements sent by each cycle body to realize the role of the cycle. In actual use, it is often used in conjunction with the if judgment statement.

2、while

格式:

i == 0

while i >= 10:

pass

i = 1

2、while

Format:

i == 0

while i >= 10:

pass

i = 1

3、break和continue

break的意思是:结束当前循环。

continue意思S是:结束当前循环进入下一个循环。

3. Break and continue

break means: end the current loop.

Continue means S to end the current loop and move on to the next loop.

4、案例

4. Case

(1)判断101-200之间有多少个素数,并输出所有素数。

python有哪几个循环结构(学习知多少python篇)(2)

(1) Determine how many prime numbers are between 101-200 and output all prime numbers.

(2)古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

python有哪几个循环结构(学习知多少python篇)(3)

(2) Classical question: There is a pair of rabbits, from the third month after birth to give birth to a pair of rabbits every month, the little rabbit grows to the third month after the birth of a pair of rabbits every month, if the rabbits do not die, ask what is the total number of rabbits per month?

今天的分享就到这里了。如果您对今天的文章有独特的想法,欢迎给我们留言,让我们相约明天。祝您今天过得开心快乐!

That's all for today's sharing.If you have a unique idea for today’s article,please leave us a message,and let us meet tomorrow.I wish you a happy day !

本文由learningyard新学苑原创,如有侵权,请联系我们!

翻译来源于谷歌翻译

部分来源于

百度文库

清华大学出版 董付国《Python程序设计基础》

编辑&排版|百味

审核|闫庆红

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页