6. Control Flow
Before talking about the control flow, we should talk about indentation. The so-called indentation is actually the blank at the beginning of the line. There are three kinds of indentation: one tab (the length of 8 spaces), two spaces, and four spaces. Choose a persistence.
In real life, we often say what it is like in programming. If statements are your choice. Maybe you have heard of the Boolean value. What is the Boolean value? It is actually true or false. However, the two values are given a name based on a person's name. The value detected by the if statement is a Boolean value.
For example:
#!/usr/bin/pythonnumber = 23if number == 23: print 'the number is 23'elif number == 24: print 'the number is 24'else: print 'the number is not 23'
How can I see the usage of the IF statement? The IF keyword is added with spaces and a judgment Expression plus a colon, followed by line breaks and indentation. The indent statement is the statement executed after the if condition is true, after execution, other judgment statements (Elif plus judgment statements) and else statements are skipped to execute subsequent programs. If the first condition fails, the next condition is executed. If both conditions fail, the statement block after else is executed. Of course, The Elif and else statement blocks can be omitted. If you do not need to execute any statements, you need to use pass, which tells the program that you can pass the statement from here, and there is no statement to be executed here? It is equivalent to an empty statement or an empty statement block in other languages. It's easy.
Our house is decorated today with a lot of ceramic tiles to be moved. We can only move 10 pieces at a time, with a total of 500 pieces. Each time we move, we need to confirm how many pieces were moved. What should we do if we use python to solve this problem? It is always impossible to judge whether there are 500 pieces... Because this is a loop, we should use the loop statements provided by python to solve it. Let's take a look at what loop statements and their syntax Python provides for us.
The first is the while statement:
While condition:
Statement Block
Else:
Statement Block
Oh, you have found out. Yes, the while statement is very different from other languages. There is an optional else statement after the while statement. When the condition following the while statement is not true, if you select this else statement, the else statement block is executed.
Okay. Let's take a look at how we can complete this applet.
#!/usr/bin/pythonTOATAL = 500movedNum = 10 while movedNum != TOTAL: print 'we have moved', movedNum movedNum += 10else: print 'Done!'
But I think a lot of people may not get used to this else Statement (including me), so well, it is the same to directly print this statement without it.
Let's take a look at the following example:
#!/usr/bin/pythonnumber = 23running = True while running: guessNum = int(raw_input('Enter an integer: ')) if number == guessNum: print 'you got it' running= False elif number < guessNum: print 'No, it is a little lower' else: print 'No, it is a little higher'print 'Done!'
We found that there are two new things here. What are these things? You may have guessed that raw_input is a function for requesting input, in parentheses it is a prompt, and the INT () function is to convert the input string to an integer.
Are there any other loop statements? The author of python is not so stingy. It only provides us with this loop statement. We also have a for in function. If you have learned other languages such as Java, you will think this is a very friendly syntax, but it is different from the foreach statement in Java, in python, The for statement obtains elements from a sequence after in. Maybe it's not very clear. Let's take a look at the example.
#!/usr/bin/pythonfor u in range(1, 5): print u,else: print 'this loop is over.'
Display:
1 2 3 4 this loop is loop
I want to see the example to explain the confusion. Why? After reading this example, you will find that the for statement is actually quite simple. Isn't that the case? But you will find out where the sequence is, and why is there no line feed? Here we need to explain the range function. The range function returns a sequence. In the example above, the range function returns an integer between [1, 5, that is, 1, 2, 3, and 4. The range function also needs to be supplemented. In fact, the range function has three parameters, both of which are known. The third parameter is not difficult to guess because of this, and the third parameter is the step size. This parameter can be positive or negative. Another example:
#!/usr/bin/pythonfor u in range(1, 10, 3): print u
Display:
1
4
7
Well, let's take a look at why there is no line feed in the previous example. If you observe yourself, you will find a comma next to the print statement. That's right, it's because of this comma.
You need to know about the control flow. The break statement and the continue statement. In fact, these two statements are very simple. The break statement forces the end (or jump out) of the current recent loop statement. The continue statement skips all the statements after the continue statement and then carries out the next loop. Let's take a look at the example.
#!/usr/bin/pythonfor o in range(1, 3): print 'out' foru in range(1,20, 3): if u == 10: print 'break' break else: print 'continue' continue print 'in' # you won’t see this statement
Display:
Out
Continue
Continue
Continue
Break
Out
Continue
Continue
Continue
Break
The best way to understand this is to do more exercises.