Ext.: http://www.cnblogs.com/dolphin0520/archive/2013/03/13/2954682.html
I. Conditional SELECTION statements
The keywords for conditional selection statements in Python are: If, elif, else three. Its basic form is as follows:
If condition: blockelif Condition: block...else: block
Where the elif and else statement blocks are optional. For if and elif only condition is true, the spoke statement executes only if and all of the Elif's condition are false if the Else branch is executed. Note the differences between conditional selection statements and C in Python, condition in C must be enclosed in parentheses, not in Python, but be aware that there is a colon behind condition.
Here is an example of grade grading:
Score=input () If score<60: print " D" elif score<80: print "C" elif score<90: print "B" Else: Print "A"
Two. Looping statements
Like the C language, Python also provides a For loop and a while loop (there is no do in Python). While loop) two kinds. But the for loop usage in Python is very different from the C language (similar to the for loop usage in Java, C #), while the loop usage is roughly similar to the C language.
The basic form of a For loop is as follows:
For variable in list: block
For example, calculate from 1 plus to 100 and:
sum=0for var in range (1,101): sum+=varprint sum
Range () is a built-in function that can generate a list of numbers in a range. For example, range (1,6) generates a list of [1,2,3,4,5], and a range (8) generates a list of [0,1,2,3,4,5,6,7].
Of course there can be nested loops, for example there is a list list=[' China ', ' England ', ' America ', to traverse the output of each letter.
list=[' China ', ' England ', ' America ']for I in range (len (list)): Word=list[i] for J in range (Len (word)): Print Word[j]
The built-in function Len () can be used not only to calculate the length of a string, but also to find the number of members in a list or collection.
Here's a look at the basic form of the while loop:
While condition: block
The loop is executed only if condition is true. Once the condition is false, the loop terminates.
As an example:
Count=2while count>0: print "I love python!" Count=count-1
If you want to terminate a loop during a block of statements, you can use break or continue. Break is jumping out of the loop, and continue is jumping out of the loop.
Count=5while True: print "I love python!" Count=count-1 if count==2: break
Count=5while count>0: count=count-1 if count==3: continue print "I love python!"
Lastly, the for and while loops in Python can be added to the ELSE clause, and the ELSE clause executes when the entire loop execution condition is not met (this usage is generally less used now). See two examples:
#这两段循环功能完全相同for i in range (0,10): Print ielse: print "Over" for I in range (0,10): print iprint ' over ‘
The following is the while: Else's usage:
#这两段循环功能完全相同count =5while count>0: print ' I love Python ' count=count-1else: print ' over ' count= 5while count>0: print ' I love python ' count=count-1print ' over '
Conditional selection and looping statements in the "Go" python