Python non parola chiave è un operatore logico che viene solitamente utilizzato per calcolare la negazione o il valore booleano opposto dell'operando. La parola chiave 'non' è un operatore di tipo unario, il che significa che accetta un solo operando per l'operazione logica e restituisce il complementare del valore booleano dell'operando. Ad esempio, se diamo false come operando alla parola chiave not, otteniamo true come valore restituito.
Sintassi: c'è una nota
Come utilizzare l'operatore Not in Python?
L'operatore not è molto facile da usare. Devi solo usare la parola chiave 'not' davanti alla variabile. Capiamolo meglio con un esempio:
Esempio: Esempio base di operatore not con variabile vera.
Python3
a>=> True> print>(>not> a)> |
>
>
Produzione:
False>
Come puoi vedere dall'esempio sopra, abbiamo semplicemente utilizzato l'operatore not per modificare il valore true in false.
Applicazioni pratiche
Le possibili applicazioni pratiche della parola chiave “non” sono:
- Questa parola chiave viene utilizzata principalmente per modificare il file Viene utilizzato con un se dichiarazione . Viene utilizzato per negare la condizione nell'istruzione if,
- La parola chiave 'not' viene utilizzata anche con ' nella parola chiave ‘. Viene utilizzato con la parola chiave 'in' quando cerchiamo un valore specifico nella raccolta di dati.
Altri esempi su Not Operator
Diamo un'occhiata ad alcuni esempi di operatore not nei codici Python, ogni esempio mostra diversi casi d'uso dell'operatore not.
Python non operatore con variabile
Esempio base di operatore not con variabile.
Python3
# variable> a>=> False> print>(>not> a)> |
>
cosa c'è in Python
>
Produzione:
True>
Utilizzo dell'operatore non booleano in Python con condizione specifica
La proprietà fondamentale della parola chiave 'not' è che viene utilizzata per invertire il valore di verità dell'operando. Quindi qui possiamo vedere che il risultato di ogni valore è invertito rispetto al suo valore reale. Al punto 5 possiamo vedere che il risultato dell'operazione di confronto sarebbe falso, quindi negandolo otteniamo il valore Vero. Allo stesso modo, possiamo vedere che tutti i risultati sono invertiti.
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()> |
>
>
Produzione:
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>
Utilizzo dell'operatore Not con valore diverso
In questo codice mostriamo il funzionamento dell'operatore 'not' con un valore diverso da booleano e vediamo come funziona.
Python3
aprire il menu delle impostazioni
# 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()> |
>
>
Produzione:
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>
Nell'esempio precedente, abbiamo visto che trattando tutti i tipi di dati come operandi con la parola chiave not., 'not' considera true tutti i tipi di dati che avevano valore e false quelli che avevano valore vuoto.
Operatore logico NOT con l'elenco
In questo esempio utilizziamo l'operatore Not con l'elenco:
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()> |
>
>
Produzione:
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>
Abbiamo trattato il significato, la sintassi e gli usi dell'operatore not in Python. Questo potrebbe averti dato il quadro completo di ciò che non è in Python. Puoi guardare gli esempi sopra o sperimentare sul tuo dispositivo il non operatore. È un operatore molto semplice ma utile in Python.
Letture simili
- Operatori logici Python con esempi
- Python IF con operatore NOT
- La differenza tra != e non è un operatore in Python