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

Python中的判断、循环 if...else,while

时间:2014-07-03 07:01:19      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:使用   strong   for   python   字符串   re   

if...else语句:

a=3; b=3; 
if a == b :
print(a,b)
elif a <= b :
print(str(a) + " is less than " + str(b))
else :
print(str(a) + " is greater than " + str(b))

###################################

n = 3
if (n >= 0 and n <= 8) or (n >= 12 and n <= 25): 
print("hhh")

##或者 

n = 3
if (n >= 0 and n <= 8) or (n >= 12 and n <= 25): print("hhh")

 

一个字符串也相当于是一个数组,通过for循环可以将每个字符输出

for循环,相当于foreach
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
for p in pa:
print("current p is:",p)

使用下标索引:
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
for index in range(len(pa)):
print("current p is:",pa[index])

或者:
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
for index in range(2,4):
print("current p is:",pa[index])


使用while循环:
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
i=0
while(i<len(pa)):
print("current p is:",pa[i])
i=i+1


while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环

Python中的判断、循环 if...else,while,布布扣,bubuko.com

Python中的判断、循环 if...else,while

标签:使用   strong   for   python   字符串   re   

原文地址:http://www.cnblogs.com/dreamer-fish/p/3818408.html

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