Python Learning (5) Conditional statements

Source: Internet
Author: User

Directory
    • Conditional statements
    • Simple Group of statements

Pythonconditional Statements

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 by:

The Python program language specifies any non-0 and non-null (NULL) values of true,0 or NULL to FALSE.

In Python programming, if statements are used in the execution of control programs, in the basic form:

If  judgment condition:     EXECUTE statement ... Else: EXECUTE statement      ...

Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range.

Else as an optional statement, you can execute the relevant statement when you need to execute the content when the condition is not true, as shown in the following example:

The output is:

>>>

The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).

When judging a condition for multiple values, you can use the following form:

if judgment condition 1: Execute statement 1... elif judgment Condition 2: EXECUTE statement 2... elif judgment Condition 3: EXECUTE statement 3... else: EXECUTE statement 4             ... 

Examples are as follows:

The output is:

>>> Roadman

Since Python does not support switch statements, multiple conditions can only be determined by elif, and if the judgment requires multiple conditions to be judged at the same time, the or (or) is used to indicate that two conditions have a successful condition when they are established; When using and (and), Indicates that only two conditions have been established at the same time, the judgment condition succeeds.

>>>Hello# Output Results>>>Undefine# Output Results >> > Undefine# output          

If there are multiple conditions, you can use parentheses to distinguish the order of judgment, the judgment in parentheses precedence, and the priority of and and or is lower than > (greater than), < (less than), such as the judgment symbol, that is, greater than and less than in the absence of parentheses will be compared with or priority to judge.

Simple Group of statements

You can also use the IF condition judgment statement on the same line, as in the following example:

The above code executes the output as follows:

  The value of variable var isgood bye    

Python Learning (5) 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.