Python はキーワードではありません は、オペランドの否定または反対のブール値を計算するために通常使用される論理演算子です。キーワード 'ない' これは、論理演算にオペランドを 1 つだけ取り、オペランドのブール値の補数を返すことを意味する単項演算子です。たとえば、not キーワードのオペランドとして false を指定すると、戻り値として true が返されます。
構文: メモがあります
PythonでNot演算子を使用するには?
not 演算子は非常に使いやすいです。変数の前に「not」キーワードを使用するだけです。例を挙げて理解を深めてみましょう。
例: true 変数を使用した not 演算子の基本的な例。
Python3
a>=> True> print>(>not> a)> |
>
>
出力:
False>
上の例からわかるように、not 演算子を使用して true 値を false に変更しただけです。
実用的なアプリケーション
「not」キーワードの実際の応用例は次のとおりです。
- このキーワードは主に、 と一緒に使用されます if ステートメント 。 if文の条件を否定するために使用します。
- 「not」キーワードは、「 キーワードで 「。」データのコレクション内の特定の値を検索する場合、「in」キーワードとともに使用されます。
Not 演算子のその他の例
Python コードの not 演算子の例をいくつか見てみましょう。各例は not 演算子のさまざまな使用例を示しています。
Python は変数を持つ演算子ではありません
変数を使用した not 演算子の基本的な例。
Python3
# variable> a>=> False> print>(>not> a)> |
>
機械学習とその型
>
出力:
True>
Python で特定の条件で非ブール演算子を使用する
「not」キーワードの基本的な特性として、オペランドの真理値を反転するために使用されることが挙げられます。したがって、ここでは、すべての値の結果が真の値から反転していることがわかります。 #5 では、比較演算の結果が false になることがわかり、それを否定することで True の値が得られます。同様に、すべての結果が反転していることがわかります。
Python3
# Python code to demonstrate> # 'not' keyword> # Function showing working of not keyword> def> geek_Func():> > ># 1 Not with False boolean value> >geek_x>=> not> False> >print>(>'Negation of False : '>, geek_x)> ># 2 Not with true boolean value> >geek_y>=> not> True> >print>(>'Negation of True : '>, geek_y)> ># 3 Not with result of and operation> >geek_and>=> not>(>True> and> False>)> >print>(>'Negation of result of And operation : '>, geek_and)> ># 4 Not with result of or operation> >geek_or>=> not>(>True> or> False>)> >print>(>'Negation of result of or operation : '>, geek_or)> ># 5 Not with result of compare operation> >geek_Com>=> not> (>5> >>>7>)> >print>(>'Negation of result of And operation : '>, geek_Com)> geek_Func()> |
>
>
出力:
Negation of False : True Negation of True : False Negation of result of And operation : True Negation of result of or operation : False Negation of result of And operation : True>
異なる値で Not 演算子を使用する
このコードでは、ブール値以外の異なる値を使用した「not」演算子の動作を示し、それがどのように動作するかを確認します。
Python3
# Python code to demonstrate> # 'not' keyword> # Function showing working of not keyword> def> geek_Func():> > ># Not with String boolean value> >geek_Str>=> 'geek'> >print>(>'Negation of String : '>,>not> geek_Str)> ># Not with list boolean value> >geek_List>=> [>1>,>2>,>3>,>4>]> >print>(>'Negation of list : '>,>not> geek_List)> ># Not with dictionary> >geek_Dict>=> {>'geek'>:>'sam'>,>'collage'>:>'Mit'>}> >print>(>'Negation of dictionary : '>,>not> geek_Dict)> ># Not with Empty String> >geek_EDict>=> ''> >print>(>'Negation of Empty String : '>,>not> geek_EDict)> ># Not with Empty list> >geek_EList>=> []> >print>(>'Negation of Empty List : '>,>not> geek_EList)> ># Not with Empty dictionary> >geek_EStr>=> {}> >print>(>'Negation of Empty Dictionary : '>,>not> geek_EStr)> geek_Func()> |
>
>
出力:
Negation of String : False Negation of list : False Negation of dictionary : False Negation of Empty String : True Negation of Empty List : True Negation of Empty Dictionary : True>
上の例では、not キーワードを使用してすべてのデータ型をオペランドとして扱うことがわかりました。「not」は、値を持つすべてのデータ型に対して true を扱い、空の値を持つデータ型に対しては false を扱います。
リストを使用した論理 NOT 演算子
この例では、リストで Not 演算子を使用しています。
Python3
# Python code to demonstrate> # 'not' keyword> geek_list>=> [>5>,>10>,>20>,>59>,>134>,>83>,>95>]> # Function showing working of not keyword> def> geek_Func():> > ># Using not with if statement> >if> not> geek_list:> >print>(>'Inputted list is Empty'>)> >else>:> >for> i>in> geek_list:> >if> not>(i>%> 5>):> > ># Using not with in statement> >if> i>not> in> (>0>,>10>):> >print>(>'Multiple is not in range'>)> >else>:> >print>(i)> >else>:> >print>(>'The number is not multiple of 5'>)> geek_Func()> |
>
>
出力:
Multiple is not in range 10 MUltiple is not in range The number is not multiple of 5 The number is not multiple of 5 The number is not multiple of 5 Multiple is not in range>
Python の not 演算子の意味、構文、使用法について説明しました。これで、Python にないものの全体像がわかったかもしれません。上記の例を確認するか、デバイス上で not オペレーターについて実験してください。これは、Python の非常に基本的でありながら便利な演算子です。
BFS検索
類似の記事