Getting Started with Python - loop

A, while circulation

If the condition is satisfied (true), repeat the same operation conditions are not met, out of the loop

while cycling conditions:

Cycle operation
Example: two students are input 5 grades, average scores were calculated and
1 j = 1 # defines the outer loop counter initial value
2 prompt = 'Enter the student's name' # define a string variable, called when the user input this variable can reduce the trouble knock characters
3 while j <= 2: # is defined as the outer loop is performed twice
4 sum = 0 # define the initial value results, the reason is defined herein is to be input when the second student score let sum is initialized to 0, the re-received new student grades and
5 i = 1 # defines an inner loop counter initial value
6 name = raw_input (prompt) # receiving student's name input by the user, is assigned to the variable name
7 while i <= 5: # 5 cycles defined internal function, is to receive results of five courses
8 print ( 'Please enter% d door examination results:'% i) # prompts the user grades, which uses a formatted output, taking% of d value display with the value of i, a first course, the second course ......
. 9 iNPUT + SUM = SUM () # receiving performance of a user input, assigned to SUM
10 i + # i = variable increment. 1 1, i becomes 2, the cycle continues, until i is equal to 6, out of cycle Ring
11 avg = sum / (i- 1) # calculates the average score of a student sum / (6-1), assigned to AVG
12 is Print name, 'the average score is% d \ n'% avg # output Students average score
13 j = j + 1 # inside the loop is finished, the outer loop counter j is incremented by one, to 2, then the outer loop
14 print 'input completion student performance! '# External loop ends, prompted to enter complete!

Two, for circulation

1) for statement can traverse all of the elements, for example, by one output string of characters, one by one element in the output list, tuple in the set of elements (note the order of the elements in the assignment), dictionary key ......
Fruits = [ 'watermelon', 'peaches', 'grapes']
for Fruit in Fruits:
Print Fruit

Results:
watermelon
peaches
grapes

2) repeat the same operation

Using the range () function to create a list of numbers

Before beginning the start to the end of the digital numbers: range
for i in range (0,5): # 0-4 are sequentially stored in the variable i
print 'JKCC SNEAKER CLUB'

结果:
JKCC SNEAKER CLUB
JKCC SNEAKER CLUB
JKCC SNEAKER CLUB
JKCC SNEAKER CLUB
JKCC SNEAKER CLUB

Third, the control loop

Loop control statements may change the normal cycle of execution order

Loop control statements

break statement: out of this cycle (out of nested loops only one cycle)

continue statement: Skip remaining statements in the current round of the loop body, the state re-test cycle, enter the next cycle, such as the number of cycles a total of five times, the fourth encounter continue, then we do not continue, and direct 5th cycle Analyzing

Published 16 original articles · won praise 0 · Views 287

Guess you like

Origin blog.csdn.net/JOKERSNEAKER/article/details/105085567