小朋友学Python(13):循环

网友投稿 640 2022-11-11

小朋友学Python(13):循环

小朋友学Python(13):循环

一、while循环

例1

count = 0while (count < 9): print 'The count is:', count count = count + 1print "Good bye!"

运行结果:

The count is: 0The count is: 1The count is: 2The count is: 3The count is: 4The count is: 5The count is: 6The count is: 7The count is: 8

例2 (while…else句式)

count = 0while count < 5: print count, " is less than 5" count = count + 1else: print count, " is not less than 5"

运行结果:

0 is less than 51 is less than 52 is less than 53 is less than 54 is less than 55 is not less than 5

二、for循环

例3

# -*- coding: UTF-8 -*-for letter in 'Python': # 第一个实例 print '当前字母 :', letterfruits = ['banana', 'apple', 'mango']for fruit in fruits: # 第二个实例 print '当前水果 :', fruitprint "Good bye!"

运行结果:

当前字母 : P当前字母 : y当前字母 : t当前字母 : h当前字母 : o当前字母 : n当前水果 : banana当前水果 : apple当前水果 : mangoGood bye!

三、Pass语句

例4

# -*- coding: UTF-8 -*- # 输出 Python 的每个字母for letter in 'Python': if letter == 'h': pass print '这是 pass 块' print '当前字母 :', letterprint "Good bye!"

运行结果:

当前字母 : P当前字母 : y当前字母 : t这是 pass

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小朋友学C语言(32):求圆周率
下一篇:MyBatisPlus代码生成器的使用示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~