logo

Python-operatører

Introduktion:

I denne artikel diskuterer vi Python-operatører. Operatoren er et symbol, der udfører en specifik operation mellem to operander, ifølge én definition. Operatører tjener som grundlaget, hvorpå logikken er konstrueret i et program i et bestemt programmeringssprog. I hvert programmeringssprog udfører nogle operatører flere opgaver. På samme måde som andre sprog har Python også nogle operatorer, og disse er angivet nedenfor -

  • Aritmetiske operatorer
  • Sammenligningsoperatører
  • Opdragsoperatører
  • Logiske operatører
  • Bitwise operatører
  • Medlemskabsoperatører
  • Identitetsoperatører
  • Aritmetiske operatorer

Aritmetiske operatorer

Aritmetiske operatorer brugt mellem to operander for en bestemt operation. Der er mange aritmetiske operatorer. Det inkluderer eksponent (**) operatoren samt + (addition), - (subtraktion), * (multiplikation), / (divide), % (påmindelse) og // (gulvdeling) operatorerne.

Overvej følgende tabel for en detaljeret forklaring af aritmetiske operatorer.

Operatør Beskrivelse
+ (Tilføjelse) Det bruges til at tilføje to operander. For eksempel, hvis a = 10, b = 10 => a+b = 20
- (subtraktion) Det bruges til at trække den anden operand fra den første operand. Hvis den første operand er mindre end den anden operand, er værdien negativ. For eksempel, hvis a = 20, b = 5 => a - b = 15
/ (dele) Den returnerer kvotienten efter at have divideret den første operand med den anden operand. For eksempel, hvis a = 20, b = 10 => a/b = 2,0
* (Multiplikation) Det bruges til at gange den ene operand med den anden. For eksempel, hvis a = 20, b = 4 => a * b = 80
% (påmindelse) Den returnerer påmindelsen efter at have divideret den første operand med den anden operand. For eksempel, hvis a = 20, b = 10 => a%b = 0
** (Eksponent) Da den beregner den første operands magt til den anden operand, er den en eksponentoperator.
// (Etageinddeling) Det giver kvotientens bundværdi, som fås ved at dividere de to operander.

Programkode:

Nu giver vi kodeeksempler på aritmetiske operatorer i Python. Koden er angivet nedenfor -

 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 kompilerer vi ovenstående kode i Python, og efter vellykket kompilering kører vi den. Så er output givet nedenfor -

starter med java
 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 

Sammenligningsoperatør

Sammenligningsoperatører bruger hovedsageligt til sammenligningsformål. Sammenligningsoperatorer sammenligner værdierne af de to operander og returnerer en sand eller falsk boolsk værdi i overensstemmelse med dem. Eksemplet på sammenligningsoperatorer er ==, !=, =, >,<. in the below table, we explain works of operators.< p>

Operatør Beskrivelse
== Hvis værdien af ​​to operander er lig, bliver betingelsen sand.
!= Hvis værdien af ​​to operander ikke er ens, bliver betingelsen sand.
<=< td> Betingelsen er opfyldt, hvis den første operand er mindre end eller lig med den anden operand.
>= Betingelsen er opfyldt, hvis den første operand er større end eller lig med den anden operand.
> Hvis den første operand er større end den anden operand, bliver betingelsen sand.
< Hvis den første operand er mindre end den anden operand, bliver betingelsen sand.

Programkode:

Nu giver vi kodeeksempler på sammenligningsoperatorer i Python. Koden er angivet nedenfor -

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

Opdragsoperatører

Ved at bruge tildelingsoperatorerne tildeles det højre udtryks værdi til den venstre operand. Der er nogle eksempler på tildelingsoperatorer som =, +=, -=, *=, %=, **=, //=. I nedenstående tabel forklarer vi operatørernes arbejde.

Operatør Beskrivelse
= Det tildeler værdien af ​​det højre udtryk til venstre operand.
+= Ved at gange værdien af ​​højre operand med værdien af ​​venstre operand, modtager venstre operand en ændret værdi. For eksempel, hvis a = 10, vil b = 20 => a+ = b være lig med a = a+ b og derfor er a = 30.
-= Det reducerer værdien af ​​venstre operand med værdien af ​​højre operand og tildeler den ændrede værdi tilbage til venstre operand. For eksempel, hvis a = 20, vil b = 10 => a- = b være lig med a = a- b og derfor er a = 10.
*= Den multiplicerer værdien af ​​den venstre operand med værdien af ​​den højre operand og tildeler den ændrede værdi tilbage til den venstre operand. For eksempel, hvis a = 10, vil b = 20 => a* = b være lig med a = a* b og derfor er a = 200.
%= Den deler værdien af ​​venstre operand med værdien af ​​højre operand og tildeler påmindelsen tilbage til venstre operand. For eksempel, hvis a = 20, vil b = 10 => a % = b være lig med a = a % b og derfor er a = 0.
**= a**=b vil være lig med a=a**b, for eksempel, hvis a = 4, b =2, vil a**=b tildele 4**2 = 16 til a.
//= A//=b vil være lig med a = a// b, for eksempel, hvis a = 4, b = 3, vil a//=b tildele 4//3 = 1 til a.

Programkode:

Nu giver vi kodeeksempler på tildelingsoperatorer i Python. Koden er angivet nedenfor -

 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 kompilerer vi ovenstående kode i Python, og efter vellykket kompilering kører vi den. Så er output givet nedenfor -

hvordan man læser fra en csv-fil i java
 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 to operanders værdier behandles bit for bit af de bitvise operatorer. Eksempler på bitvise operatorer er bitvise OR (|), bitvise AND (&), bitvise XOR (^), negation (~), venstreforskydning (<>). Overvej sagen nedenfor.

For eksempel,

 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 nedenstående tabel forklarer vi de bitvise operatørers arbejde.

Operatør Beskrivelse
& (binær og) Et 1 kopieres til resultatet, hvis begge bits i to operander på samme placering er 1. Hvis ikke, kopieres 0.
| (binær eller) Den resulterende bit vil være 0, hvis begge bits er nul; ellers vil den resulterende bit være 1.
^ (binær xor) Hvis de to bits er forskellige, vil udfaldsbitten være 1, ellers vil den være 0.
~ (nægtelse) Operaandens bits beregnes som deres negationer, så hvis en bit er 0, vil den næste bit være 1, og omvendt.
<< (venstre skift) Antallet af bits i højre operand multipliceres med venstre forskydning af værdien af ​​venstre operand.
>> (højre skift) Den venstre operand flyttes til højre med antallet af bits i den højre operand.

Programkode:

registrere hukommelse

Nu giver vi kodeeksempler på Bitwise-operatorer i Python. Koden er angivet nedenfor -

 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 kompilerer vi ovenstående kode i Python, og efter vellykket kompilering kører vi den. Så er output givet nedenfor -

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

Logiske operatører

Vurderingen af ​​udtryk til at træffe beslutninger bruger typisk logiske operatorer. Eksemplerne på logiske operatorer er og, eller, og ikke. I tilfælde af logisk OG, hvis den første er 0, afhænger den ikke af den anden. I tilfælde af logisk ELLER, hvis den første er 1, afhænger den ikke af den anden. Python understøtter følgende logiske operatorer. I nedenstående tabel forklarer vi de logiske operatørers værker.

Operatør Beskrivelse
og Betingelsen vil også være sand, hvis udtrykket er sandt. Hvis de to udtryk a og b er ens, skal a og b begge være sande.
eller Betingelsen vil være sand, hvis en af ​​sætningerne er sand. Hvis a og b er de to udtryk, skal an eller b være sand, hvis og er sand, og b er falsk.
ikke Hvis et udtryk -en er sandt, så vil (a) ikke være falsk og omvendt.

Programkode:

indeks over listen

Nu giver vi kodeeksempler på aritmetiske operatorer i Python. Koden er angivet nedenfor -

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

Medlemskabsoperatører

Medlemskabet af en værdi inde i en Python-datastruktur kan verificeres ved hjælp af Python-medlemskabsoperatorer. Resultatet er sandt, hvis værdien er i datastrukturen; ellers returnerer den falsk.

Operatør Beskrivelse
i Hvis den første operand ikke kan findes i den anden operand, vurderes den til at være sand (liste, tuple eller ordbog).
ikke i Hvis den første operand ikke er til stede i den anden operand, er evalueringen sand (liste, tuple eller ordbog).

Programkode:

Nu giver vi kodeeksempler på medlemskabsoperatører i Python. Koden er angivet nedenfor -

 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 kompilerer vi ovenstående kode i Python, og efter vellykket kompilering kører vi den. Så er output givet nedenfor -

 Is value Present? True Is value not Present? True 

Identitetsoperatører

Operatør Beskrivelse
er Hvis referencerne på begge sider peger på det samme objekt, er det bestemt, at det er sandt.
er ikke Hvis referencerne på begge sider ikke peger på det samme objekt, er det bestemt, at det er sandt.

Programkode:

Nu giver vi kodeeksempler på identitetsoperatorer i Python. Koden er angivet nedenfor -

 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 kompilerer vi ovenstående kode i python, og efter vellykket kompilering kører vi den. Så er output givet nedenfor -

 True False False True True False 

Operatør forrang

Den rækkefølge, som operatørerne undersøges i, er afgørende at forstå, da den fortæller os, hvilken operatør, der skal overvejes først. Nedenfor er en liste over Python-operatørernes præcedenstabeller.

t flip flop
Operatør Beskrivelse
** Samlet set andre operatorer, der anvendes i udtrykket, får eksponentoperatoren forrang.
~ + - minus, unær plus og negation.
*/% // divisionen af ​​etagen, modulerne, divisionen og multiplikationen.
+ - Binære plus og minus
>> << Venstre skift. og højreskift
& Binære og.
^ | Binær xor, og eller
<=>= Sammenligningsoperatorer (mindre end, mindre end lig med, større end, større end lig med).
== != Ligestillingsoperatører.
= %= /= //= -= +=
*= **=
Opgaveoperatører
er er ikke Identitetsoperatører
i ikke i Medlemskabsoperatører
ikke eller og Logiske operatører

Konklusion:

Så i denne artikel diskuterer vi alle Python-operatørerne. Vi diskuterer kort, hvordan de fungerer og deler programkoden ved hjælp af hver operator i Python.