Python learning Day06

First, the process control loop while

1.1 What is the cycle

When we Raiders stand-alone game, I want to repeat some of checkpoints Raiders repeated in order to achieve the purpose of customs clearance. In programming, we also need to have some program cycle operation, this time you need to use for loop or while loop.

1.2while loop syntax

while 条件表达式:
    语句块

1.3while+break

By the above example it can be seen with the results of running on the computer, while its own circulation and does not automatically end the cycle, we need to control him out of the loop by using break

while 条件表达式:
    语句块
    if 条件成立 :
        break       #通过if判断,break跳出循环

1.4while+continue

Sometimes we need to be certain without circulation, but we want to have been in the loop, so this time we can use while + continue to control

For example: I need to order from 1-10 apples, but I do not want a fifth

apple=0
while apple<=10
    apple+=1
    if apple==5
    continue
The results are printed as: 1234678910

1.5while loop nest

  • What is the nested loop:

    Sometimes we repeat do something when, in the middle need to repeat to do several things, then we can use this time to do a nested loop

    while True:
      while True:
      #if后面式可以跟else的
          if  条件表达式 :
              break
      if 条件表达式 :
          break   

Second, the process of the control loop for

2.1 Why use a for loop

Perhaps it is all very strange, obviously there while loop, why use a for loop it?

In fact, the reason is very simple, you can control the number of cycles when the for loop, more convenient and more accurate, and while he is not a loop for recycling control cycles, so this time, for the benefits of cycling on the obvious

2.2, for loop syntax

Why use a for loop to say, it would have to talk about the syntax of the for loop

xiguale = ['xi','age','run','book']
for i in xiguale :
    print(i)
Output:
xi

age
run
book

Can be found traversing the for loop and convenient, as well as for the powerful loop through it.

2.3、for+break与for+continue

xiguale = ['xi','age','run','book']
for i in xiguale :
    if i=='age' :
        break
    print(i)

xiguale = ['xi','age','run','book']
for i in xiguale :
    if i=='age' :
        continue
    print(i)    
Output:
piss
run
book

Guess you like

Origin www.cnblogs.com/ledgua/p/11283136.html