2012-06-16 10 views
238

あなたはどういうわけではないと思いますか?Pythonに「等しくない」演算子がありますか?

if hi == hi: 
    print "hi" 
elif hi (does not equal) bye: 
    print "no hi" 

同様

は "等しくない" を意味==の置き換えはありますか?

+3

あなたは 'else'、'!= '(オプションで' <> ')または' is not'について質問していますか? – Tadeck

+10

このクエストの検索エンジンランキングは、オンラインドキュメントのランキングよりも高いことに注意してください。したがって、それは依然として有効な質問です。 – f4der

+7

Python 3では、<>はもはや動作しませんので、!= – Antonello

答えて

430

!=を使用してください。 comparison operatorsを参照してください。オブジェクトIDを比較するには、キーワードisとその否定is notを使用できます。

1 == 1 # -> True 
1 != 1 # -> False 
[] is [] #-> False (distinct objects) 
a = b = []; a is b # -> True (same object) 
+49

Python 3から '<>'が削除されたので、 '!='しかないと言いましょう。それを行う方法は1つ、できれば1つだけです;とにかく+1 – Tadeck

+1

新しい文書 - http://docs.python.org/library/stdtypes.html#comparisons - '<>'は、バックコンパチビリティのために2.6と2.7のみであり、 "新しいコードは常に'!= 'を使用するべきです。 – lvc

+11

'<>'はPython 3から削除されていません。 'PEP401'をチェックアウトしてから' '__future__ import barry_as_FLUFL' lol〜 – yegle

39

等しくない!=(等しい==対)

あなたはこのような何かについて尋ねていますか?

answer = 'hi' 

if answer == 'hi':  # equal 
    print "hi" 
elif answer != 'hi': # not equal 
    print "no hi" 

このPython - Basic Operatorsチャートが参考になる場合があります。

17

二つの値は、種類に注意してくださいしかし、異なる場合Trueを返し!=(等しくない)演算子はありますが、これは常にtrueを返しますと型が異なるため"1" == 1は常に、Falseを返します、Pythonは動的けど強く"1" != 1を引き起こします他の静的型付けされた言語は、異なる型の比較について不平を言うでしょう。

Theresのもelse

# this will always print either "hi" or "no hi" unless something unforseen happens. 
if hi == "hi":  # the variable hi is being compared to the string "hi", strings are immutable in python so you could use the is operator. 
    print "hi"  # if indeed it is the string "hi" then print "hi" 
else:    # hi and "hi" are not the same 
    print "no hi" 

isオペレータは、実際には2つのオブジェクトが同じであるかどうかを確認するために使用object identity演算子です:

a = [1, 2] 
b = [1, 2] 
print a == b # This will print True since they have the same values 
print a is b # This will print False since they are different objects. 
4

は皆、すでに記載されているたとして見て最も同等ではないと言う他の方法のうち、私はちょうど追加するでしょう:

if not (1) == (1): # This will eval true then false 
    # (ie: 1 == 1 is true but the opposite(not) is false) 
    print "the world is ending" # This will only run on a if true 
elif (1+1) != (2): #second if 
    print "the world is ending" 
    # This will only run if the first if is false and the second if is true 
else: # this will only run if the if both if's are false 
    print "you are good for another day" 

それが負に正==(真)のチェックを切り替えるシンプルで、その逆。この場合...

-3

は、あなたは、単に行うことができます。

if hi == hi: 
    print "hi" 
elif hi != bye: 
    print "no hi" 
+0

変数 'hi'と' bye'にどのような値を代入しますか?それが何であれ、エリフ句は決して達成されないだろう。最後に、この例では質問に対する回答が明確に示されていません。 –

3

あなたは!=または<>両方を使用することができます。

ただし、<>は推奨されていませんが、!=が推奨されます。

0

!=または<>を使用してください。どちらも等しくない。

比較演算子<>!=は、同じ演算子の代わりのスペルです。 !=が好ましいスペルです。 <>は廃止されました。 [参考:Python言語リファレンス]

+0

この回答は基本的に前に与えた@ user128364のコピーです。 –

-1

"等しくない" 状態のためのpythonに2つの演算子がある -

)。!= 2つのオペランドの値が等しくない場合、conditionはtrueになります。 (a!= b)がtrueです。

b)<> 2つのオペランドの値が等しくない場合、conditionはtrueになります。 (a <> b)が真です。これは!=演算子に似ています。

+1

Python 3では '<>'が削除されました。いずれの場合でも、この回答は新しい情報を提供しません。 – vaultah

関連する問題