Python conditional Statements and loop statements

Source: Internet
Author: User

First, sequential structure

The sequential structure is the simplest kind of program structure, and the program executes from top to bottom in the order in which the statements are written.

Two, branch control statements

A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false).

1. If statement

The general form of the IF statement in Python is as follows:

if condition_1:    statement_block_1elif  condition_2:    statement_block_2Else  :    statement_block_3
    • If "condition_1" is True, the "statement_block_1" block statement is executed
    • If "condition_1" is false, the "condition_2" will be judged
    • If "Condition_2" is True, the "statement_block_2" block statement is executed
    • If "Condition_2" is false, the "STATEMENT_BLOCK_3" block statement is executed

Python replaces the else ifwith elif , so the keyword for the IF statement is:if–elif–else.

Attention:

    • 1. Use a colon (:) after each condition to indicate the next block of statements to be executed after the condition is met.
    • 2, using indentation to divide the statement block, the same indentation number of statements together to form a block of statements.
    • 3. There are no switch–case statements in Python.
2. If nesting

In a nested if statement, the IF...ELIF...ELSE structure can be placed in another if...elif...else structure.

if expression 1:    statement if  expression 2:        statement     elif  expression 3:        statement      else         statement elif  expression 4:    statement Else:    statement

Third, cycle programming

1. For statement

A Python for loop can traverse any sequence of items, such as a list or a string.

The general format for the For loop is as follows:

 for  in <sequence>:    <statements>Else:    <statements>
2. While loop

The general form of a while statement in Python:

 while Judging Condition:    statement

3. Break and continue statements and ELSE clauses in loopsThe break statement can jump out of a for and while loop body. If you terminate from a for or while loop, any corresponding loop else blocks will not execute. 4. Pass Statement

The Python Pass is an empty statement in order to maintain the integrity of the program structure.

Pass does not do anything, generally used as a placeholder statement,

Python conditional Statements and loop statements

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.