Directory:
1 Control structure ...
1.1 Branch Statement ...
1.1.1 Nesting of If statements ...
1.2 For Loop ...
1.2.1 Control statements in the Python loop ...
1.3 While loop statement ...
1.3.1 Example ...
1 Control structure
Python support for three different control structures: if , for and the while , does not support C in the language Switch statement.
1.1 Branch Statements
Format:
If EXPRESSION1:
STATEMENT1
Elif EXPRESSION2:
STATEMENT2
Else
STATEMENT3
If < conditions >:
<if Blocks >
else:
<else Blocks >
Description: < conditions > no parentheses are required, such as A==b , but there is a colon that is not less ": " ;
Else There is also a colon after ": ".
<if Blocks > , <else Blocks > to write in indented format, because Python , the same amount of indentation is the same block. This is not the same as a pair of curly braces {} in the C language .
if statement is used to test a condition, If < conditions > to be true, we run a piece of statement (called if- Block ), otherwise we deal with another piece of statement (called else- Block ). The Else clause is optional.
Example 1
Score=int (input ())
If score<60:
Print ("D")
Elif score<80:
Print ("C")
Elif score<90:
Print ("B")
Else
Print ("A")
Example 2
# if elif else Statement
score = Int (input ("score:"))
if (score >=) and (Score <= 100):
Print ("A")
Elif (Score >=) and (Score < 90):
Print ("B")
Elif (Score >=) and (Score < 80):
Print ("C")
Else
Print ("D")
1.1.1 Nesting of IF statements
When you write conditional statements, you should avoid using nested statements whenever possible. Nested statements are not easy to read, and some possibilities may be overlooked.
Example:
x =-1
y = 99
if (x >= 0):
if (x > 0): # Nested IF Statement
y = 1
Else
y = 0
Else
y =-1
Print ("y =", y)
How to implement a switch-like statement function:
http://blog.csdn.net/lynn_yan/article/details/5464911
1.2 For Loop
Format:
For < loop variable > in < sequence >:
< Circulation Body >
Example 1 :
MyList = "for statement"
For CC in MyList:
Print (CC)
Else: # Final Execution
Print ("End list")
Example 2:
#!/usr/bin/python for the ' Python ': # first Example print ' Current letter: ', letter fruits = [' banana ', ' Apple ', ' mango ']for fruit in fruits: # Second Example print ' current fruit: ', fruit print ' good bye! '
1.2.1 iterating through a sequence index
Another way to perform a loop is through an index, as in the following example:
#!/usr/bin/python
Fruits = [' banana ', ' apple ', ' Mango ']
For index in range (len (fruits)):
Print (' Current fruit: ', fruits[index])
Print ("Good bye!")
The result of the above example output:
Current Fruit:banana
Current Fruit:apple
Current Fruit:mango
Good bye!
we used the built-in function for the above example Len () and the range (), function Len () returns the length of the list, which is the number of elements. Range Returns the number of a sequence.
function range () is a built-in function: Generates a list of numbers in a range.
For example:
Range (1,6) will generate [1,2,3,4,5] such a list,
Range (8) will generate [0,1,2,3,4,5,6,7] such a list.
Example:
For I in Range (1,5,1):
Print (i)
It recursively iterates over a sequence of objects, using each item in the queue one at a time to execute the loop body for each item.
The format used in the application is
for < loop variable > in range (N1, N2, N3):
< Circulation Body >
among them, N1 represents the starting value, N2 represents the terminating value, N3 represents the stride size. < loop variable > Start from N1 , Interval N3, value to n2-1 , execute < Circulation body >.
Of course there can be nested loops, for example there is a list list=[' China ', ' England ', ' America '] , to traverse each letter of the output.
Realize:
list=[' China ', ' England ', ' America ']
For I in range (len (list)):
Word=list[i]
For j in range (Len (word)):
Print (Word[j])
For example:
For I in range (1, 10,3):
Print (i)
Print (' For loop was over ')
The result is:
C:\python31>python for.py
1
4
7
The For loop are over
in this program, the number of a sequence is printed, and this sequence is used in the built-in range function to generate the. For example, range (1,5) gives the sequence [1, 2, 3, 4]. By default, range has a step of 1. Range (1,10,3) gives [1,4,7]. Range extends up to the second number, but does not contain the second number.
for Loops within this range recursion--for I in range (1,10,3) equivalent to For i in [1, 4, 7] , which is like assigning each number (or object) in a sequence to I , and then once for each I The value of the execution of this program block.
finally add a little, Python in the for and the while Loops all can be added Else clauses , Else clauses executes when the entire loop execution condition is not met (this usage is generally less used now).
# these two loops function exactly the same
Count=5
While count>0:
Print (' I love Python ')
Count=count-1
Else
Print (' over ')
Count=5
While count>0:
Print (' I love Python ')
Count=count-1
Print (' over ')
1.2.2 Control statements in the Python loop
① Break
Break Statement and C language, jump out of the nearest for or while loops.
② Continue
Continue The statement is also from C language-Borrowed , it terminates the next iteration of the current iteration and loops.
③ Pass
Pass statement does nothing, it only requires a statement in syntax, but the program does not need any action when it is used. the pass statement is designed to maintain the integrity of the program structure.
④ Else clauses
in a loop statement, you can also use the Else clause, Else clause at the end of the sequence traversal ( for statement) or the loop condition is False ( while statement) is executed, but the loop is Break not executed upon termination. As shown below:
例子:
# Loop End Execution Else clauses
For I in range (2, 11):
Print (i)
Else
Print (' For statement are over. ')
# be break no else is executed on termination clauses
For I in range (5):
if (i = = 4):
Break
Else
Print (i)
Else
print (' For statement are over ') # does not output
1.3 While Loop statement
While < conditions :
< Loop Body >
Note ,< conditions > there is a colon after the,< Circulation Body > the format in which to use indentation. the function of the while statement is that when the party < condition > is established (true), the loop body is executed, and then the < condition is checked again >, If it is set up, the loop body is executed again until < condition > is no longer established and then executes the subsequent program.
1.3.1 Example
A = 0
While a < 5:
A = a + 1
Print (a)
Else
Print ("A ' s value is five")
Example:
Count=5
While count>0:
Count=count-1
If count==3:
Continue
Print ("I love python!")
>>>
I love python!
I love python!
I love python!
I love python!
python_02-Control Statements