A Brief Introduction to Python process control statements and python process statements

Source: Internet
Author: User

A Brief Introduction to Python process control statements and python process statements

People often say that life is a process of constantly making multiple choice questions: some people do not have to choose, there is only one way to go; some people are better, you can choose one; some are good or good at home, there can be more options; some people will go around in the old days of their lives and cannot find directions. For those who believe in God, it is like God has prepared a line of life for us in advance, or the hardships that God has set up in advance for the lessons learned by TANG Zeng, god and God take control of everything. Programming languages can simulate all aspects of human life. programmers can control the execution process of a program through special keywords in programming languages like God and God. These keywords constitute a process control statement.

The process control statements in programming languages are divided into the following types:

  1. Sequential statements
  2. Branch statement
  3. Loop statement

The ordered statement does not need to be controlled by a separate keyword, that is, the execution of a row does not require special instructions. Here we mainly talk about the branch and loop statements.

I. Branch statements

The execution result (True/False) of one or more statements (condition judgment) determines the code block of the branch to be executed. The branch statement provided in Python is the if. else statement, and the switch.. case statement is not provided. The if... else statement has the following forms:

Single Branch:
If condition:
Code block
If the code block of a single branch statement has only one statement, you can write the if statement and the code in the same line:

If condition: Code
Instance: determines whether the specified uid is a root user.

uid = 0if uid == 0:  print("root")

You can also write as follows:

uid = 0if uid == 0: print("root")

Output result: root

Dual Branch:

If condition:
Code block
Else:
Code block
Instance: print the user identity based on the user ID

uid = 100if uid == 0:  print("root")else:  print("Common user")

Output result: Common user

Multiple branches:

If condition 1:
Code Block 1
Elif judgment condition 2:
Code Block 2
...
Elif judgment condition n:
Code block n
Else:
Default Code Block

Example: print the letter level based on the Student Score

score = 88.8level = int(score % 10)if level >= 10:  print('Level A+')elif level == 9:  print('Level A')elif level == 8:  print('Level B')elif level == 7:  print('Level C')elif level == 6:  print('Level D')else:  print('Level E')

Output result: Level B

Note:

The expression in the above "judgment condition" can be any expression or any type of data object instance. If the True value of the final returned result of a condition is True, the condition is True and the corresponding code block is executed. Otherwise, the condition is invalid, determine the next condition.

Ii. Cyclic statements

When we need to execute a code statement or code block multiple times, we can use a loop statement. The loop statements provided in Python are: while LOOP and for loop. Note that there is no do... while loop in Python. In addition, there are several loop control statements used to control the cyclic Execution Process: break, continue, and pass.

1. while Loop

Basic Form
The basic form of the while LOOP statement is as follows:

While judgment condition:
Code block
If the True value of the returned value of the given judgment condition is True, the code of the loop body is executed. Otherwise, the loop body is exited.

Instance: print the number 0-9 cyclically.

count = 0while count <= 9:  print(count, end=' ')  count += 1

Output result: 0 1 2 3 4 5 6 7 8 9

While endless loop

When the while clause is always True, the code in the while LOOP body will continue forever.

While True:
Print ("this is an endless loop ")
Output result:

This is an endless loop
This is an endless loop
This is an endless loop
...
In this case, you can use Ctrl + C to terminate the operation.

While... else
Statement format:

While judgment condition:
Code block
Else:
Code block
The code block in else is executed when the while loop is executed normally. If the while loop is interrupted by break, the code block in else is not executed.

Example 1: When the while loop ends normally (the statements in else will be executed)

count = 0while count <=9:  print(count, end=' ')  count += 1else:  print('end')

Execution result: 0 1 2 3 4 5 6 7 8 9 end

Instance 2: When the while loop is interrupted (the statements in else are not executed)

count = 0while count <=9:  print(count, end=' ')  if count == 5:    break  count += 1else:  print('end')

Output result: 0 1 2 3 4 5

2. for Loop

A for Loop is usually used to traverse sequences (such as list, tuple, range, and str), sets (such as set), and ing objects (such as dict ).

Basic Form
The basic format of the for Loop:

For temporary variable in iteratable object:
Code block
Instance: print the elements in a list through traversal.

names = ['Tom', 'Peter', 'Jerry', 'Jack']for name in names:  print(name)

For sequences, the index is also used for iteration:

names = ['Tom', 'Peter', 'Jerry', 'Jack']for i in range(len(names)):  print(names[i])

Execution result:

Tom
Peter
Jerry
Jack

For... else

It is basically the same as while... else.

3. Loop Control statement

The loop control statement can change the execution process of the program in the loop body, such as interrupting the loop or skipping this loop.

Cyclic control statement description
Break terminates the entire cycle
Contine skips this loop and executes the next loop
The pass statement is an empty statement. It only has no special meaning to maintain the integrity of the program structure. The pass statement is not only used in loop statements, but also in branch statements.
Example 1: traverse all numbers in the 0-9 range, and print the odd number in the loop control statement.

for i in range(10):  if i % 2 == 0:    continue  print(i, end=' ')

Output result: 1 3 5 7 9

Instance 2: print the first three elements in a list using a loop control statement.

names = ['Tom', 'Peter', 'Jerry', 'Jack', 'Lilly']for i in range(len(names)):  if i >= 3:    break  print(names[i])

Output result:

Tom
Peter
Jerry

4. loop nesting

Loop nesting refers to embedding another loop in a loop body.

Example 1: print the 99 multiplication table through A while LOOP

j = 1while j <= 9:  i = 1  while i <= j:    print('%d*%d=%d' % (i, j, i*j), end='\t')    i += 1  print()  j += 1

Example 2: print the 99 multiplication table through a for Loop

for j in range(1, 10):  for i in range(1, j+1):    print('%d*%d=%d' % (i, j, i*j), end='\t')    i += 1  print()  j += 1

Output result:

1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.