首页 > 代码库 > python学习笔记
python学习笔记
1.if语句(python中没有switch语句,可用if……elif……else代替)
格式:
if 表达式:
#语句
else:
#语句
eg:
number = 23guess = 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:
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>):
#语句
1 for i in range(1,5):2 print i3 else:4 print "The for loop is over"
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。