python条件运算符
如果条件运算符 (if else conditional operator)
Just like other programming languages, Python also provides the feature to evaluate conditional statements using the conditional operator.
与其他编程语言一样,Python也提供了使用条件运算符评估条件语句的功能。
In this tutorial, we will see the if else conational operator.
在本教程中,我们将看到if else conational运算符。
if else conditional operator is used to evaluate/get either value/statement (from the given two values/statements) depending on the result of the given Boolean expression.
如果不是,则根据给定布尔表达式的结果,使用条件运算符评估/获取值/声明(从给定的两个值/声明)。
Syntax:
句法:
value1 if expression else value2
Here,
这里,
value1 – represents the value for the conditional expression if it is True.
value1 –表示条件表达式的值为True。
expression – represents the condition that must be evaluated to a Boolean (i.e. we can say it is a condition)
表达式 –表示必须评估为布尔值的条件(即我们可以说这是一个条件)
value2 – represents the value for the conditional expression if it is False.
value2 –表示条件表达式的值为False。
In simple words, we can say – if the expression is True, value1 will be returned and if it is False, value2 will be returned.
简单来说,我们可以说–如果表达式为True,则将返回value1 ,如果表达式为False,则将返回value2 。
Example 1: Printing the largest value among two values
示例1:打印两个值中的最大值
# find the largest Value
x = 20
y = 10
# if else conditional operator
largest = x if x>y else y
# printing the values
print("x: ", x)
print("y: ", y)
print("largest: ", largest)
print()
x = 10
y = 20
# if else conditional operator
largest = x if x>y else y
# printing the values
print("x: ", x)
print("y: ", y)
print("largest: ", largest)
print()
x = 10
y = 10
# if else conditional operator
largest = x if x>y else y
# printing the values
print("x: ", x)
print("y: ", y)
print("largest: ", largest)
print()
Output:
输出:
x: 20
y: 10
largest: 20
x: 10
y: 20
largest: 20
x: 10
y: 10
largest: 10
Example 2: Printing the largest value among three values (It's an example of nested if else conditional operator)
示例2:打印三个值中的最大值(这是一个嵌套的,如果不是条件运算符的例子)
# find the largest Value
x = 10
y = 20
z = 30
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and x>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
x = 10
y = 30
z = 20
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and y>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
x = 30
y = 20
z = 10
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and y>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
x = 10
y = 10
z = 10
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and y>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
Output:
输出:
x: 10
y: 20
z: 30
largest: 30
x: 10
y: 30
z: 20
largest: 30
x: 30
y: 20
z: 10
largest: 30
x: 10
y: 10
z: 10
largest: 10
翻译自: https://www.includehelp.com/python/if-else-conditional-operator.aspx
python条件运算符