Home  >  Article  >  Backend Development  >  What are the control structures in python

What are the control structures in python

王林
王林Original
2020-06-11 14:26:1517337browse

What are the control structures in python

There are three major control structures in Python, namely sequential structure, branch structure (selection structure) and loop structure. Any project or algorithm can be designed using these three structures.

Let’s explain them separately:

1. Sequential structure

The sequential structure is to execute the code in the order you wrote, that is, one by one. Statements are executed sequentially. The logic of this structure is the simplest, just execute it in order.

2. Branch structure (selection structure)

The branch structure is also called the selection structure, which means that the program code chooses to execute specific code based on judgment conditions. . If the condition is true, the program executes one part of the code; otherwise, it executes another part of the code.

In the Python language, the syntax for selecting a structure is represented by the keywords if, elif, and else. The specific syntax is as follows:

The basic syntax is as follows:

1、if

2、if...else

3、if...elif...else

4、if... elif...elif...else

5. If nesting

3. Loop structure

The loop structure is the most used a structure. A loop structure refers to a coding structure that repeatedly executes a certain piece of code under certain conditions. Among Python's loop structures, the common loop structures are for loops and while loops.

1. for loop

The for loop is a type of loop structure. In Python, a for loop is an iterative loop, which repeats the same operation. Each operation is based on the result of the previous one. The for loop is often used to facilitate data structures such as strings, lists, and dictionaries. The for loop needs to know the number of loops. The basic syntax is:

2. for...in.... loop

3. while loop

The while loop does not need to know the number of loops. That is, it loops infinitely until the conditions are not met.

Note:

1. The loop also ends with a colon (:)

2. The conditions are various arithmetic expressions,

a ) When true, loop body statement group 1, repeat execution

b) When false, loop body statement group 2, stop execution

3. If the loop body forgets to accumulate, condition If the judgment is always true, it is an infinite loop. The loop body is always executed.

a) Infinite loops are sometimes used to build infinite loops

b) You can use ctrl c to terminate, or stop the IDE

Recommended tutorial: python tutorial

The above is the detailed content of What are the control structures in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn