logo

Python-operatörer

Introduktion:

I den här artikeln diskuterar vi Python-operatörer. Operatören är en symbol som utför en specifik operation mellan två operander, enligt en definition. Operatörer fungerar som grunden på vilken logik är konstruerad i ett program i ett visst programmeringsspråk. I varje programmeringsspråk utför vissa operatörer flera uppgifter. Liksom andra språk har Python också några operatorer, och dessa ges nedan -

  • Aritmetiska operatorer
  • Jämförelseoperatörer
  • Uppdragsoperatörer
  • Logiska operatörer
  • Bitwise operatörer
  • Medlemskapsoperatörer
  • Identitetsoperatörer
  • Aritmetiska operatorer

Aritmetiska operatorer

Aritmetiska operatorer som används mellan två operander för en viss operation. Det finns många aritmetiska operatorer. Den inkluderar operatorn exponent (**) samt operatorerna + (addition), - (subtraktion), * (multiplikation), / (dividera), % (påminnelse) och // (våningsdelning).

Betrakta följande tabell för en detaljerad förklaring av aritmetiska operatorer.

Operatör Beskrivning
+ (tillägg) Den används för att lägga till två operander. Till exempel, om a = 10, b = 10 => a+b = 20
- (subtraktion) Den används för att subtrahera den andra operanden från den första operanden. Om den första operanden är mindre än den andra operanden blir värdet negativt. Till exempel, om a = 20, b = 5 => a - b = 15
/ (dela upp) Den returnerar kvoten efter att ha dividerat den första operanden med den andra operanden. Till exempel, om a = 20, b = 10 => a/b = 2,0
* (Multiplikation) Den används för att multiplicera en operand med den andra. Till exempel, om a = 20, b = 4 => a * b = 80
% (påminnelse) Den returnerar påminnelsen efter att ha dividerat den första operanden med den andra operanden. Till exempel, om a = 20, b = 10 => a%b = 0
** (Exponent) Eftersom den beräknar den första operandens kraft till den andra operanden, är den en exponentoperator.
// (våningsindelning) Den ger kvotens golvvärde, som erhålls genom att dividera de två operanderna.

Programkod:

Nu ger vi kodexempel på aritmetiska operatorer i Python. Koden ges nedan -

 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) 

Produktion:

Nu kompilerar vi ovanstående kod i Python, och efter framgångsrik kompilering kör vi den. Då ges utgången nedan -

vad är storleken på min skärm
 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 

Jämförelseoperatör

Jämförelseoperatörer använder huvudsakligen för jämförelsesyften. Jämförelseoperatorer jämför värdena för de två operanderna och returnerar ett sant eller falskt booleskt värde i enlighet med detta. Exemplet på jämförelseoperatorer är ==, !=, =, >,<. in the below table, we explain works of operators.< p>

Operatör Beskrivning
== Om värdet av två operander är lika, blir villkoret sant.
!= Om värdet på två operander inte är lika, blir villkoret sant.
<=< td> Villkoret är uppfyllt om den första operanden är mindre än eller lika med den andra operanden.
>= Villkoret är uppfyllt om den första operanden är större än eller lika med den andra operanden.
> Om den första operanden är större än den andra operanden blir villkoret sant.
< Om den första operanden är mindre än den andra operanden blir villkoret sant.

Programkod:

Nu ger vi kodexempel på jämförelseoperatorer i Python. Koden ges nedan -

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

Uppdragsoperatörer

Med hjälp av tilldelningsoperatorerna tilldelas det högra uttryckets värde till den vänstra operanden. Det finns några exempel på tilldelningsoperatorer som =, +=, -=, *=, %=, **=, //=. I tabellen nedan förklarar vi operatörernas arbete.

Operatör Beskrivning
= Den tilldelar värdet av det högra uttrycket till den vänstra operanden.
+= Genom att multiplicera värdet på den högra operanden med värdet på den vänstra operanden får den vänstra operanden ett ändrat värde. Till exempel, om a = 10, blir b = 20 => a+ = b lika med a = a+ b och därför är a = 30.
-= Den minskar värdet på den vänstra operanden med värdet på den högra operanden och tilldelar det ändrade värdet tillbaka till den vänstra operanden. Till exempel, om a = 20, blir b = 10 => a- = b lika med a = a- b och därför är a = 10.
*= Den multiplicerar värdet på den vänstra operanden med värdet på den högra operanden och tilldelar det modifierade värdet tillbaka till den vänstra operanden. Till exempel, om a = 10, blir b = 20 => a* = b lika med a = a* b och därför är a = 200.
%= Den delar värdet på den vänstra operanden med värdet på den högra operanden och tilldelar påminnelsen tillbaka till den vänstra operanden. Till exempel, om a = 20, b = 10 => a % = b kommer att vara lika med a = a % b och därför är a = 0.
**= a**=b kommer att vara lika med a=a**b, till exempel, om a = 4, b =2, tilldelar a**=b 4**2 = 16 till a.
//= A//=b kommer att vara lika med a = a// b, till exempel, om a = 4, b = 3, tilldelar a//=b 4//3 = 1 till a.

Programkod:

Nu ger vi kodexempel på tilldelningsoperatorer i Python. Koden ges nedan -

webbplats som coomeet
 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) 

Produktion:

Nu kompilerar vi ovanstående kod i Python, och efter framgångsrik kompilering kör vi den. Då ges utgången nedan -

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

Bitwise operatörer

De två operandernas värden bearbetas bit för bit av de bitvisa operatorerna. Exemplen på bitvisa operatorer är bitvis OR (|), bitvis AND (&), bitvis XOR (^), negation (~), Vänsterskift (<>). Tänk på fallet nedan.

Till exempel,

 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 

I tabellen nedan förklarar vi de bitvisa operatorernas arbete.

Operatör Beskrivning
& (binär och) En 1 kopieras till resultatet om båda bitarna i två operander på samma plats är 1. Om inte kopieras 0.
| (binär eller) Den resulterande biten blir 0 om båda bitarna är noll; annars blir den resulterande biten 1.
^ (binär xor) Om de två bitarna är olika blir utfallsbiten 1, annars blir den 0.
~ (negation) Operandens bitar beräknas som deras negationer, så om en bit är 0 blir nästa bit 1 och vice versa.
<< (vänster skift) Antalet bitar i den högra operanden multipliceras med förskjutningen åt vänster av värdet på den vänstra operanden.
>> (högerväxling) Den vänstra operanden flyttas åt höger med antalet bitar som finns i den högra operanden.

Programkod:

spara youtube video vlc

Nu ger vi kodexempel på Bitwise-operatorer i Python. Koden ges nedan -

 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:\',>

Produktion:

Nu kompilerar vi ovanstående kod i Python, och efter framgångsrik kompilering kör vi den. Då ges utgången nedan -

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

Logiska operatörer

Bedömningen av uttryck för att fatta beslut använder vanligtvis logiska operatorer. Exemplen på logiska operatorer är och, eller, och inte. I fallet med logisk AND, om den första är 0, beror den inte på den andra. I fallet med logiskt ELLER, om det första är 1, beror det inte på det andra. Python stöder följande logiska operatorer. I tabellen nedan förklarar vi de logiska operatorernas arbeten.

Operatör Beskrivning
och Villkoret kommer också att vara sant om uttrycket är sant. Om de två uttrycken a och b är lika, måste a och b båda vara sanna.
eller Villkoret kommer att vara sant om en av fraserna är sann. Om a och b är de två uttrycken måste an eller b vara sant om och är sant och b är falskt.
inte Om ett uttryck a är sant, kommer inte (a) att vara falskt och vice versa.

Programkod:

Nu ger vi kodexempel på aritmetiska operatorer i Python. Koden ges nedan -

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

Medlemskapsoperatörer

Medlemskapet för ett värde i en Python-datastruktur kan verifieras med Python-medlemskapsoperatorer. Resultatet är sant om värdet finns i datastrukturen; annars returnerar det falskt.

Operatör Beskrivning
i Om den första operanden inte kan hittas i den andra operanden, bedöms den vara sann (lista, tuppel eller ordbok).
inte i Om den första operanden inte finns i den andra operanden är utvärderingen sann (lista, tuppel eller ordbok).

Programkod:

sträng java innehåller

Nu ger vi kodexempel på medlemskapsoperatörer i Python. Koden ges nedan -

 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) 

Produktion:

Nu kompilerar vi ovanstående kod i Python, och efter framgångsrik kompilering kör vi den. Då ges utgången nedan -

 Is value Present? True Is value not Present? True 

Identitetsoperatörer

Operatör Beskrivning
är Om referenserna på båda sidor pekar på samma objekt, är det fastställt att det är sant.
är inte Om referenserna på båda sidor inte pekar på samma objekt, är det fastställt att det är sant.

Programkod:

numpy linspace

Nu ger vi kodexempel på identitetsoperatorer i Python. Koden ges nedan -

 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) 

Produktion:

Nu kompilerar vi ovanstående kod i python, och efter framgångsrik kompilering kör vi den. Då ges utgången nedan -

 True False False True True False 

Operatörsföreträde

Ordningen i vilken operatörerna undersöks är avgörande att förstå eftersom det talar om för oss vilken operatör som måste beaktas först. Nedan finns en lista över Python-operatorernas prioritetstabeller.

Operatör Beskrivning
** Sammantaget andra operatorer som används i uttrycket ges exponentoperatorn företräde.
~ + - minus, unärt plus och negation.
*/% // divisionen av våningen, modulerna, divisionen och multiplikationen.
+ - Binära plus och minus
>> << Vänster Shift. och högerväxling
& Binära och.
^ | Binär xor, och eller
<=>= Jämförelseoperatorer (mindre än, mindre än lika med, större än, större än lika med).
== != Jämställdhetsaktörer.
= %= /= //= -= +=
*= **=
Uppdragsoperatörer
är är inte Identitetsoperatörer
i inte i Medlemskapsoperatörer
inte eller och Logiska operatorer

Slutsats:

Så i den här artikeln diskuterar vi alla Python-operatörer. Vi diskuterar kort hur de fungerar och delar programkoden med varje operator i Python.