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

# example 1:if basic usage

# coding = gb2312 flag = Falsename = ' Luren '     if name = = ' Python ':         # Determine if variable is ' python '    flag = True      # Set flag when condition is set For true    print ' welcome boss '    # and OUTPUT welcome message else:    print name              # condition not valid when output variable name >>> luren          # output result

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:

# example 2:ELIF usage num = 5    If num = = 3:            # To determine the value of num    print ' boss '       elif num = = 2:    print ' user ' elif num = = 1:    print ' worker ' Elif num < 0:           # value less than zero output    print ' error ' else:    print ' Roadman '     # conditions are not set when the output >>> Roadman        # Output results

Because Python does not support the switch statement, so many conditions to judge, can only be implemented with Elif, if the judgment needs to be judged by multiple conditions, you can use or (or), indicating that two conditions have a successful judgment condition, when using and (and), it means that only two conditions at the same time Circumstances, the judging condition succeeds.

# example 3:IF statement multiple conditions

num = 9if num >= 0 and num <=:    # Determine if the value is between 0~10    print ' hello ' >>> hello      # output num = 10if num & Lt 0 or num >:    # Determine if the value is less than 0 or greater than the    print ' hello ' else:    print ' undefine ' >>> undefine       # output num = 8 # Determine if the value is between 0~5 or 10~15 if (num >= 0 and Num <= 5) or (Num >= and num <=15):       print ' hello ' else:    print ' Undefine ' >>> undefine       # Output results

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:

#!/usr/bin/python

var = 100

if (var = =): print "Value of expression is 100"

Print "Good bye!"

The above code executes the output as follows:

Value of expression is 100

Good bye!

  • 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.