Latest web development tutorials

Python Conditional statements

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

The following figure can be a simple understanding of the execution of the conditional statement:

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

Python programming if statement is used to execute the control program, the basic form:

if 判断条件:
    执行语句……
else:
    执行语句……

Wherein when the "determination condition" was established (non-zero), then the statement following the execution and implementation of the contents can be multi-line, indented to distinguish represent the same range.

else statement is optional, 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 -*-

# 例1:if 基本用法

flag = False
name = 'luren'
if name == 'python':         # 判断变量否为'python'
    flag = True          # 条件成立时设置标志为真
    print 'welcome boss'    # 并输出欢迎信息
else:
    print name              # 条件不成立时输出变量名称

The output is:

>>> luren			# 输出结果

Analyzing conditions if statement can be> (greater than), <(less than), == (equal to),> = (greater than or equal), <= (less than or equal) to represent the relationship.

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

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

Examples are as follows:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例2:elif用法

num = 5     
if num == 3:            # 判断num的值
    print 'boss'        
elif num == 2:
    print 'user'
elif num == 1:
    print 'worker'
elif num < 0:           # 值小于零时输出
    print 'error'
else:
    print 'roadman'     # 条件均不成立时输出

The output is:

>>> roadman		# 输出结果

Since python does not support the switch statement, so multiple conditional, elif can only be achieved, if the judge needs to simultaneously determine multiple conditions, you can use or (or), expressed the determination condition when two conditions have successfully established a ; use and when (and) represents the case of only two conditions are true, the condition was judged successful.

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 例3:if语句多个条件

num = 9
if num >= 0 and num <= 10:    # 判断值是否在0~10之间
    print 'hello'
>>> hello		# 输出结果

num = 10
if num < 0 or num > 10:    # 判断值是否在小于0或大于10
    print 'hello'
else:
	print 'undefine'
>>> undefine		# 输出结果

num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):    
    print 'hello'
else:
    print 'undefine'
>>> undefine		# 输出结果

When if there are multiple conditions may use parentheses to distinguish determine the order in parentheses judgment takes precedence, and addition and or lower priority than the> (greater than), <(less than) and other judges symbol, above and below under no circumstances brackets or priority than the judge.

Simple statement group

You can also use conditional statements in the position if the same row on the following examples:

#!/usr/bin/python 
# -*- coding: UTF-8 -*-
 
var = 100 
 
if ( var  == 100 ) : print "变量 var 的值为100" 
 
print "Good bye!" 

The above code is executed the following output:

变量 var 的值为100
Good bye!