Conditions and loops
Conditional execution:
Name = Raw_input (' What's your name? ' If Name.endswith (' Gumby '):p rint ' Hello, Mr.gumby '
What is your name? Gumbyhello, Mr.gumby
Name = Raw_input (' What's your name? ' If Name.endswith (' Gumby '):p rint ' Hello, mr.gumby ' else:print ' Hello, stranger '
Multiple conditions:
num = input (' Enter a number: ') if num > 0:print ' The number is positive ' elif num < 0:print ' The number is negative ' E Lse:print ' The number is zero '
Enter a number:5the number is positive
Enter A number: -1the number is negative
Enter a number:0the number is zero
While loop:
x = 1while X<=100:print xx+=1
For loop:
numbers = [0,1,2,3,4,5,6,7,8,9]for number in Numbers:print number
0123456789
else in the loop:
For n in range (99,81,-1): root = sqrt (n) if root = = Int (root):p rint nbreakelse: #只在没有调用break时执行print "didn ' t find it!"
Beginning Python from Novice to Professional (5)-conditions and loops