One, the Python statement
Python statements are broadly divided into: sequential execution statements, conditional execution statements, loop execution statements.
Statements run individually
Python executes statements from a nested block in a file from beginning to end
Automatic boundary detection for blocks and statements
Python does not have or \ begin/end "Split characters
Python uses the statement indentation under the first line to combine statements within nested blocks, with no semicolon ending, and the end of a line is usually the end of the statement
Compound statement, first line + ":" + Indent statement
The compound statements in Python follow the same format, with the first line terminating with a colon, followed by one or more nested statements, usually indented at the first line, indented as a block, or as a group, where the elif and else clauses are part of the IF statement and the first row of the nested block itself.
Blank lines, spaces, and comments are usually ignored
The document string is ignored, but is saved and displayed by the tool
Python code block
Python automatically indents to the boundary, that is, the left blank of the program code, and indents to the right distance belong to the unified block code
Python statement delimiter
If you use syntax brackets, the statement can span multiple lines
For I in range: print I
If the statement ends with a backslash, it can span multiple lines
print ' Python is good '
String constants have special rules ' "" or "'"
Print "He s a teacher"
Second, if statement
If statement format:
An IF condition expression:
Code Block 1
elif conditional expression:
Code Block 2
elif conditional expression:
Code block 3
Else
Code Block 4
The If statement consists of three parts: the keyword itself, the conditional expression used to determine true and false, and the code block (keyword, conditional expression, code block) that executes when the expression is true or nonzero
A conditional expression used to determine true or false, which can be one or more, or a multiple expression.
A block of code for a compound statement, such as an IF clause, while or for loop, contains only one line of code, which can be entered in the same line
Elif is a Python else-if statement that checks if multiple expressions are true and executes code in a particular block of code when it is true.
Like else, the Elif declaration is optional, but the difference is that there can be at most one else statement after the IF statement, but there may be any number of elif statements.
A=87if a>=90:print ' A ' elif a>=80:print ' B ' elif a>=70:print ' C ' elif a>=60:print ' D ' Else:pri NT ' E '
Execution results are
B
Third, while Loop statement
While statement format:
while (conditional expression):
code block
While the fact is that it is a conditional loop statement. If the condition after the if is true, the corresponding block of code is executed once, compared to the if declaration. While the block of code in the while loop executes until the loop condition is no longer true
Use the while loop because it is possible that the conditional expression will never be a Boolean false. So the loop will never end
The code block clause of the while loop is executed until the conditional expression value is Boolean false. This type of looping mechanism is often used in counting loops, these "infinite" loops are not necessarily bad, and many of the client/server systems of a communications Server are working through it.
Sum=0i=0while (i<100): sum+=i i+=1print sum
Execution results are
4950
Four, for loop statements
For Loop statement format:
For iteration variable in range (Start,end,step):
Loop body
For iteration variable in xrange (start,end,step):
Loop body
For Each loop, the iteration variable is set to iterate over the current element of the object (sequence, iterator, or other object that supports iterations), which is provided to the Loop body statement block using
Range () differs from xrange (): Each time a range is executed, the system reads all the numbers in the range and then performs other actions, and when the program executes to xrange, the system directly finds the values needed in xrange. Therefore, xrange is faster than range, avoid using range
Print range (1,10,2) print xrange (1,10,2)
Execution results are
[1, 3, 5, 7, 9]xrange (1, 11, 2)
For provides the most powerful looping structure in Python. It can traverse the sequence members, which can be used in list parsing and generator expressions, and it will automatically invoke the next () method of the iterator, capturing the stopiteration exception and ending the loop (all this is happening internally)
For is one of the most common looping methods in Python
Using the For loop accesses the iterator and accesses the sequence in the same way. The only difference is that the for statement will do something extra for you. Iterators do not represent a collection of circular entries.
The iterator object has a next () method that returns the next entry after the call. After all the entries have been iterated, the iterator throws a stopiteration exception telling the program that the loop ends. The For statement internally calls next () and catches the exception.
Sum=0for i in range: Sum+=iprint sum
Execution results are
4950
Four, break, continue, pass, Else statement
1.break statements
The break statement in Python can end the current loop and then jump to the next statement, similar to the traditional break in C. Commonly used when an external condition is triggered (usually checked by an if statement), When you need to exit from the loop immediately. The break statement can be used in the while and for loops to interrupt the iteration of the list. The purpose is to find the target element in the list, delete it from the database if found, and then exit the loop
For I in Xrange (1,100): If i%4==0:break print I
Execution results are
123
2.continue statements
The continue statement in Python is no different from the traditional continue in other high-level languages. It can be used in while and for loops. While loops are conditional, and for loops are iterative, so continue There are some prerequisites to meet before starting the next loop, otherwise the loop ends normally when the continue statement is encountered, the program terminates the current loop, ignores the remaining statements, and then returns to the top of the loop.
Continue in a while conditional loop statement (writes the self-increment variable before the Continue statement)
I=1while (i<5): i+=1 If i==3:continue print I
Execution results are
245
Continue in a while conditional loop statement (after writing the self-increment variable to the Continue statement)
I=1while (i<5): If i==3:continue print i i+=1
Execution results are
12
As can be seen, after printing out 1 and 2, the program will fall into a dead loop, so you should pay more attention when using continue in a conditional loop statement.
Continue in a For Condition Loop statement
For I in xrange (1,5): If i==3:continue print I
Execution results are
124
3.pass statements
Python does not use the traditional curly braces to mark blocks of code, some of which require code in syntax, and Python does not have an empty curly brace or semicolon (;) to denote "do nothing" in C), and if you do not write any statements where you need the block of the child statement, the interpreter will prompt for a syntax error.
Python provides the pass statement, which does nothing-that is, NOP, (no operation, no operation) borrows this concept from assembly language
Pass can also be used as a development tip to mark the code you want to complete later
For I in xrange (1,5): If i%4==0:pass print I
Execution results are
1234
4.else statements
You can use the Else statement in the while and for loops
The Else statement is used in the while and for loops. How do they work? When used in a loop, the ELSE clause executes only after the loop completes, meaning that the break statement also skips the Else block
For I in xrange (1,5): Print Ielse:print ' This is Else statement '
Execution results are
1234 This is the Else statement
For I in xrange (1,5): If i%2==0:break print Ielse:print ' This is Else statement '
Execution results are
1
This article is from the "Raffaele" blog, make sure to keep this source http://raffaele.blog.51cto.com/6508076/1569009
Python Learning Notes 2-python statements (order, selection, loop)