前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小朋友学Python(13):循环

小朋友学Python(13):循环

作者头像
海天一树
发布2018-04-17 11:50:37
4570
发布2018-04-17 11:50:37
举报
文章被收录于专栏:海天一树海天一树

一、while循环

例1

代码语言:javascript
复制
count = 0
while (count < 9):
   print 'The count is:', count
   count = count + 1
print "Good bye!"

运行结果:

代码语言:javascript
复制
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

例2 (while…else句式)

代码语言:javascript
复制
count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"

运行结果:

代码语言:javascript
复制
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

二、for循环

例3

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

运行结果:

代码语言:javascript
复制
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!

三、Pass语句

例4

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

运行结果:

代码语言:javascript
复制
当前字母 : P
当前字母 : y
当前字母 : t
这是 pass 块
当前字母 : h
当前字母 : o
当前字母 : n
Good bye!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-02-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 海天一树 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、while循环
    • 例1
      • 例2 (while…else句式)
      • 二、for循环
        • 例3
        • 三、Pass语句
          • 例4
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档