logo

Operatorji Python

Uvod:

V tem članku razpravljamo o operaterjih Python. Operator je simbol, ki izvaja določeno operacijo med dvema operandoma v skladu z eno definicijo. Operatorji služijo kot osnova, na kateri je zgrajena logika v programu v določenem programskem jeziku. V vsakem programskem jeziku nekateri operaterji opravljajo več nalog. Tako kot drugi jeziki ima tudi Python nekaj operatorjev, ki so navedeni spodaj -

  • Aritmetični operatorji
  • Operatorji primerjave
  • Operatorji dodelitve
  • Logični operatorji
  • Bitni operaterji
  • Operaterji članstva
  • Operaterji identitete
  • Aritmetični operatorji

Aritmetični operatorji

Aritmetični operatorji, uporabljeni med dvema operandoma za določeno operacijo. Obstaja veliko aritmetičnih operatorjev. Vključuje operator eksponenta (**) ter operatorje + (seštevanje), - (odštevanje), * (množenje), / (deljenje), % (opomnik) in // (drobno deljenje).

Za podrobno razlago aritmetičnih operatorjev si oglejte naslednjo tabelo.

Operater Opis
+ (dodatek) Uporablja se za seštevanje dveh operandov. Na primer, če je a = 10, b = 10 => a+b = 20
- (odštevanje) Uporablja se za odštevanje drugega operanda od prvega operanda. Če je prvi operand manjši od drugega operanda, je vrednost negativna. Na primer, če je a = 20, b = 5 => a - b = 15
/ (deli) Vrne količnik po deljenju prvega operanda z drugim operandom. Na primer, če je a = 20, b = 10 => a/b = 2,0
* (Množenje) Uporablja se za množenje enega operanda z drugim. Na primer, če je a = 20, b = 4 => a * b = 80
% (opomnik) Po deljenju prvega operanda z drugim operandom vrne opomnik. Na primer, če je a = 20, b = 10 => a%b = 0
** (eksponent) Ker izračuna potenco prvega operanda na drugega operanda, je eksponentni operator.
// (razdelitev tal) Zagotavlja najnižjo vrednost količnika, ki jo dobimo z deljenjem obeh operandov.

Programska koda:

Zdaj podajamo primere kode aritmetičnih operatorjev v Pythonu. Koda je podana spodaj -

 a = 32 # Initialize the value of a b = 6 # Initialize the value of b print('Addition of two numbers:',a+b) print('Subtraction of two numbers:',a-b) print('Multiplication of two numbers:',a*b) print('Division of two numbers:',a/b) print('Reminder of two numbers:',a%b) print('Exponent of two numbers:',a**b) print('Floor division of two numbers:',a//b) 

Izhod:

Zdaj zgornjo kodo prevedemo v Python in jo po uspešni prevedbi zaženemo. Nato je rezultat podan spodaj -

kako pretvoriti niz v char
 Addition of two numbers: 38 Subtraction of two numbers: 26 Multiplication of two numbers: 192 Division of two numbers: 5.333333333333333 Reminder of two numbers: 2 Exponent of two numbers: 1073741824 Floor division of two numbers: 5 

Operator primerjave

Operatorji primerjave uporabljajo predvsem za namene primerjave. Operatorji primerjave primerjajo vrednosti obeh operandov in v skladu s tem vrnejo resnično ali napačno logično vrednost. Primeri primerjalnih operatorjev so ==, !=, =, >,<. in the below table, we explain works of operators.< p>

Operater Opis
== Če je vrednost dveh operandov enaka, postane pogoj resničen.
!= Če vrednost dveh operandov ni enaka, postane pogoj resničen.
<=< td> Pogoj je izpolnjen, če je prvi operand manjši ali enak drugemu operandu.
>= Pogoj je izpolnjen, če je prvi operand večji ali enak drugemu operandu.
> Če je prvi operand večji od drugega operanda, postane pogoj resničen.
< Če je prvi operand manjši od drugega operanda, postane pogoj resničen.

Programska koda:

Zdaj podajamo primere kode primerjalnih operatorjev v Pythonu. Koda je podana spodaj -

 a = 32 # Initialize the value of a b = 6 # Initialize the value of b print(&apos;Two numbers are equal or not:&apos;,a==b) print(&apos;Two numbers are not equal or not:&apos;,a!=b) print(&apos;a is less than or equal to b:&apos;,a=b) print(&apos;a is greater b:&apos;,a&gt;b) print(&apos;a is less than b:&apos;,a <b) < pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Two numbers are equal or not: False Two numbers are not equal or not: True a is less than or equal to b: False a is greater than or equal to b: True a is greater b: True a is less than b: False </pre> <h2>Assignment Operators</h2> <p>Using the assignment operators, the right expression&apos;s value is assigned to the left operand. There are some examples of assignment operators like =, +=, -=, *=, %=, **=, //=. In the below table, we explain the works of the operators.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>=</td> <td>It assigns the value of the right expression to the left operand.</td> </tr> <tr> <td>+= </td> <td>By multiplying the value of the right operand by the value of the left operand, the left operand receives a changed value. For example, if a = 10, b = 20 =&gt; a+ = b will be equal to a = a+ b and therefore, a = 30.</td> </tr> <tr> <td>-=</td> <td>It decreases the value of the left operand by the value of the right operand and assigns the modified value back to left operand. For example, if a = 20, b = 10 =&gt; a- = b will be equal to a = a- b and therefore, a = 10.</td> </tr> <tr> <td>*=</td> <td>It multiplies the value of the left operand by the value of the right operand and assigns the modified value back to then the left operand. For example, if a = 10, b = 20 =&gt; a* = b will be equal to a = a* b and therefore, a = 200.</td> </tr> <tr> <td>%=</td> <td>It divides the value of the left operand by the value of the right operand and assigns the reminder back to the left operand. For example, if a = 20, b = 10 =&gt; a % = b will be equal to a = a % b and therefore, a = 0.</td> </tr> <tr> <td>**=</td> <td>a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.</td> </tr> <tr> <td>//=</td> <td>A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of Assignment operators in Python. The code is given below -</p> <pre> a = 32 # Initialize the value of a b = 6 # Initialize the value of b print(&apos;a=b:&apos;, a==b) print(&apos;a+=b:&apos;, a+b) print(&apos;a-=b:&apos;, a-b) print(&apos;a*=b:&apos;, a*b) print(&apos;a%=b:&apos;, a%b) print(&apos;a**=b:&apos;, a**b) print(&apos;a//=b:&apos;, a//b) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> a=b: False a+=b: 38 a-=b: 26 a*=b: 192 a%=b: 2 a**=b: 1073741824 a//=b: 5 </pre> <h2>Bitwise Operators</h2> <p>The two operands&apos; values are processed bit by bit by the bitwise operators. The examples of Bitwise operators are bitwise OR (|), bitwise AND (&amp;), bitwise XOR (^), negation (~), Left shift (&lt;&gt;). Consider the case below.</p> <p> <strong>For example,</strong> </p> <pre> if a = 7 b = 6 then, binary (a) = 0111 binary (b) = 0110 hence, a &amp; b = 0011 a | b = 0111 a ^ b = 0100 ~ a = 1000 Let, Binary of x = 0101 Binary of y = 1000 Bitwise OR = 1101 8 4 2 1 1 1 0 1 = 8 + 4 + 1 = 13 Bitwise AND = 0000 0000 = 0 Bitwise XOR = 1101 8 4 2 1 1 1 0 1 = 8 + 4 + 1 = 13 Negation of x = ~x = (-x) - 1 = (-5) - 1 = -6 ~x = -6 </pre> <p>In the below table, we are explaining the works of the bitwise operators.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>&amp; (binary and)</td> <td>A 1 is copied to the result if both bits in two operands at the same location are 1. If not, 0 is copied.</td> </tr> <tr> <td>| (binary or)</td> <td>The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1.</td> </tr> <tr> <td>^ (binary xor)</td> <td>If the two bits are different, the outcome bit will be 1, else it will be 0.</td> </tr> <tr> <td>~ (negation) </td> <td>The operand&apos;s bits are calculated as their negations, so if one bit is 0, the next bit will be 1, and vice versa.</td> </tr> <tr> <td>&lt;&lt; (left shift)</td> <td>The number of bits in the right operand is multiplied by the leftward shift of the value of the left operand.</td> </tr> <tr> <td>&gt;&gt; (right shift)</td> <td>The left operand is moved right by the number of bits present in the right operand.</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of Bitwise operators in Python. The code is given below -</p> <pre> a = 5 # initialize the value of a b = 6 # initialize the value of b print(&apos;a&amp;b:&apos;, a&amp;b) print(&apos;a|b:&apos;, a|b) print(&apos;a^b:&apos;, a^b) print(&apos;~a:&apos;, ~a) print(&apos;a&lt; <b:', a<>b:&apos;, a&gt;&gt;b) </b:',></pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> a&amp;b: 4 a|b: 7 a^b: 3 ~a: -6 a&lt; <b: 320 a>&gt;b: 0 </b:></pre> <h2>Logical Operators</h2> <p>The assessment of expressions to make decisions typically uses logical operators. The examples of logical operators are and, or, and not. In the case of logical AND, if the first one is 0, it does not depend upon the second one. In the case of logical OR, if the first one is 1, it does not depend on the second one. Python supports the following logical operators. In the below table, we explain the works of the logical operators.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>and</td> <td>The condition will also be true if the expression is true. If the two expressions a and b are the same, then a and b must both be true.</td> </tr> <tr> <td>or</td> <td>The condition will be true if one of the phrases is true. If a and b are the two expressions, then an or b must be true if and is true and b is false.</td> </tr> <tr> <td>not</td> <td>If an expression <strong>a</strong> is true, then not (a) will be false and vice versa.</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of arithmetic operators in Python. The code is given below -</p> <pre> a = 5 # initialize the value of a print(Is this statement true?:&apos;,a &gt; 3 and a 3 or a 3 and a <5))) < pre> <p> <strong>Output:</strong> </p> <p>Now we give code examples of Bitwise operators in Python. The code is given below -</p> <pre> Is this statement true?: False Any one statement is true?: True Each statement is true then return False and vice-versa: True </pre> <h2>Membership Operators</h2> <p>The membership of a value inside a Python data structure can be verified using Python membership operators. The result is true if the value is in the data structure; otherwise, it returns false.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>in</td> <td>If the first operand cannot be found in the second operand, it is evaluated to be true (list, tuple, or dictionary).</td> </tr> <tr> <td>not in</td> <td>If the first operand is not present in the second operand, the evaluation is true (list, tuple, or dictionary).</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of Membership operators in Python. The code is given below -</p> <pre> x = [&apos;Rose&apos;, &apos;Lotus&apos;] print(&apos; Is value Present?&apos;, &apos;Rose&apos; in x) print(&apos; Is value not Present?&apos;, &apos;Riya&apos; not in x) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Is value Present? True Is value not Present? True </pre> <h2>Identity Operators</h2> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>is</td> <td>If the references on both sides point to the same object, it is determined to be true.</td> </tr> <tr> <td>is not</td> <td>If the references on both sides do not point at the same object, it is determined to be true.</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of Identity operators in Python. The code is given below -</p> <pre> a = [&apos;Rose&apos;, &apos;Lotus&apos;] b = [&apos;Rose&apos;, &apos;Lotus&apos;] c = a print(a is c) print(a is not c) print(a is b) print(a is not b) print(a == b) print(a != b) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in python, and after successful compilation, we run it. Then the output is given below -</p> <pre> True False False True True False </pre> <h2>Operator Precedence</h2> <p>The order in which the operators are examined is crucial to understand since it tells us which operator needs to be considered first. Below is a list of the Python operators&apos; precedence tables.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>**</td> <td>Overall other operators employed in the expression, the exponent operator is given precedence.</td> </tr> <tr> <td>~ + -</td> <td>the minus, unary plus, and negation. </td> </tr> <tr> <td>* / % //</td> <td>the division of the floor, the modules, the division, and the multiplication.</td> </tr> <tr> <td>+ -</td> <td>Binary plus, and minus</td> </tr> <tr> <td>&gt;&gt; &lt;&lt;</td> <td>Left shift. and right shift</td> </tr> <tr> <td>&amp;</td> <td>Binary and.</td> </tr> <tr> <td>^ |</td> <td>Binary xor, and or</td> </tr> <tr> <td><=>=</=></td> <td>Comparison operators (less than, less than equal to, greater than, greater then equal to).</td> </tr> <tr> <td> == !=</td> <td>Equality operators.</td> </tr> <tr> <td>= %= /= //= -= += <br> *= **=</td> <td>Assignment operators</td> </tr> <tr> <td>is is not</td> <td>Identity operators</td> </tr> <tr> <td>in not in</td> <td>Membership operators</td> </tr> <tr> <td>not or and</td> <td>Logical operators</td> </tr> </table> <h2>Conclusion:</h2> <p>So, in this article, we are discussing all the Python Operators. We briefly discuss how they work and share the program code using each operator in Python.</p> <hr></5)))></pre></b)>

Operatorji dodelitve

Z uporabo operatorjev dodelitve se vrednost desnega izraza dodeli levemu operandu. Obstaja nekaj primerov operatorjev dodeljevanja, kot so =, +=, -=, *=, %=, **=, //=. V spodnji tabeli razlagamo dela operaterjev.

Operater Opis
= Vrednost desnega izraza dodeli levemu operandu.
+= Z množenjem vrednosti desnega operanda z vrednostjo levega operanda levi operand prejme spremenjeno vrednost. Na primer, če je a = 10, bo b = 20 => a+ = b enako a = a+ b in torej a = 30.
-= Zmanjša vrednost levega operanda za vrednost desnega operanda in dodeli spremenjeno vrednost nazaj levemu operandu. Na primer, če je a = 20, bo b = 10 => a- = b enako a = a- b in zato a = 10.
*= Pomnoži vrednost levega operanda z vrednostjo desnega operanda in dodeli spremenjeno vrednost nazaj levemu operandu. Na primer, če je a = 10, bo b = 20 => a* = b enako a = a* b in zato a = 200.
%= Vrednost levega operanda deli z vrednostjo desnega operanda in opomnik dodeli nazaj levemu operandu. Na primer, če je a = 20, bo b = 10 => a % = b enako a = a % b in zato je a = 0.
**= a**=b bo enako a=a**b, na primer, če je a = 4, b =2, bo a**=b a dodelil 4**2 = 16.
//= A//=b bo enako a = a// b, če je na primer a = 4, b = 3, bo a//=b a dodelil 4//3 = 1.

Programska koda:

Zdaj podajamo primere kode operatorjev dodelitve v Pythonu. Koda je podana spodaj -

primitivni tipi podatkov v Javi
 a = 32 # Initialize the value of a b = 6 # Initialize the value of b print(&apos;a=b:&apos;, a==b) print(&apos;a+=b:&apos;, a+b) print(&apos;a-=b:&apos;, a-b) print(&apos;a*=b:&apos;, a*b) print(&apos;a%=b:&apos;, a%b) print(&apos;a**=b:&apos;, a**b) print(&apos;a//=b:&apos;, a//b) 

Izhod:

Zdaj zgornjo kodo prevedemo v Python in jo po uspešni prevedbi zaženemo. Nato je rezultat podan spodaj -

 a=b: False a+=b: 38 a-=b: 26 a*=b: 192 a%=b: 2 a**=b: 1073741824 a//=b: 5 

Bitni operaterji

Vrednosti obeh operandov bit za bitom obdelajo bitni operaterji. Primeri bitnih operatorjev so bitni ALI (|), bitni IN (&), bitni XOR (^), negacija (~), premik v levo (<>). Razmislite o spodnjem primeru.

na primer

 if a = 7 b = 6 then, binary (a) = 0111 binary (b) = 0110 hence, a &amp; b = 0011 a | b = 0111 a ^ b = 0100 ~ a = 1000 Let, Binary of x = 0101 Binary of y = 1000 Bitwise OR = 1101 8 4 2 1 1 1 0 1 = 8 + 4 + 1 = 13 Bitwise AND = 0000 0000 = 0 Bitwise XOR = 1101 8 4 2 1 1 1 0 1 = 8 + 4 + 1 = 13 Negation of x = ~x = (-x) - 1 = (-5) - 1 = -6 ~x = -6 

V spodnji tabeli razlagamo delovanje bitnih operaterjev.

Operater Opis
& (binarni in) 1 se kopira v rezultat, če sta oba bita v dveh operandih na isti lokaciji 1. Če ni, se kopira 0.
| (binarni ali) Dobljeni bit bo 0, če sta oba bita nič; sicer bo dobljeni bit 1.
^ (binarni xor) Če sta bita različna, bo izidni bit 1, drugače pa 0.
~ (negacija) Biti operanda se izračunajo kot njihove negacije, tako da če je en bit 0, bo naslednji bit 1 in obratno.
<< (levi premik) Število bitov v desnem operandu se pomnoži s premikom vrednosti levega operanda v levo.
>> (desni premik) Levi operand se premakne desno za število bitov v desnem operandu.

Programska koda:

java typeof spremenljivka

Zdaj podajamo primere kode bitnih operatorjev v Pythonu. Koda je podana spodaj -

 a = 5 # initialize the value of a b = 6 # initialize the value of b print(&apos;a&amp;b:&apos;, a&amp;b) print(&apos;a|b:&apos;, a|b) print(&apos;a^b:&apos;, a^b) print(&apos;~a:&apos;, ~a) print(&apos;a&lt; <b:\', a<>b:&apos;, a&gt;&gt;b) </b:\',>

Izhod:

Zdaj zgornjo kodo prevedemo v Python in jo po uspešni prevedbi zaženemo. Nato je rezultat podan spodaj -

 a&amp;b: 4 a|b: 7 a^b: 3 ~a: -6 a&lt; <b: 320 a>&gt;b: 0 </b:>

Logični operatorji

Ocenjevanje izrazov za sprejemanje odločitev običajno uporablja logične operatorje. Primeri logičnih operatorjev so in, ali in ne. V primeru logičnega IN, če je prvi 0, ni odvisen od drugega. V primeru logičnega ALI, če je prvi 1, ni odvisen od drugega. Python podpira naslednje logične operaterje. V spodnji tabeli pojasnjujemo delovanje logičnih operatorjev.

Operater Opis
in Pogoj bo tudi resničen, če je izraz resničen. Če sta izraza a in b enaka, morata biti a in b oba resnična.
oz Pogoj bo resničen, če je ena od fraz resnična. Če sta a in b izraza, potem mora biti an ali b resničen, če je in resničen in je b napačen.
ne Če izraz a je res, potem bo ne (a) napačno in obratno.

Programska koda:

Zdaj podajamo primere kode aritmetičnih operatorjev v Pythonu. Koda je podana spodaj -

 a = 5 # initialize the value of a print(Is this statement true?:&apos;,a &gt; 3 and a 3 or a 3 and a <5))) < pre> <p> <strong>Output:</strong> </p> <p>Now we give code examples of Bitwise operators in Python. The code is given below -</p> <pre> Is this statement true?: False Any one statement is true?: True Each statement is true then return False and vice-versa: True </pre> <h2>Membership Operators</h2> <p>The membership of a value inside a Python data structure can be verified using Python membership operators. The result is true if the value is in the data structure; otherwise, it returns false.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>in</td> <td>If the first operand cannot be found in the second operand, it is evaluated to be true (list, tuple, or dictionary).</td> </tr> <tr> <td>not in</td> <td>If the first operand is not present in the second operand, the evaluation is true (list, tuple, or dictionary).</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of Membership operators in Python. The code is given below -</p> <pre> x = [&apos;Rose&apos;, &apos;Lotus&apos;] print(&apos; Is value Present?&apos;, &apos;Rose&apos; in x) print(&apos; Is value not Present?&apos;, &apos;Riya&apos; not in x) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Is value Present? True Is value not Present? True </pre> <h2>Identity Operators</h2> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>is</td> <td>If the references on both sides point to the same object, it is determined to be true.</td> </tr> <tr> <td>is not</td> <td>If the references on both sides do not point at the same object, it is determined to be true.</td> </tr> </table> <p> <strong>Program Code:</strong> </p> <p>Now we give code examples of Identity operators in Python. The code is given below -</p> <pre> a = [&apos;Rose&apos;, &apos;Lotus&apos;] b = [&apos;Rose&apos;, &apos;Lotus&apos;] c = a print(a is c) print(a is not c) print(a is b) print(a is not b) print(a == b) print(a != b) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in python, and after successful compilation, we run it. Then the output is given below -</p> <pre> True False False True True False </pre> <h2>Operator Precedence</h2> <p>The order in which the operators are examined is crucial to understand since it tells us which operator needs to be considered first. Below is a list of the Python operators&apos; precedence tables.</p> <table class="table"> <tr> <th>Operator</th> <th>Description</th> </tr> <tr> <td>**</td> <td>Overall other operators employed in the expression, the exponent operator is given precedence.</td> </tr> <tr> <td>~ + -</td> <td>the minus, unary plus, and negation. </td> </tr> <tr> <td>* / % //</td> <td>the division of the floor, the modules, the division, and the multiplication.</td> </tr> <tr> <td>+ -</td> <td>Binary plus, and minus</td> </tr> <tr> <td>&gt;&gt; &lt;&lt;</td> <td>Left shift. and right shift</td> </tr> <tr> <td>&amp;</td> <td>Binary and.</td> </tr> <tr> <td>^ |</td> <td>Binary xor, and or</td> </tr> <tr> <td><=>=</=></td> <td>Comparison operators (less than, less than equal to, greater than, greater then equal to).</td> </tr> <tr> <td> == !=</td> <td>Equality operators.</td> </tr> <tr> <td>= %= /= //= -= += <br> *= **=</td> <td>Assignment operators</td> </tr> <tr> <td>is is not</td> <td>Identity operators</td> </tr> <tr> <td>in not in</td> <td>Membership operators</td> </tr> <tr> <td>not or and</td> <td>Logical operators</td> </tr> </table> <h2>Conclusion:</h2> <p>So, in this article, we are discussing all the Python Operators. We briefly discuss how they work and share the program code using each operator in Python.</p> <hr></5)))>

Operaterji članstva

Pripadnost vrednosti znotraj podatkovne strukture Python je mogoče preveriti z uporabo operatorjev članstva Python. Rezultat je resničen, če je vrednost v podatkovni strukturi; sicer vrne false.

Operater Opis
v Če prvega operanda ni mogoče najti v drugem operandu, se oceni kot resničen (seznam, tuple ali slovar).
ni notri Če prvi operand ni prisoten v drugem operandu, je ocena resnična (seznam, tuple ali slovar).

Programska koda:

prečrtana markdown

Zdaj podajamo primere kode operatorjev članstva v Pythonu. Koda je podana spodaj -

 x = [&apos;Rose&apos;, &apos;Lotus&apos;] print(&apos; Is value Present?&apos;, &apos;Rose&apos; in x) print(&apos; Is value not Present?&apos;, &apos;Riya&apos; not in x) 

Izhod:

Zdaj zgornjo kodo prevedemo v Python in jo po uspešni prevedbi zaženemo. Nato je rezultat podan spodaj -

 Is value Present? True Is value not Present? True 

Operaterji identitete

Operater Opis
je Če reference na obeh straneh kažejo na isti predmet, se ugotovi, da je resničen.
ni Če sklicevanja na obeh straneh ne kažejo na isti predmet, se ugotovi, da je resnično.

Programska koda:

globalne spremenljivke js

Zdaj podajamo primere kode operaterjev identitete v Pythonu. Koda je podana spodaj -

 a = [&apos;Rose&apos;, &apos;Lotus&apos;] b = [&apos;Rose&apos;, &apos;Lotus&apos;] c = a print(a is c) print(a is not c) print(a is b) print(a is not b) print(a == b) print(a != b) 

Izhod:

Zdaj zgornjo kodo prevedemo v python in jo po uspešni prevedbi zaženemo. Nato je rezultat podan spodaj -

 True False False True True False 

Prednost operaterja

Vrstni red, v katerem se pregledujejo operaterji, je ključnega pomena za razumevanje, saj nam pove, kateri operater je treba najprej upoštevati. Spodaj je seznam tabel prednosti operaterjev Python.

Operater Opis
** Pri vseh drugih operatorjih, uporabljenih v izrazu, ima operator eksponenta prednost.
~ + - minus, unarni plus in negacija.
*/% // delitev nadstropja, modulov, delitev in množenje.
+ - Binarni plus in minus
>> << Levi shift. in desni premik
& Binarno in.
^ | Binarni xor in ali
<=>= Primerjalni operatorji (manj kot, manj kot enako, večje od, večje kot enako).
== != Operatorji enakosti.
= %= /= //= -= +=
*= **=
Operatorji dodelitve
je ni Identitetni operaterji
v ne v Operaterji članstva
ne ali in Logični operatorji

Zaključek:

Torej, v tem članku razpravljamo o vseh operaterjih Python. Na kratko razpravljamo o njihovem delovanju in delimo programsko kodo z uporabo vsakega operatorja v Pythonu.