Ubuntu 16.04 Python 2.7 control flow syntax (III)
When we know that python can handle strings, we need to learn more about the statements and functions it uses; When I am familiar with these process statements, we can simply do some tasks and deal with simple current scripted work; Then wait and see!
Other process control tools
In addition to the while statements in the previous section, python also has control statements similar to other languages, such as for, if, else, continue, break, etc. Let's introduce them one by one;
if, elif and else are well known
>>> x = int(raw_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' ... More
The Python for loop can traverse any sequence of items, such as a list or a string.
The syntax format of the for loop is as follows:
for iterating_var in sequence: statements(s)
Use cases are as follows:
>>> #test ... word = ['hello','world','python'] >>> for w in word: ... print w, len(w) ... hello 5 world 5 python 6 >>>
You can also do some insert functions in the loop
>>> # Measure some strings: ... words = ['cat', 'window', 'defenestrate'] >>> for w in words[:]: # Loop over a slice copy of the entire list. ... if len(w) > 6: ... words.insert(0, w) ... >>> words ['defenestrate', 'cat', 'window', 'defenestrate']
range(start, stop[, step])
Start: count starts from start. The default is to start from 0. For example, range (5) is equivalent to range (0, 5);
Stop: count to the end of stop, but do not include stop. For example, range (0, 5) is [0, 1, 2, 3, 4] without 5
Step: step size; the default value is 1. For example, range (0, 5) is equivalent to range(0, 5, 1)
Here are some examples
>>> range(11) # From 0 to 11 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
>>>range(10) # From 0 to 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1, 11) # From 1 to 11 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> range(0, 30, 5) # Step size is 5 [0, 5, 10, 15, 20, 25] >>> range(0, 10, 3) # Step size is 3 [0, 3, 6, 9] >>> range(0, -10, -1) # negative [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> range(0)  >>> range(1, 0) 
The following is the use of range in for, looping out each letter of python:
>>> x = 'python' >>> for i in range(len(x)): ... print(x[i]) ... p y t h o n >>>
Python break statement
The break statement is used to terminate the loop statement, that is, if the loop condition has no False condition or the sequence has not been completely recursive, the execution of the loop statement will also be stopped. Break statements are mainly used in while and for loops. If you use nested loops, the break statement stops executing the deepest loop and starts executing the next line of code.
>>> for x in 'python': ... if x == 't': ... break ... print x ... p y >>>
Python continue statement
Python continue statement jumps out of this loop, and break jumps out of the whole loop. The continue statement is used to tell Python to skip the remaining statements of the current loop and then proceed to the next loop. The continue statement is used in while and for loops.
>>> for x in 'python': ... if x == 't': ... continue ... print x ... p y h o n >>>
Python loop nesting
Python allows one loop to be nested into another loop;
Python for loop nesting syntax:
for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)
Python while loop nesting syntax:
while expression: while expression: statement(s) statement(s)
You can embed other loop bodies in the loop. For example, you can embed a for loop in a while loop. On the contrary, you can embed a while loop in a for loop.
>>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... 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 * 3
(yes, this is the correct code. Look carefully: else clause belongs to for loop, not if statement.)
Python pass function
Python pass is an empty statement to maintain the integrity of the program structure. Pass does nothing. It is generally used as a placeholder statement.
>>> while True: ... pass # Busy-wait for keyboard interrupt (Ctrl+C) ...
This is usually used to create the smallest class:
>>> class MyEmptyClass: ... pass ...
Function can improve the modularity of application and the reuse rate of code. You already know that Python provides many built-in functions, such as print(). But you can also create your own functions, which are called user-defined functions.
def functionname( parameters ): "function_Document string" function_suite return [expression]
The keyword def introduces a function definition. It must be followed by the function name and a list of formal arguments in parentheses. The statements that make up the body of the function start on the next line and must be indented.
We can create a function that outputs Fibonacci sequence in any range:
>>> def fib(n): # write Fibonacci series up to n ... """Print a Fibonacci series up to n.""" ... a, b = 0, 1 ... while a < n: ... print a, ... a, b = b, a+b ... >>> # Now call the function we just defined: ... fib(2000) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
Because we have to learn more about functions, we will continue to learn functions in the next section;
It's hard to stick to one thing, but if you stick to it, you will gain a lot; Often people can't postpone their sense of satisfaction and like to meet in time; It's like playing a game. One will be promoted, one will rank higher, one will play a strange game, and one will get a lot of gold coins... The game can give you continuous and timely feedback and give you satisfaction in time; But after playing the game, I found that I had nothing to gain in a day, and reading, exercise and writing all need to be insisted on for a long time; We may not get anything for a long time, but after one month, five months, one year, two years or ten years, we will find that this change is imperceptible; Believe in yourself, you can do it;