Chapter 6 of python3, python3

Source: Internet
Author: User

Chapter 6 of python3, python3

A Python Condition Statement is a code block that is determined by the execution result (True or False) of one or more statements.

The execution process of conditional statements, such:

 

 

A condition statement, also known as an if statement, has the following complete Syntax:

If condition 1: Statement Block 1 elif condition 2: Statement Block 2 else: Statement block 3

The execution process of the preceding syntax is:

When condition 1 is True, execute the statement of statement Block 1.

If condition 1 is False, condition 2 is used.

When condition 2 is True, execute the statement of statement Block 2.

If condition 2 is False, execute statement Block 3.

 

Elif can be omitted, that is

If condition 1: Statement Block 1 else: Statement Block 2

Elif can appear multiple times, that is

If condition 1: Statement Block 1 elif condition 2: Statement Block 2 elif Condition 3: Statement block 3 else: Statement block 4

Else can be omitted, that is

If condition 1: Statement Block 1

It seems complicated. In fact, you only need to remember that the condition statements are from top to bottom to determine whether the condition is true. If the condition is true, the block corresponding to the condition is executed, if not, continue to judge the next condition. If none of the conditions are true, execute the statement block corresponding to else.

Note that:

  • Each condition is followed by a colon (:), indicating the statement block to be executed after the condition is met.
  • Use indentation to divide statement blocks. statements with the same number of indentations form a statement block.

Elif is short for else if. in python, elif replaces else if. Therefore, the keywords of python are: if, elif, and else.

 

The following shows how to use it:

A = 20b = 100if a> B: print ('a greater than B ') else: print ('a not greater than B ')

Output the code after execution:

A is not greater than B

 

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.