如何在Python中用if语句编写条件性分支

商业

解释Python中用if语句进行的条件性分支。

  • if语句的基础知识(if、elif、else)。
  • 用比较运算符指定条件,等等。
  • 通过数字、列表等指定条件。
  • 用逻辑运算符(和、或、非)指定多个条件或否定。
  • 新行和多行的条件表达式

还有一个三元运算符,可以在一行中描述一个条件性分支。见以下文章。

if语句的基础知识(if、elif、else)。

if语句的基本形式如下

if Conditional expression 1:
    `Processing to be performed if Expression 1 is True.`
elif Conditional expression 2:
    `Processing to be performed when expression 1 is false and expression 2 is true.`
elif Expression 3:
    `Process when expression 1 and 2 are false and expression 3 is true.`
...
else:
    `Processing when all conditionals are false.`

elif “对应于C语言和其他语言中的 “else if”,可能有任意数量的 “elif”。

如果只有一个条件表达式,或者不需要处理false时,”elif “和 “else “块可以省略。

用比较运算符指定条件,等等。

用一个返回bool类型(真,假)的操作来指定条件,例如比较运算符。

Python的比较运算符如下

经营者结果
x < y如果x小于y为真
x <= y如果x小于或等于y,则为真
x > y如果x大于y为真
x >= y如果x大于或等于y为真
x == y如果x和y的值相等,则为真
x != y如果x和y值不相等,则为真x is y如果x和y是同一个对象,则为truex is not y如果x和y不是同一个对象,则为truex in y如果x包含在y中,则为真x not in y如果x不包含在y中,则为真

例子。为方便起见,它被定义为一个带有def语句的函数。

def if_test(num):
    if num > 100:
        print('100 < num')
    elif num > 50:
        print('50 < num <= 100')
    elif num > 0:
        print('0 < num <= 50')
    elif num == 0:
        print('num == 0')
    else:
        print('num < 0')

if_test(1000)
# 100 < num

if_test(70)
# 50 < num <= 100

if_test(0)
# num == 0

if_test(-100)
# num < 0

下面的内容可以用Python特有的方式来写。详见下面的文章。
a < x < b

def if_test2(num):
    if 50 < num < 100:
        print('50 < num < 100')
    else:
        print('num <= 50 or num >= 100')

if_test2(70)
# 50 < num < 100

if_test2(0)
# num <= 50 or num >= 100
  • ==
  • !=

以上是数值的比较,如果要比较对象的身份,请使用以下方法

  • is
  • is not

例如,当比较一个整数和一个浮点数时,如果数值相等,”==”返回真,但 “是 “返回假,因为它们是不同的对象。

i = 10
print(type(i))
# <class 'int'>

f = 10.0
print(type(f))
# <class 'float'>

print(i == f)
# True

print(i is f)
# False

也可以将一个列表或一个字符串是否包含一个特定的元素(字符)作为条件。

  • in:包括
  • not in:不包括
def if_test_in(s):
    if 'a' in s:
        print('a is in string')
    else:
        print('a is NOT in string')

if_test_in('apple')
# a is in string

if_test_in('melon')
# a is NOT in string

通过数字、列表等指定条件。

if语句的条件表达式可以是一个数字、列表或其他不属于bool类型(真、假)的对象。

if 10:
    print('True')
# True

if [0, 1, 2]:
    print('True')
# True

在Python if 语句的条件表达式中,以下对象被认为是假的。

代表零、空字符串、列表等的数字被认为是假的;所有其他数字被认为是真的。

如何判断该对象,可以用bool()检查。

print(bool(10))
# True

print(bool(0.0))
# False

print(bool([]))
# False

print(bool('False'))
# True

例如,这可以用来在列表为空时简单地写入进程。

def if_test_list(l):
    if l:
        print('list is NOT empty')
    else:
        print('list is empty')

if_test_list([0, 1, 2])
# list is NOT empty

if_test_list([])
# list is empty

注意,字符串'False'也将是真实的,因为如上面的例子所示,任何在字符串中不为空的字符串都是真实的'。要将特定的字符串,如'True'或'False'转换为1,0,请使用distutils.util模块中的strtobool()。

用逻辑运算符(和、或、非)指定多个条件或否定。

逻辑运算符(and, or, not)可以用来处理逻辑连线、逻辑分离和多个条件的否定。

经营者结果(在if语句的条件表达中)。
x and y如果x和y都是真的,则为真
x or y如果x或y为真,则为真
not x如果x为真,则为假,如果x为假,则为真
def if_test_and_not(num):
    if num >= 0 and not num % 2 == 0:
        print('num is positive odd')
    else:
        print('num is NOT positive odd')

if_test_and_not(5)
# num is positive odd

if_test_and_not(10)
# num is NOT positive odd

if_test_and_not(-10)
# num is NOT positive odd

事实上,”x和y “和 “x或y “并不返回真或假,而是返回x或y。只要它们被用于if语句中的条件表达式,就没有必要担心它们,因为它们评估的结果是真或假。详见以下文章。

可以不止一次地使用和和或。

def if_test_and_not_or(num):
    if num >= 0 and not num % 2 == 0 or num == -10:
        print('num is positive odd or -10')
    else:
        print('num is NOT positive odd or -10')

if_test_and_not_or(5)
# num is positive odd or -10

if_test_and_not_or(10)
# num is NOT positive odd or -10

if_test_and_not_or(-10)
# num is positive odd or -10

新行和多行的条件表达式

当用 “和 “或 “或 “连接多个条件表达式,并且每行都变得很长时,有时需要将条件表达式断开,写在多行上。

断行可以通过使用反斜杠或用圆括号括住整个行来实现。

def if_test_and_backslash(num):
    if num >= 0 \
       and not num % 2 == 0:
        print('num is positive odd')
    else:
        print('num is NOT positive odd')

if_test_and_backslash(5)
# num is positive odd

def if_test_and_brackets(num):
    if (num >= 0
        and not num % 2 == 0):
        print('num is positive odd')
    else:
        print('num is NOT positive odd')

if_test_and_brackets(5)
# num is positive odd

你可以使用反斜杠来中断一个行,次数不限。同样,你也可以在圆括号内任意断开一行。没有缩进的限制。

注意,这是一个可以在Python代码中任何地方使用的技术,而不仅仅是在if语句中。

Copied title and URL