首页 > 代码库 > python学习笔记

python学习笔记

1.if语句(python中没有switch语句,可用if……elif……else代替)

格式:

  if 表达式:

    #语句

  else:

    #语句

eg:

number = 23guess = int(raw_input(Enter an integer:))if guess == number:    printCongratulations    printbut you do not win any prizes!elif guess < number:    printNo,it is higherelse:    printNO,it is lowerprintDone
View Code

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         printCongratulations 8         running = False 9     elif guess < number:10         printit is higher11     if guess > number:12         printit is lower13 else:14     printThe while loop is over.15 16 printDone
View Code

 

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"
View Code