Python nav atslēgvārds ir loģisks operators, ko parasti izmanto, lai noskaidrotu operanda noliegumu vai pretējo Būla vērtību. Atslēgvārds 'nē' ir unāra tipa operators, kas nozīmē, ka loģiskajai darbībai nepieciešams tikai viens operands un atgriež operanda Būla vērtības komplementāru. Piemēram, ja mēs piešķiram false kā operandi atslēgvārdam not, mēs saņemam true kā atgriešanās vērtību.
Sintakse: ir piezīme
Kā Python lietot ne operatoru?
Nav operators ir ļoti viegli izmantot. Jums vienkārši jāizmanto atslēgvārds “not” mainīgā priekšā. Izpratīsim to labāk ar piemēru:
Piemērs: Pamatpiemērs nav operatoram ar patieso mainīgo.
Python3
a>=> True> print>(>not> a)> |
>
>
Izvade:
False>
Kā redzat no iepriekš minētā piemēra, mēs vienkārši izmantojām nevis operatoru, lai mainītu patieso vērtību uz false.
Praktiski pielietojumi
Atslēgvārda “not” iespējamie praktiskie pielietojumi ir šādi:
- Šo atslēgvārdu galvenokārt izmanto, lai mainītu To lieto ar ja paziņojums . To izmanto, lai noliegtu nosacījumu if paziņojumā,
- Atslēgvārds “not” tiek lietots arī ar “ atslēgvārdā ‘. To lieto kopā ar atslēgvārdu “in”, kad mēs meklējam konkrētu vērtību datu vākšanā.
Vairāk piemēru vietnē Not Operator
Apskatīsim dažus piemērus, ka Python kodos nav operatora, katrs piemērs parāda dažādus ne operatora lietošanas gadījumus.
Python nav operators ar mainīgo
Pamatpiemērs ne operatoram ar mainīgo.
Python3
# variable> a>=> False> print>(>not> a)> |
>
Aktieris Reha
>
Izvade:
True>
Izmantojot nevis Būla operatoru Python ar īpašu nosacījumu
Atslēgvārda “not” galvenā īpašība ir tāda, ka to izmanto, lai apgrieztu operanda patiesības vērtību. Tātad mēs varam redzēt, ka katras vērtības rezultāts ir apgriezts no to patiesās vērtības. Pie #5 mēs varam redzēt, ka salīdzināšanas darbības rezultāts ir nepatiess, tāpēc, noliedzot to, mēs iegūstam patieso vērtību. Tāpat mēs varam redzēt, ka visi rezultāti ir apgriezti.
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> >>> >print>(>'Negation of result of And operation : '>, geek_Com)> geek_Func()> |
kārtot masīvu sarakstu
>
>
Izvade:
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 Operator izmantošana ar atšķirīgu vērtību
Šajā kodā mēs parādām operatora “not” darbību ar atšķirīgu vērtību, nevis Būla vērtību, un redzam, kā tas darbojas.
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()> |
>
>
Izvade:
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>
Iepriekš minētajā piemērā mēs redzējām, ka, apstrādājot visus datu tipus kā operandus ar nevis atslēgvārdu., “not” tiek uzskatīts par patiesu visiem datu veidiem, kuriem bija vērtība, un false pret tiem, kuriem vērtība bija tukša.
Loģisks NOT operators ar sarakstu
Šajā piemērā mēs izmantojam operatoru Not ar sarakstu:
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()> |
>
>
Izvade:
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>
Mēs esam apskatījuši Python operatora not nozīmi, sintaksi un lietojumus. Tas, iespējams, sniedza jums pilnīgu priekšstatu par to, kas nav Python. Varat apskatīt iepriekš minētos piemērus vai eksperimentēt savā ierīcē par to, vai tas nav operators. Tas ir ļoti vienkāršs, taču noderīgs Python operators.
Līdzīgi lasījumi
- Python loģiskie operatori ar piemēriem
- Python IF ar NOT operatoru
- Atšķirība starp != un nav operators Python