1. Conditional Judgment
1.If...else
If judging condition:
EXECUTE statement ...
Else
EXECUTE statement ...
2.If...elif: python does not support switch statements
example:num = 5
if num = = 3: # To determine the value of num
print ' Boss '
elif num = = 2:
print ' user '
elif num = = 1:
print ' worker '
Elif num < 0: # value is less than zero output
print ' ERROR '
Else
print ' Roadman ' # conditions are not set when the output
2. Looping statements
The while loop executes the loop body when the given judgment condition is true, otherwise exits the loop body.
The For Loop repeats the execution of the statement
Nested Loops you can nest A for loop in the while loop body
1.While Loop statement
While judging condition:
EXECUTE statement ...
Example:count = 0
while (Count < 9):
print ' The count is: ', count
Count = Count + 1
Print "Good bye!"
looping with else statements
Example:count = 0
While Count < 5:
Print count, "is less than 5"
Count = Count + 1
Else
Print count, "is isn't less than 5"
While statement when two important commands Continue,break to skip loops, Continue is used to skip the loop
i = 1
While I < 10:
i + = 1
If i%2 > 0: # Skip output when non-even
Continue
Print I # output 2, 4, 6, 8, 10
i = 1
While 1: # The loop condition is 1 must be set
Print I # output 1~10
i + = 1
If i > 10: # When I is greater than 10 o'clock jump out of the loop
Break
2.F or Loop
For...in.
Example:for letter in ' Python ': # First Instance
print ' Current letters: ', letter
looping with else statements
Example:for num in range (10,20): # iterations between 10 and 20 digits
For I in Range (2,num): # based on factor iterations
If num%i = = 0: # Determine the first factor
J=num/i # Calculates a second factor
print '%d equals%d *%d '% (num,i,j)
Break # jumps out of the current loop
Else: # The else part of the loop
Print num, ' is a prime number '
3.Python Loop Nesting
Python for loop nesting syntax:
For Iterating_var in sequence:
For Iterating_var in sequence:
Statements (s)
Statements (s)
Python while loop nesting syntax:
While expression:
While expression:
Statement (s)
Statement (s)
Instance:
The following instance uses the prime number between the nested loop output 2~100:
i = 2
while (I < 100):
j = 2
while (J <= (i/j)):
If not (I%J): Break
j = j + 1
if (J > i/j): Print I, "is prime"
i = i + 1
Print "Good bye!"
5. Circular control Statements
Break statement
The break statement is used to terminate the loop statement, which is used in the while and for loops.
If you use a nested loop,the break statement stops executing the deepest loop and starts executing the next line of code.
Example (Python 2.0+)
#!/usr/bin/python
#-*-Coding:utf-8-*-
For letter in ' Python ': # First Instance
If letter = = ' h ':
Break
print ' Current letters: ', letter
var = 10 # A second instance
While var > 0:
print ' current variable value: ', var
var = var-1
if var = = 5: # when variable var equals 5 o'clock Exit loop
Break
Print "Good bye!"
Continue statements
The Python continue statement jumps out of the loop, and break jumps out of the loop.
The continue statement is used to tell Python to skip the remaining statements of the current loop, and then proceed to the next round of loops.
The continue statement is used in the while and for loops.
Instance:
Example (Python 2.0+)
#!/usr/bin/python
#-*-Coding:utf-8-*-
For letter in ' Python ': # First Instance
If letter = = ' h ':
Continue
print ' Current letters: ', letter
var = 10 # A second instance
While var > 0:
var = var-1
if var = = 5:
Continue
print ' current variable value: ', var
Print "Good bye!"
Pass Statement
The Python Pass is an empty statement in order to maintain the integrity of the program structure.
Pass does not do anything, generally used as a placeholder statement.
Instance:
#!/usr/bin/python
#-*-Coding:utf-8-*-
# output Python for each letter
For letter in ' Python ':
If letter = = ' h ':
Pass
print ' This is a pass block '
print ' Current letters: ', letter
Print "Good bye!"
Python_ Control Statement 3