當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python operator.eq()用法及代碼示例


operator.eq() 函數

operator.eq()函數是一個庫函數operator模塊,用於對兩個值執行“等於操作”並返回True如果第一個值等於第二個值,False,否則。

模塊:

    import operator

用法:

    operator.eq(x,y)

參數:

  • x,y– 要比較的值。

返回值:

這個方法的返回類型是bool,它返回True如果x等於yFalse,否則。

範例1:

# Python operator.eq() Function Example

import operator

# integers
x = 10
y = 20

print("x:",x, ", y:",y)
print("operator.eq(x,y):", operator.eq(x,y))
print("operator.eq(y,x):", operator.eq(y,x))
print("operator.eq(x,x):", operator.eq(x,x))
print("operator.eq(y,y):", operator.eq(y,y))
print()

# strings
x = "Apple"
y = "Banana"

print("x:",x, ", y:",y)
print("operator.eq(x,y):", operator.eq(x,y))
print("operator.eq(y,x):", operator.eq(y,x))
print("operator.eq(x,x):", operator.eq(x,x))
print("operator.eq(y,y):", operator.eq(y,y))
print()

# printing the return type of the function
print("type((operator.eq(x,y)):", type(operator.eq(x,y)))

輸出:

x:10 , y:20
operator.eq(x,y): False
operator.eq(y,x): False
operator.eq(x,x): True
operator.eq(y,y): True

x:Apple , y:Banana
operator.eq(x,y): False
operator.eq(y,x): False
operator.eq(x,x): True
operator.eq(y,y): True

type((operator.eq(x,y)): <class 'bool'>

範例2:

# Python operator.eq() 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.eq(x,y):
  print(x, "is equal to", y)
else:
  print(x, "is not equal to", y)

輸出:

RUN 1:
Enter first number:10
Enter second number:10
x:10 , y:10
10 is equal to 10

RUN 2:
Enter first number:10
Enter second number:20
x:10 , y:20
10 is not equal to 20


相關用法


注:本文由純淨天空篩選整理自 Python operator.eq() Function with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。