Python conditional statements

Source: Internet
Author: User

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

The conditional statements in Python include the IF, elif, else statements, which are used to control the execution of the program and, when judged to be true, execute the body under the statement, and skip the statement when the condition is false. The specific syntax is as follows:

format 1 :

if Judging Condition:

EXECUTE Statement ...

Executes the statement under the condition when the condition is true (the value is true), otherwise the statement under that condition is not executed. Take the following code as an example:

Code:

Results:

The execution result of the above code is True. Because the value of a is ' a ', when the first judgment condition is executed, a= ' A 'is satisfied, so the statement under that condition is executed to print the result True.

format 2 :

off Judging Condition:

EXECUTE Statement ...

Else :

EXECUTE Statement ...

Executes the statement under the condition when the condition is true (the value is true); otherwise, the statement under else is executed. It is important to note that Python determines a block of code by indentation. Take the following code as an example:

Code:

Results:

The execution result of the above code is True. Because the value of a is 1, when the first judgment condition is executed, a>0is satisfied, so the statement under that condition is executed to print the result True. Since the first judging condition has been fulfilled, the program no longer checks for other judging conditions. Make a slight change to the above code, see below:

Code:

Results:

The execution result of the above code is False. Because the value of a is 0, when the first judgment condition is executed, a>0is not satisfied, so the execution statement is skipped, the execution body under the else statement is executed, and the print result is False.

format 3 :

if Judging Condition:

EXECUTE Statement ...

elif Judging Condition:

EXECUTE Statement ...

elif Judging Condition:

EXECUTE Statement ...

Else :

EXECUTE Statement ...

You can use the above form when judging the condition as multiple values.

The Python conditional statement has several characteristics

1. If there are multiple criteria for judging, you can use and/or connection.

2, if there are more than one condition can use parentheses to distinguish the order of judgment, the judgment in parentheses precedence execution,

In addition, the priority of and and or is lower than that of > (greater than), < (less than), that is, greater than and less than and/or in cases where no parentheses are preferred.

Code:

Results:

3. The Python composite Boolean expression calculation uses the short-circuit rule, that is, if the value of the entire expression has been computed through the previous section, the latter part is no longer evaluated. See the following example:

Code:

Results:

When the program determines that a>0 is false when it is running, the result of the "and" Operation for false and any Boolean value is false, so the program no longer evaluates the expression ( b/a>2 ) , and the result "no" is eventually printed. However, this code is actually wrong, because 0 cannot do the divisor, and in the expression (b/a>2) , 0 does the divisor. Because of this characteristic of Python, the program does not report zero except error when it is running. We will modify the above code slightly as follows:

Code:

Results:

We changed the "and"in the code to "or" because the value of the expression (a>0) is False, so the program continues to evaluate the expression (b/a >2), at which point the program checks for a divisor of 0, so the program runs the Times wrong.

4. In nested if statements, the if...elif...else structure can be placed in another If...elif...else structure.

Python conditional 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.