python学习笔记
1.if语句(python中没有switch语句,可用if……elif……else代替)
格式:
if 表达式:
#语句
else:
#语句
eg:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
number = 23 guess = int(raw_input('Enter an integer:')) if guess == number: print'Congratulations' print'but you do not win any prizes!' elif guess < number: print'No,it is higher' else: print'NO,it is lower' print'Done'
2.while语句
格式:
while <test>:
#语句
else:#可跟else从句
eg:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 number = 23 2 running = True 3 4 while running: 5 guess = int(raw_input('Enter an integer:')) 6 if guess == number: 7 print'Congratulations' 8 running = False 9 elif guess < number: 10 print'it is higher' 11 if guess > number: 12 print'it is lower' 13 else: 14 print'The while loop is over.' 15 16 print'Done'
3.for语句格式:
for <var> in range(<begin>,<end>):
#语句
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 for i in range(1,5): 2 print i 3 else: 4 print "The for loop is over"
posted on 2014-06-20 11:16 shijianupc 阅读(160) 评论(0) 编辑 收藏 举报