Python conditional statements

Python conditional statements are blocks of code that are executed by the execution result of one or more statements (True or False).

The following figure can be used to briefly understand the execution of conditional statements:

The Python programming language specifies any non-zero and non-null values ​​to be true, 0 or null to false.

In Python programming, an if statement is used to control the execution of a program. The basic form is:

if Conditions: < /span>
    Execute statement...
else:
    Execute statement...

When the "judgment condition" is established (non-zero), the following statement is executed, and the execution content can be multi-line, and the indentation is used to distinguish the same range.

else is an optional statement. When you need to execute the content when the condition is not met, you can execute the related statement. The specific examples are as follows:

Instance

#!/usr/bin/python # -*- coding: UTF-8 -*- # Example 1: If basic usage flag= False< Span class="hl-code"> name = '< Span class="hl-string">luren' if name == 'python': # Determine if the variable is 'python' flag= True< Span class="hl-code"> # Set the flag to true when the condition is met print 'welcome boss' # and output welcome message else: print name #output variable name when condition is not met

The output is:

luren            < Span class="com"># output results

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

When the judgment condition is multiple values, the following form can be used:

if Critical conditions1:
    Execute statement1...
elif Conditions2:
    execute statement2...
elif Conditions3:
    Execute statement3...
else:
    execute statement4...

Examples are as follows:

Instance

#!/usr/bin/python # -*- coding: UTF-8 -*- # Example 2: Elif usage num = 5 if num == 3: # Determine the value of num print 'boss' elif num == 2: print 'user' elif num == 1: print 'worker' elif num < 0: # Output when the value is less than zero print 'error' else: print 'roadman' # Output when the conditions are not met

The output is:

roadman        # Output 

Because Python does not support switch statements, multiple conditional judgments can only be implemented with elif. If it is judged that multiple conditions need to be judged at the same time, you can use or (or) to indicate that one of the two conditions is true. The judgment condition is successful; when using and, it means that only two conditions are satisfied at the same time, the judgment condition is successful.

Instance

#!/usr/bin/python # -*- coding: UTF-8 -*- # Example 3:If statement multiple conditions num = 9 if num >= 0 and num <= 10: # Determine if the value is 0~Between 10 print 'hello' # Output result: hello num = 10 if num < 0 or num > 10: # Determine if the value is less than 0 or greater than 10 print 'hello' else: print 'undefine' # Output result: undefine num = 8 # Whether the judgment value is between 0~5 or 10~15 if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): print 'hello' else: print 'undefine' # Output result: undefine

When if there are multiple conditions, parentheses can be used to distinguish the order of judgment. The judgments in parentheses are executed first. In addition, the priority of and and or is lower than the judgment symbols such as (greater than) and < (less than). That is, greater than and less than in the case of no parentheses will be compared with or with priority.

Simple statement group

You can also use the if condition to determine statements on the same line, as in the following example:

Instance

#!/usr/bin/python # -*- coding: UTF-8 -*- var = 100 if ( var == 100 ) : print "variable var The value is 100" print "Good bye!"

The above code execution output is as follows:

variable var Value100
Good bye!





welookups is optimized for learning.© welookups .
All Right Reserved and you agree to have read and accepted our term and condition