python 条件运算符
Python provides some conditionals to compare two python data or variables. We can compare same or similar variables and data then we will get result which present the comparison result. The comparison results will be true
or false
. In this tutorial we will look most popular comparison operations in python.
Python提供了一些条件来比较两个python数据或变量。 我们可以比较相同或相似的变量和数据,然后得到表示比较结果的结果。 比较结果将为true
或false
。 在本教程中,我们将介绍python中最流行的比较操作。
少于 (Less Than)
Less than or <
is a mathematical operator used in python. There is other uses than mathematic. For example we can compare two dates with less than operator. This operator is generally used to compare two integers or float numbers and returns result as boolean True
or False
. We will use <
operator.
小于或<
是python中使用的数学运算符。 除数学外,还有其他用途。 例如,我们可以比较两个小于日期的日期。 此运算符通常用于比较两个整数或浮点数,并以布尔T rue
或False
返回结果。 我们将使用<
运算符。
4<5
#This will return false
5<4
#This will return true because 'a' is a string ASCII char and converted to the number
5<'a'
#This will raise exception because a is not a comparable type
5<a
比...更棒(Greater Than)
Greater than is the reverse of the less than operator. This operator will generally used to compare numbers but also can be used to compare dates. We will use >
as greater operator.
大于大于是小于运算符的逆。 该运算符通常用于比较数字,但也可以用于比较日期。 我们将使用>
作为更大的运算符。
5 > 4
#This will return true
5 > 4
#This will return false because 'a' is a string ASCII char and converted to the number
5 > 'a'
#This will raise exception because a is not a comparable type
5 > a
小于或等于(Less Than or Equal)
We may need to compare two numbers or dates but also check whether they are equal. In this situations we will use Less than or equal operator which is a combination of less than and equal operators. We will use <=
as less than or equal operator.
我们可能需要比较两个数字或日期,但还要检查它们是否相等。 在这种情况下,我们将使用小于或等于运算符,它是小于和等于运算符的组合。 我们将<=
用作小于或等于运算符。
5 <= 4
4 <= 4
大于或等于(Greater Than or Equal)
We can combine equal operator with greater than operator too. This will return boolean value True
or False
according to the situation. If the first value is greater or equal to second value this will return true if not false.
我们也可以将大于或等于运算符组合在一起。 这将根据情况返回布尔值True
或False
。 如果第一个值大于或等于第二个值,则如果不为假,则返回true。
4 >=5
5 >=5
等于(Equals)
Equal is very popular comparison operator. This will check whether given values or variables are equal. Also Equal can be used for date values. We will provide both variables or values around ==
operator.
等于是非常流行的比较运算符。 这将检查给定的值或变量是否相等。 等于也可以用于日期值。 我们将在==
运算符周围提供变量或值。
5 == 5
5 == 4
不平等(Not Equal)
Not equal operator is the reverse of the equal operator. With this operator we will check whether two values or variables are not the same. If they are not same True
boolean result is returned it not False
is returned. We will use !=
as not equal operator.
不等于运算符与等于运算符相反。 使用此运算符,我们将检查两个值或变量是否不相同。 如果它们不同,则返回True
布尔结果,而不是False
。 我们将!=
用作不等于运算符。
5 != 4
5 != 5
翻译自: https://www.poftut.com/python-conditionals-like-greater-lower-equal-operators-examples/
python 条件运算符