Python이 아닌 키워드 피연산자의 부정 또는 반대 부울 값을 알아내는 데 일반적으로 사용되는 논리 연산자입니다. 키워드 '아니다' 단항 유형 연산자입니다. 즉, 논리 연산에 하나의 피연산자만 사용하고 피연산자의 부울 값의 보수를 반환합니다. 예를 들어, not 키워드에 피연산자로 false를 제공하면 반환 값으로 true를 얻습니다.
통사론: 메모가 있어요
Python에서 Not 연산자를 사용하는 방법은 무엇입니까?
not 연산자는 사용하기 매우 쉽습니다. 변수 앞에 'not' 키워드를 사용하면 됩니다. 예를 들어 더 잘 이해해 봅시다:
예: true 변수를 사용하는 not 연산자의 기본 예입니다.
파이썬3
a>=> True> print>(>not> a)> |
>
>
산출:
False>
위의 예에서 볼 수 있듯이 true 값을 false로 변경하기 위해 not 연산자를 사용했습니다.
실용적인 적용
'not' 키워드의 가능한 실제 적용은 다음과 같습니다.
- 이 키워드는 주로 이는 다음과 함께 사용됩니다. if 문 . if 문에서 조건을 부정하는 데 사용됩니다.
- 'not' 키워드는 '와 함께 사용되기도 합니다. 키워드로 '. 수집된 데이터에서 특정 값을 검색할 때 'in' 키워드와 함께 사용됩니다.
Not 연산자에 대한 추가 예
Python 코드에서 not 연산자의 몇 가지 예를 살펴보겠습니다. 각 예는 not 연산자의 다양한 사용 사례를 보여줍니다.
Python은 변수를 사용하는 연산자가 아닙니다.
변수가 있는 not 연산자의 기본 예입니다.
파이썬3
# variable> a>=> False> print>(>not> a)> |
>
알리사 만요녹
>
산출:
True>
특정 조건으로 Python에서 not 부울 연산자 사용
'not' 키워드의 기본 속성은 피연산자의 진리값을 반전시키는 데 사용된다는 것입니다. 따라서 여기서는 모든 값의 결과가 실제 값에서 반전되는 것을 볼 수 있습니다. #5에서는 비교 연산 결과가 false임을 알 수 있으므로 이를 부정하면 True 값을 얻습니다. 마찬가지로 모든 결과가 반전된 것을 볼 수 있습니다.
파이썬3
# 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' 연산자의 작동 방식을 보여주고 작동 방식을 확인합니다.
파이썬3
# 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()> |
안드로이드 프로세스 acore가 계속 중지됩니다.
>
>
산출:
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'은 값이 있는 모든 데이터 유형에 대해 true를 처리하고 비어 있는 값인 데이터 유형에 대해 false를 처리합니다.
목록이 포함된 논리 NOT 연산자
이 예에서는 목록에 Not 연산자를 사용하고 있습니다.
파이썬3
# 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에서 매우 기본적이면서도 유용한 연산자입니다.
유사한 읽기