“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

I’m glad that Python has learned looping statements, and it’s getting more and more fun. Basically, it can handle pipelining work, but it’s a little less flexible and less reusable.

Looping statements

Loop statements in Python include for and while; As you can see in the figure above, the loop basically needs to do something, because not all data is needed, and the output is controlled by an if condition.

The while loop

General sentence

# if True, execute the code block inside; if false, exit the loop
while (condition): 
    print("Execute statement")
Copy the code

Hint: There is no do in Python… The while loop

  • It seems that most users of Python do not use while… Else conditions like that
while False:
    print("Will not be carried out.")
else:
    print("Execute when the while condition is False")
Copy the code
  • Example: To find the sum between 1 and 100, use the while loop
dig=100

sum=0
count=1
while count <=dig:
    sum+=count
    count+=1
    
print("The sum between 1 and {} is :{}".format(dig,sum))
The sum between 1 and 100 is 5050
Copy the code
  • Code analysis:

    • Count acts as a counter for the while loop, and it cannot exceed the target value dig; Otherwise we exit the loop, and sum+=count, and sum+count is reassigned to sum every time
  • Of course, this is the result of our study, so how to determine the result is correct?

    • Two methods, their own pen to calculate a calculation, what joke; If you add 100 and 50, there are 49 pairs of 100, such as 1+99, 2+98, 3+97, and so on, until 49+51, that is, 49 pairs of 100, plus 100+50, is 5050
    • Python provides the sum() and range() functions. There is a criticism here. In the above example, sum=0 overwrites the original sum() function, so the sum() function cannot be used in the current block.
The argument to the # sum() function must be of an iterable type, except STR
s=sum(range(1.101))

print(s) # output: 5050

The range function will be introduced later in the built-in functions
Copy the code
  • While True: This loop has a special case where real-time requests are required on the client side, and infinite loops are useful.

The for loop

This is a bit of a special loop because it’s for… Combination of in, what is in? Our member operator, for, is while, condition is x in XXX:, so it can’t be for True but x in XXX; Think about it: can “while” be accompanied by “x in XXX”?

  • The for loop iterates over sequence types such as list or string
a="1234567"

for i in a:
    print(i)
# output:
1
2
3
4
5
6
7
Copy the code
  • The for loop is often confused with the range() function; The range() function is a closed and open conditional function
for i in range(5) :print(i)

# output:
0
2
3
4
Copy the code

The range () function

  • The range() function starts at 0 by default and can specify ranges: range(1,5)
for i in range(1.5) :print(i)

# output:
1
2
3
4
Copy the code
  • (n:m:k), but in range, it needs to be changed into a comma: range(n,m,k),m>n,k is the step size
# odd
for i in range(1.10.2) :print(i)

# output:
1
3
5
7
9
Copy the code

The continue and break

Literally, a continue is a continuation loop, and a break is an interruption loop

  • For example, while True is an infinite loop. If controlled by the if condition, break breaks out of the loop when the condition is satisfied
Enter the Python interactive command mode

>>> count=1
>>> while True:
.    print(count)
.    if count==4:
.        print(count)
.        break
.    count+=1.1
2
3
4
4
Copy the code
  • In the example above, if you change it to continue, then the loop is infinite, and it terminates before it terminates.
  • Note 1: All loops that break out of for or while do not execute else;
  • Note 2: the continue and break positions will not be executed if they precede a block of code.

i=1
while i<10:
    if i==3:
        continue  
        # will not execute I +=1, I will always be 3, and then it will loop forever here
        print("Unable to execute")
    i+=1
Copy the code
  • So for it to continue, add the condition I +=1 before continue
# CMD Enter Python into interactive mode:

>>> i=1
>>> while i<10:
.    print(i)
.    if i==3:
.        i+=1
.        continue
.        print("Unable to execute statement")
.    print("When I ==3, this sentence does not execute, {}".format(i))
.    i+=1.1When I = =3This sentence is not executed.1
2When I = =3This sentence is not executed.2
3
4When I = =3This sentence is not executed.4
5When I = =3This sentence is not executed.5
6When I = =3This sentence is not executed.6
7When I = =3This sentence is not executed.7
8When I = =3This sentence is not executed.8
9When I = =3This sentence is not executed.9

# I ==3
Copy the code
  • Here’s another example of the break keyword:
Enter Python into interactive mode

>>> i=1
>>> while i<10:
.    print(i)
.    if i==3:
.        i+=1
.        break
.        print("Unable to execute statement")
.    i+=1
.    print("It will only execute until I ==3")
.else:
.    print("Jump out of the loop above, don't execute the code block here.")...1It only executes until I ==3
2It only executes until I ==3
3
Copy the code

Pass the keyword

It’s an empty statement that does nothing but placeholders; Maintain structural integrity.


if i in range(4) :pass

# execute nothing, but it will iterate over
Copy the code

Think about it: can “while” be accompanied by “x in XXX”?