operator.le()函數 (operator.le() Function)
operator.le() function is a library function of operator module, it is used to perform "less than or equal to operation" on two values and returns True if the first value is less than or equal to the second value, False, otherwise.
operator.le()函數是運算符模塊的庫函數,用于對兩個值執行“小于或等于運算” ,如果第一個值小于或等于第二個值False ,則返回True ,否則返回。
Module:
模塊:
import operator
Syntax:
句法:
operator.le(x,y)
Parameter(s):
參數:
x,y – values to be compared.
x,y –要比較的值。
Return value:
返回值:
The return type of this method is bool, it returns True if x is less than or equal to y, False, otherwise.
此方法的返回類型為bool ,如果x小于或等于y ,則返回True ,否則返回False 。
Example 1:
范例1:
# Python operator.le() Function Example
import operator
# integers
x = 10
y = 20
print("x:",x, ", y:",y)
print("operator.le(x,y): ", operator.le(x,y))
print("operator.le(y,x): ", operator.le(y,x))
print("operator.le(x,x): ", operator.le(x,x))
print("operator.le(y,y): ", operator.le(y,y))
print()
# strings
x = "Apple"
y = "Banana"
print("x:",x, ", y:",y)
print("operator.le(x,y): ", operator.le(x,y))
print("operator.le(y,x): ", operator.le(y,x))
print("operator.le(x,x): ", operator.le(x,x))
print("operator.le(y,y): ", operator.le(y,y))
print()
# printing the return type of the function
print("type((operator.le(x,y)): ", type(operator.le(x,y)))
Output:
輸出:
x: 10 , y: 20
operator.le(x,y): True
operator.le(y,x): False
operator.le(x,x): True
operator.le(y,y): True
x: Apple , y: Banana
operator.le(x,y): True
operator.le(y,x): False
operator.le(x,x): True
operator.le(y,y): True
type((operator.le(x,y)): <class 'bool'>
Example 2:
范例2:
# Python operator.le() Function Example
import operator
# input two numbers
x = int(input("Enter first number : "))
y = int(input("Enter second number: "))
# printing the values
print("x:",x, ", y:",y)
# comparing
if operator.le(x,y):
print(x, "is less than or equal to", y)
else:
print(x, "is not less than or equal to", y)
Output:
輸出:
RUN 1:
Enter first number : 10
Enter second number: 20
x: 10 , y: 20
10 is less than or equal to 20
RUN 2:
Enter first number : 20
Enter second number: 10
x: 20 , y: 10
20 is not less than or equal to 10
翻譯自: https://www.includehelp.com/python/operator-le-function-with-examples.aspx