logo

no Operador en Python | Lògica booleana

Python no paraula clau és un operador lògic que s'utilitza normalment per esbrinar la negació o el valor booleà oposat de l'operand. La paraula clau 'no' és un operador de tipus unari, el que significa que només pren un operand per a l'operació lògica i retorna el complementari del valor booleà de l'operand. Per exemple, si donem false com a operand a la paraula clau not, obtenim true com a valor de retorn.

Sintaxi: hi ha una nota

Com utilitzar Not Operator a Python?

El no operador és molt fàcil d'utilitzar. Només heu d'utilitzar la paraula clau 'no' davant de la variable. Entenem-ho millor amb un exemple:



Exemple: Exemple bàsic de no operador amb variable veritable.

Python 3




a>=> True> print>(>not> a)>

>

>

Sortida:

False>

Com podeu veure a l'exemple anterior, només hem utilitzat l'operador not per canviar el valor true a false.

Aplicacions pràctiques

Les possibles aplicacions pràctiques de la paraula clau 'no' són:

  1. Aquesta paraula clau s'utilitza principalment per modificar el S'utilitza amb un declaració si . S'utilitza per negar la condició a la instrucció if,
  2. La paraula clau 'no' també s'utilitza amb ' en paraula clau ‘. S'utilitza amb la paraula clau 'in' quan cerquem un valor específic en la recollida de dades.

Més exemples sobre Not Operator

Vegem alguns exemples d'operador no als codis de Python, cada exemple mostra diferents casos d'ús de l'operador no.

Python no operador amb variable

Exemple bàsic de no operador amb variable.

Python 3




# variable> a>=> False> print>(>not> a)>

>

llenguatge màquina

>

Sortida:

True>

Utilitzant l'operador no booleà a Python amb una condició específica

Com a propietat bàsica de la paraula clau 'no' és que s'utilitza per invertir el valor de veritat de l'operand. Així que podem veure aquí que el resultat de cada valor s'inverteix del seu valor real. Al número 5 podem veure que el resultat de l'operació de comparació seria fals, de manera que en negar-lo obtenim el valor Vertader. De la mateixa manera, podem veure que tots els resultats estan invertits.

Python 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()>

>

>

Sortida:

estampat d'estrelles
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>

Utilitzant l'operador Not amb un valor diferent

En aquest codi, mostrem el funcionament de l'operador 'no' amb un valor diferent de booleà i veiem com funciona.

Python 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()>

>

>

Sortida:

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>

A l'exemple anterior, vam veure que tractar tots els tipus de dades com a operands amb not keyword., 'not' tracta de cert a tots els tipus de dades que tenien valor i fals als que tenien valor buit.

Operador NOT lògic amb la llista

Aquí, en aquest exemple, estem utilitzant l'operador Not amb la llista:

Python 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()>

>

>

Sortida:

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>

Hem tractat el significat, la sintaxi i els usos de l'operador not a Python. Això podria haver-vos donat la imatge completa del que no hi ha a Python. Podeu mirar els exemples anteriors o experimentar amb el vostre dispositiu sobre no operador. És un operador molt bàsic però útil en Python.

Llegiu-ne més Operador Python

Lectures similars