设为首页 收藏本站
查看: 717|回复: 0

[经验分享] 输出循环《Python for Beginners》学习笔记(4) 输出循环

[复制链接]
YunVN网友  发表于 2017-5-6 10:53:51 |阅读模式
  这两天笔者几篇文章介绍了改输出循环的文章. 关联文章的地址
  《Python for Beginners》为LearnStreet上的Python入门课程。本节重要学习内容为循环语句
  一、基本法语
1. while循环
上面两种写法是等价的:
while flag:
while flag == True:
  2. for循环
for x in range(0,3):
  range(0,3) 行执的有效值范围为[0,1,2], 并且同range(3)是等价的。
  二、程编训练
1. Incrementing and Decrementing

1 def run(first, second):
2     #your code here
3     first +=1
4     second -=1
5     return (first, second)
6
7 #This is just for you to see what happens when the function is called
8 print run(1, 20)

  输出结果:
(2, 19)
  #注意本数函有两个返回值。
  2. while循环训练
数函能功:输出1-10

1 def run():
2     count = 1
3     #your code here
4     while count <=10:
5         print count
6         count +=1
7     return count
8
9 #This is just for you to see what happens when the function is called
10 print run()

  输出结果:
1
2
3
4
5
6
7
8
9
10
11
  思考:为什么会输出11?因为数函以print run()式方停止调用,将输出数函的返回值(值为11),若无返回值则输出None。
  3. for循环训练1
数函能功:算计1-10共10个数的和。

1 def run():
2     sum = 0
3     for i in range(11):
4         #your code here
5         sum +=i
6     return sum
7
8 #This is just for you to see what happens when the function is called
9 print run()

  输出结果:
55
  4. for循环训练2

1 def run():
2     #your loop here
3     for i in range(10):
4         print "#" * i
5
6 #This is just for you to see what happens when the function is called
7 run()

  输出结果:
  #
##
###
####
#####
######
#######
########
#########
  注意:输出结果的第一为行空行
  5. for循环训练3
实现能功:输出全部小于20的奇数,并返回这些数的和。
代码:

1 def run():
2     #your code here
3     sum = 0
4     for i in range(10):
5         print i*2+1
6         sum += i*2+1
7     return sum
8
9 #This is just for you to see what happens when the function is called
10 print run()

  输出结果:
1
3
5
7
9
11
13
15
17
19
100
  6. Iterables

1 def run():
2     """
3     print each letter in the string using the for loop syntax and then return the last character sequence
4     """
5     str = "LearnStreet!"
6     for i in range(len(str)):
7         print str
8     return str
9     
10 #This is just for you to see what happens when the function is called
11 run()

  输出结果:
L
e
a
r
n
S
t
r
e
e
t
!
  注意:如果数函以print run()式方调用,则会输出返回值(值为'!'),若没有return str语句,则返回值输出为None。
  7. 习复(条件语句和循环语句)

1 def checkLen():
2     str = "Ninja Coder!"
3     flag = True
4     while flag:
5         count = 0
6         for i in range(len(str)):
7             count +=1
8             if count == len(str):
9                 flag = False
10                 print "done"
11             else:
12                 print "not yet done"
13
14 #This is just for you to see what happens when the function is called
15 checkLen()

  输出结果:
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
done
  总结:
1. 一个数函可以有多个返回值。
2. Python许允一个字符串与整数n相乘,结果等价于n个字符串联串一同。
3. 注意数函调用时run()与print run()的别区。
  (本文完)
  文章结束给大家分享下程序员的一些笑话语录: 一位程序员去海边游泳,由于水性不佳,游不回岸了,于是他挥着手臂,大声求.救:“F1,F1!”

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373719-1-1.html 上篇帖子: 语句输出《Python for Beginners》学习笔记(3) 语句输出 下篇帖子: SVN Hook + Python实现commit后自动发送邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表