logo

Python operatori

Uvod:

U ovom članku raspravljamo o Python operatorima. Operator je simbol koji izvodi određenu operaciju između dva operanda, prema jednoj definiciji. Operatori služe kao temelj na kojem se konstruira logika u programu u određenom programskom jeziku. U svakom programskom jeziku neki operatori obavljaju nekoliko zadataka. Kao i drugi jezici, Python također ima neke operatore, a oni su navedeni u nastavku -

  • Aritmetički operatori
  • Operatori usporedbe
  • Operatori dodjele
  • Logički operatori
  • Bitovi operatori
  • Operatori članstva
  • Operatori identiteta
  • Aritmetički operatori

Aritmetički operatori

Aritmetički operatori koji se koriste između dva operanda za određenu operaciju. Postoji mnogo aritmetičkih operatora. Uključuje operator eksponenta (**), kao i operatore + (zbrajanje), - (oduzimanje), * (množenje), / (dijeljenje), % (podsjetnik) i // (podjeljenje).

Razmotrite sljedeću tablicu za detaljno objašnjenje aritmetičkih operatora.

Operater Opis
+ (dodatak) Koristi se za zbrajanje dva operanda. Na primjer, ako je a = 10, b = 10 => a+b = 20
- (Oduzimanje) Koristi se za oduzimanje drugog operanda od prvog operanda. Ako je prvi operand manji od drugog operanda, vrijednost je negativna. Na primjer, ako je a = 20, b = 5 => a - b = 15
/ (podijeli) Vraća kvocijent nakon dijeljenja prvog operanda s drugim operandom. Na primjer, ako je a = 20, b = 10 => a/b = 2,0
* (Množenje) Koristi se za množenje jednog operanda s drugim. Na primjer, ako je a = 20, b = 4 => a * b = 80
% (podsjetnik) Vraća podsjetnik nakon dijeljenja prvog operanda s drugim operandom. Na primjer, ako je a = 20, b = 10 => a%b = 0
** (eksponent) Budući da izračunava stepen prvog operanda prema drugom operandu, on je eksponentni operator.
// (Podjela etaža) Daje donju vrijednost kvocijenta koja se dobiva dijeljenjem dvaju operanda.

Programski kod:

Sada dajemo primjere kodova aritmetičkih operatora u Pythonu. Kod je naveden u nastavku -

 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) 

Izlaz:

Sada kompajliramo gornji kod u Python, a nakon uspješne kompilacije, pokrećemo ga. Zatim je izlaz dan ispod -

.sljedeća 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 

Operator usporedbe

Operatori usporedbe uglavnom se koriste u svrhu usporedbe. Operatori usporedbe uspoređuju vrijednosti dvaju operanda i u skladu s tim vraćaju istinitu ili lažnu Booleovu vrijednost. Primjeri operatora usporedbe su ==, !=, =, >,<. in the below table, we explain works of operators.< p>

Operater Opis
== Ako je vrijednost dvaju operanda jednaka, tada uvjet postaje istinit.
!= Ako vrijednost dvaju operanda nije jednaka, tada uvjet postaje istinit.
<=< td> Uvjet je ispunjen ako je prvi operand manji ili jednak drugom operandu.
>= Uvjet je ispunjen ako je prvi operand veći ili jednak drugom operandu.
> Ako je prvi operand veći od drugog operanda, tada uvjet postaje istinit.
< Ako je prvi operand manji od drugog operanda, tada uvjet postaje istinit.

Programski kod:

razlika između dva niza python

Sada dajemo primjere koda operatora usporedbe u Pythonu. Kod je naveden u nastavku -

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

Operatori dodjele

Pomoću operatora dodjele, vrijednost desnog izraza se dodjeljuje lijevom operandu. Postoje neki primjeri operatora dodjele kao što su =, +=, -=, *=, %=, **=, //=. U donjoj tablici objašnjavamo rad operatera.

Operater Opis
= Dodjeljuje vrijednost desnog izraza lijevom operandu.
+= Množenjem vrijednosti desnog operanda s vrijednošću lijevog operanda, lijevi operand dobiva promijenjenu vrijednost. Na primjer, ako je a = 10, b = 20 => a+ = b bit će jednako a = a+ b i prema tome, a = 30.
-= Smanjuje vrijednost lijevog operanda za vrijednost desnog operanda i dodjeljuje izmijenjenu vrijednost natrag lijevom operandu. Na primjer, ako je a = 20, b = 10 => a- = b bit će jednako a = a- b i prema tome, a = 10.
*= Množi vrijednost lijevog operanda s vrijednošću desnog operanda i ponovno dodjeljuje izmijenjenu vrijednost lijevom operandu. Na primjer, ako je a = 10, b = 20 => a* = b bit će jednako a = a* b i prema tome, a = 200.
%= Dijeli vrijednost lijevog operanda s vrijednošću desnog operanda i ponovno dodjeljuje podsjetnik lijevom operandu. Na primjer, ako je a = 20, b = 10 => a % = b bit će jednako a = a % b i prema tome, a = 0.
**= a**=b bit će jednako a=a**b, na primjer, ako je a = 4, b =2, a**=b će dodijeliti 4**2 = 16 a.
//= A//=b bit će jednako a = a// b, na primjer, ako je a = 4, b = 3, a//=b će dodijeliti 4//3 = 1 a.

Programski kod:

Sada dajemo primjere koda operatora dodjele u Pythonu. Kod je naveden u nastavku -

 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) 

Izlaz:

Sada kompajliramo gornji kod u Python, a nakon uspješne kompilacije, pokrećemo ga. Zatim je izlaz dan ispod -

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

Bitovi operatori

Vrijednosti dvaju operanda obrađuju se bit po bit operatori. Primjeri bitnih operatora su bitni OR (|), bitni AND (&), bitni XOR (^), negacija (~), pomak ulijevo (<>). Razmotrite slučaj u nastavku.

Na primjer,

 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 

U donjoj tablici objašnjavamo rad bitovnih operatora.

Operater Opis
& (binarni i) 1 se kopira u rezultat ako su oba bita u dva operanda na istoj lokaciji 1. Ako nisu, 0 se kopira.
| (binarno ili) Rezultirajući bit će biti 0 ako su oba bita nula; u protivnom će rezultirajući bit biti 1.
^ (binarni xor) Ako su dva bita različita, bit ishoda bit će 1, inače će biti 0.
~ (negacija) Bitovi operanda izračunavaju se kao njihove negacije, pa ako je jedan bit 0, sljedeći bit će biti 1, i obrnuto.
<< (pomak ulijevo) Broj bitova u desnom operandu množi se s pomakom vrijednosti lijevog operanda ulijevo.
>> (desni pomak) Lijevi operand se pomiče udesno za broj bitova prisutnih u desnom operandu.

Programski kod:

sta znaci xd

Sada dajemo primjere koda bitwise operatora u Pythonu. Kod je naveden u nastavku -

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

Izlaz:

Sada kompajliramo gornji kod u Python, a nakon uspješne kompilacije, pokrećemo ga. Zatim je izlaz dan ispod -

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

Logički operatori

Procjena izraza za donošenje odluka obično koristi logičke operatore. Primjeri logičkih operatora su i, ili i ne. U slučaju logičkog I, ako je prvi 0, ne ovisi o drugom. U slučaju logičkog ILI, ako je prvi 1, ne ovisi o drugom. Python podržava sljedeće logičke operatore. U donjoj tablici objašnjavamo rad logičkih operatora.

Operater Opis
i Uvjet će također biti istinit ako je izraz istinit. Ako su dva izraza a i b ista, tada a i b moraju biti istiniti.
ili Uvjet će biti istinit ako je jedna od fraza istinita. Ako su a i b dva izraza, tada an ili b moraju biti istiniti ako je i istinito, a b netočno.
ne Ako izraz a je istina, tada će ne (a) biti lažno i obrnuto.

Programski kod:

Sada dajemo primjere koda aritmetičkih operatora u Pythonu. Kod je naveden u nastavku -

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

Operatori članstva

Pripadnost vrijednosti unutar Python strukture podataka može se provjeriti pomoću Python operatora članstva. Rezultat je istinit ako je vrijednost u strukturi podataka; u suprotnom, vraća false.

razlika između lava i tigra
Operater Opis
u Ako se prvi operand ne može pronaći u drugom operandu, procjenjuje se da je istinit (popis, tuple ili rječnik).
ne u Ako prvi operand nije prisutan u drugom operandu, procjena je istinita (popis, tuple ili rječnik).

Programski kod:

Sada dajemo primjere koda operatora članstva u Pythonu. Kod je naveden u nastavku -

 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) 

Izlaz:

Sada kompajliramo gornji kod u Python, a nakon uspješne kompilacije, pokrećemo ga. Zatim je izlaz dan ispod -

kako znati veličinu zaslona
 Is value Present? True Is value not Present? True 

Operatori identiteta

Operater Opis
je Ako reference s obje strane upućuju na isti objekt, utvrđuje se da je istinit.
nije Ako reference s obje strane ne upućuju na isti objekt, utvrđuje se da je točna.

Programski kod:

Sada dajemo primjere koda operatora identiteta u Pythonu. Kod je naveden u nastavku -

 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) 

Izlaz:

Sada kompajliramo gornji kod u python, a nakon uspješne kompilacije, pokrećemo ga. Zatim je izlaz dan ispod -

 True False False True True False 

Prioritet operatora

Redoslijed kojim se operatori ispituju ključan je za razumijevanje jer nam govori koji operator treba prvi razmotriti. Ispod je popis tablica prioriteta Python operatora.

Operater Opis
** Sve u svemu, operator eksponenta ima prednost u odnosu na ostale operatore koji se koriste u izrazu.
~ + - minus, unarni plus i negacija.
*/% // podjela kata, moduli, dijeljenje i množenje.
+ - Binarni plus i minus
>> << Lijevi pomak. i desni pomak
i Binarno i.
^ | Binarni xor i or
<=>= Operatori usporedbe (manje od, manje od jednako, veće od, veće od jednako).
== != Operatori jednakosti.
= %= /= //= -= +=
*= **=
Operatori dodjele
nije nije Operatori identiteta
u ne u Operatori članstva
ne ili i Logički operatori

Zaključak:

Dakle, u ovom članku raspravljamo o svim Python operatorima. Ukratko raspravljamo o tome kako rade i dijelimo programski kod koristeći svaki operator u Pythonu.