码迷,mamicode.com
首页 > 编程语言 > 详细

python笔记001——条件和循环

时间:2015-10-28 01:14:37      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

if循环:两种选择

例1:

1 a=42
2 if a<=10:
3    print(the number less than 10)
4 else:
5    print(thank you!)

例2:    

while循环:重复某个功能多次

例1

1 greetings=1
2 while greetings<=3:    #这里记住了是<=而不是<-,不要和R混淆了
3    print(hello!*greetings)
4    greetings=greetings+1
[root@localhost ~]# python 2.py
hello!
hello!hello!
hello!hello!hello!

例2                                  

for循环:遍历列表中的每一个元素

例1

1 names=[a,b,c]
2 for name in names:
3   print(hello!+name)
[root@localhost ~]# python 1.py
hello!a
hello!b
hello!c                                    

例2

1 n=10
2 for i in range(n):
3   print(i)

题1:a=100,b=200,求a,b之间所有奇数的和

 

python笔记001——条件和循环

标签:

原文地址:http://www.cnblogs.com/wujiadong2014/p/4915966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!