[Python] basis of conditional statements


Python conditional statement is executed by one or more results of the statement ( True or False ) to determine the execution code block.

We can understand the simple execution of conditional statements by the following figure :

 

image.png

 

Python programming language specify any non- 0 and a non-empty ( null ) values to true , 0 or null is to false .

Python programming if statement for executing the control program, substantially in the form of:

if Analyzing conditions:

    Execute the statement ...... the else :

    Execute the statement ......

 

Wherein " determination condition " the establishment (non-zero), the latter statement is executed, the content may be performed multiple lines, indented to distinguish the same range.

else is optional statement, when you need to perform content-related statements can be executed when the condition is not satisfied, the following specific examples:

! # / usr / bin / python # - * - coding: UTF-8 - * - # Example 1: if the basic usage flag = Falsename = 'luren'if name == ' python ': # variable determines whether to' python ' 
    set flag flag = true # condition holds true 
    print 'welcome boss' # and the greeting message else: 
    the output variable name print condition is not satisfied when the name #


 

The output is:

>>> luren # output

 

if the judgment condition statement may be > (greater than), <( less than ) , == (equal), > = (greater than or equal), <= to indicate its relationship (or less).

When the determination condition is a plurality of values, the following form:

if the determination condition 1:

    Executing the statement . 1 ...... elif judgment conditions 2:

    Executing the statement 2 ...... elif judgment condition 3:

    Execute the statement 3 ...... the else:

    Execute the statement 4 ......

 

Examples are as follows:

! # / usr / bin / python # - * - coding: UTF-8 - * - # Example 2: elif Usage num = 5 if num == 3: # num determination value 
    print 'boss' elif num == 2 : 
    Print '== user'elif NUM. 1: 
    Print' worker'elif NUM <0: zero output value is less than # 
    Print 'error'else: 
    Print' when roadman '# output conditions are met


 

The output is:

>>> roadman # output

 

Since python does not support the switch statement, so multiple conditional, can only be used elif be achieved, if the judge requires several conditions to be judged at the same time, you can use or (or) to indicate the condition judging the success of both of these conditions have an established ; use and when (and), represents the only case where two conditions are satisfied, the judgment condition was successful.

! # / usr / bin / python # - * - coding: UTF-8 - * - # Example 3: if a plurality of condition statements num = 9if num> = 0 and num <= 10: # determines whether the value from 0 to 10 between 
    print 'hello' >>> hello # output num = 10if num <0 or num > 10: # determines whether the value is less than 0 or greater than 10 
    Print 'hello'else: 
         Print' undefine '>>> # output undefine results num = 8 # determines whether a value between 0 to 5 or 15 ~ iF 10 (NUM> = 0 and NUM <= 5) or (NUM> NUM = 10 and <= 15):    
    Print 'hello'else: 
    Print 'undefine' >>> undefine # output


 

As if there are multiple conditions may be used to distinguish the determination of the sequence in parentheses, brackets performed by priority, in addition and and or a lower priority than > (greater than), < (less than) symbols and the like is determined, i.e., above and below under no circumstances will the brackets or priority than the judge.

 

Simple statement set

You can also use a position on the same line if conditional statement, the following examples:

! # / usr / bin / Python # - * - Coding: UTF-. 8 - * - 
 var = 100 
 IF (var == 100): Print "value of var 100" 
 "! Good BYE" Print

 

Above code performs output results are as follows:

Variable var the value 100Good bye!

 

More Python Course: Ali Cloud University - developers classroom


Guess you like

Origin blog.51cto.com/13730592/2411596