Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python conditional statement


May 10, 2021 Python2


Table of contents


Python conditional statement

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

The following diagram provides a brief understanding of how conditional statements are executed:

Python conditional statement

The Python program language specifies any non-0 and non-empty (null) values as true, 0 or null as false.

The if statement in Python programming is used to control the execution of the program in the basic form of:

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

Where the "judgment condition" is established (non-zero), the subsequent statement is executed, and the execution content can be multi-line, to shrink in the distinction to represent the same range.

Else is an optional statement, and when you need to execute something when the condition is not valid, you can execute the relevant statement, as follows:

# coding=utf8
# 例1:if 基本用法

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

The output is:

>>> luren         # 输出结果

The judgment conditions of the if statement can be expressed in terms of the relationship of the relationship by being greater than, greater than or equal to, and less than or equal to.

When the condition is more than one value, you can use the following form:

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

Here's an example:

#!/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     # 输出结果

Because python does not support switch statements, multiple condition judgments can only be implemented with elif, which can be determined if multiple conditions are required at the same time, using or (or) to indicate that two conditions have a validity condition, and using and (and) means that the condition succeeds only if two conditions are valid at the same time.

#!/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 has more than one condition, you can use parentheses to distinguish the order of judgment, the judgment in parentheses takes precedence, and the priority of and or is lower than the judgment symbols such as (greater than), slt; (less than) and so on, i.e. greater than and less than in the absence of parentheses will be more than or priority judgment.

A simple group of statements

You can also use the if condition judgment statement at the same line location, as in the following example:

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

The above code executes the output as follows:

变量 var 的值为100
Good bye!