首页 > 百科知识 正文

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

时间:2023-11-19 19:46:50 阅读:76 作者:不愿弃你

学习知多少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程序设计基础》

编辑&排版|百味

审核|闫庆红

版权声明:该问答观点仅代表作者本人。如有侵犯您版权权利请告知 cpumjj@hotmail.com,我们将尽快删除相关内容。