Introduction to the

Flow control is nothing more than an if else control statement. Today we’ll look at how flow control in Python is different.

While statement

The while statement in Python is no different from any other language. I use the while statement to write a Fibolach sequence:

In [56]: while x < 10 : ... : print(x) ... : x, y = y, x+y ... : 0 1 1 2 3 5 8Copy the code

If statement

If in Python can be used with elif or else:

>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:. x = 0 ... print('Negative changed to zero') ... elif x == 0: ... print('Zero') ... elif x == 1: ... print('Single') ... else: ... print('More') ... MoreCopy the code

The if statement is very simple, so I won’t cover it here.

For statement

The for statement in Python is used primarily to iterate over a sequence, such as a list or string:

In [57]: ages = [ 10, 14, 18, 20 ,25] In [58]: for age in ages: ... : print(age) ... : 10 14 18 20 25Copy the code

During traversal, in order to prevent the original sequence from being modified during traversal, we can traverse the copy of the sequence:

In [59]: for age in ages.copy(): ... : print(age) ... : 10 14 18 20 25Copy the code

A combination of the for statement and the range() function can have a different effect.

Range () is used to generate collections within a given range:

In [61]: for age in range(5): ... : print(age) ... : 0 1 2 3 4Copy the code

The range() function can also take a step size as a third argument:

In [62]: for age in range(5, 10 , 2): ... : print(age) ... : 5 July 9Copy the code

A combination of Range() and len() allows for a convenient list of variables:

>>> a = ['Mary'.'had'.'a'.'little'.'lamb']
>>> for i in range(len(a)):. print(i, a[i]) ... 0 Mary 1 had 2 a 3 little 4 lambCopy the code

Break

Break is used to break out of the nearest for or while loop.

Note that the for loop can be used with else:

In [64]: for n in range(2, 10): ... : for x in range(2, n): ... : if n % x == 0: ... : print(n, 'equals', x, '*', n//x) ... : break ... : else: ... : print(n, 'is a prime number') ... : 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2  * 4 9 equals 3 * 3Copy the code

The else statement in the loop will execute after the for loop completes. If we break the for loop with break, the else statement will not be executed.

Continue

Continue is used to skip the rest of the loop and continue with the next loop.

Again, we use continue to modify:

In [68]: for n in range(2, 10): ... : for x in range(2, n): ... : if n % x == 0: ... : print(n, 'equals', x, '*', n//x) ... : continue ... : else: ... : print(n, 'is a prime number') ... : 2 is a prime number 3 is a prime number 4 equals 2 * 2 4 is a prime number 5 is a prime number 6 equals 2 * 3 6 equals 3  * 2 6 is a prime number 7 is a prime number 8 equals 2 * 4 8 equals 4 * 2 8 is a prime number 9 equals 3 * 3 9 is a prime numberCopy the code

As you can see, in continue, the else statement is always executed.

pass

Pass means do nothing. Is an empty execution.

Usually we use pass as a placeholder for a function or conditional substatement to indicate that specific content can be filled in at a later date.

Pass can be used in a while:

>>> while True:. pass # Busy-wait for keyboard interrupt (Ctrl+C) ...Copy the code

You can use pass in a class:

>>> class MyEmptyClass:. pass ...Copy the code

We can use pass in functions:

>>> def initlog(*args):. pass # Remember to implement this! .Copy the code

This article is available at www.flydean.com/04-python-c…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!