logo

Opérateurs Python

Introduction:

Dans cet article, nous discutons des opérateurs Python. L'opérateur est un symbole qui effectue une opération spécifique entre deux opérandes, selon une définition. Les opérateurs servent de base sur laquelle la logique est construite dans un programme dans un langage de programmation particulier. Dans chaque langage de programmation, certains opérateurs effectuent plusieurs tâches. Comme les autres langages, Python possède également certains opérateurs, qui sont indiqués ci-dessous :

  • Opérateurs arithmétiques
  • Opérateurs de comparaison
  • Opérateurs d'affectation
  • Opérateurs logiques
  • Opérateurs au niveau du bit
  • Opérateurs d'adhésion
  • Opérateurs d'identité
  • Opérateurs arithmétiques

Opérateurs arithmétiques

Opérateurs arithmétiques utilisés entre deux opérandes pour une opération particulière. Il existe de nombreux opérateurs arithmétiques. Il comprend l'opérateur exposant (**) ainsi que les opérateurs + (addition), - (soustraction), * (multiplication), / (division), % (rappel) et // (division étage).

Considérez le tableau suivant pour une explication détaillée des opérateurs arithmétiques.

Opérateur Description
+ (Ajout) Il est utilisé pour ajouter deux opérandes. Par exemple, si a = 10, b = 10 => a+b = 20
- (Soustraction) Il est utilisé pour soustraire le deuxième opérande du premier opérande. Si le premier opérande est inférieur au deuxième opérande, la valeur est négative. Par exemple, si a = 20, b = 5 => a - b = 15
/ (diviser) Il renvoie le quotient après avoir divisé le premier opérande par le deuxième opérande. Par exemple, si a = 20, b = 10 => a/b = 2,0
* (Multiplications) Il sert à multiplier un opérande par l’autre. Par exemple, si a = 20, b = 4 => a * b = 80
% (rappel) Il renvoie le rappel après avoir divisé le premier opérande par le deuxième opérande. Par exemple, si a = 20, b = 10 => a%b = 0
** (Exposant) Comme il calcule la puissance du premier opérande par rapport au deuxième opérande, il s'agit d'un opérateur exposant.
// (Division étage) Il fournit la valeur plancher du quotient, obtenue en divisant les deux opérandes.

Code du programme :

Nous donnons maintenant des exemples de code d'opérateurs arithmétiques en Python. Le code est donné ci-dessous -

 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) 

Sortir:

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

nouvelle ligne python
 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 

Opérateur de comparaison

Les opérateurs de comparaison sont principalement utilisés à des fins de comparaison. Les opérateurs de comparaison comparent les valeurs des deux opérandes et renvoient une valeur booléenne vraie ou fausse en conséquence. L'exemple d'opérateurs de comparaison sont ==, !=, =, >,<. in the below table, we explain works of operators.< p>

Opérateur Description
== Si les valeurs de deux opérandes sont égales, alors la condition devient vraie.
!= Si les valeurs de deux opérandes ne sont pas égales, alors la condition devient vraie.
<=< td> La condition est remplie si le premier opérande est inférieur ou égal au deuxième opérande.
>= La condition est remplie si le premier opérande est supérieur ou égal au deuxième opérande.
> Si le premier opérande est supérieur au deuxième opérande, alors la condition devient vraie.
< Si le premier opérande est inférieur au deuxième opérande, alors la condition devient vraie.

Code du programme :

Nous donnons maintenant des exemples de code d’opérateurs de comparaison en Python. Le code est donné ci-dessous -

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

Opérateurs d'affectation

À l'aide des opérateurs d'affectation, la valeur de l'expression de droite est affectée à l'opérande de gauche. Il existe quelques exemples d'opérateurs d'affectation tels que =, +=, -=, *=, %=, **=, //=. Dans le tableau ci-dessous, nous expliquons les travaux des opérateurs.

Opérateur Description
= Il attribue la valeur de l'expression de droite à l'opérande de gauche.
+= En multipliant la valeur de l'opérande de droite par la valeur de l'opérande de gauche, l'opérande de gauche reçoit une valeur modifiée. Par exemple, si a = 10, b = 20 => a+ = b sera égal à a = a+ b et donc a = 30.
-= Il diminue la valeur de l'opérande gauche de la valeur de l'opérande droit et attribue la valeur modifiée à l'opérande gauche. Par exemple, si a = 20, b = 10 => a- = b sera égal à a = a- b et donc, a = 10.
*= Il multiplie la valeur de l'opérande de gauche par la valeur de l'opérande de droite et attribue la valeur modifiée à l'opérande de gauche. Par exemple, si a = 10, b = 20 => a* = b sera égal à a = a* b et donc a = 200.
%= Il divise la valeur de l'opérande de gauche par la valeur de l'opérande de droite et attribue le rappel à l'opérande de gauche. Par exemple, si a = 20, b = 10 => a % = b sera égal à a = a % b et donc a = 0.
**= a**=b sera égal à a=a**b, par exemple, si a = 4, b =2, a**=b attribuera 4**2 = 16 à a.
//= A//=b sera égal à a = a// b, par exemple, si a = 4, b = 3, a//=b attribuera 4//3 = 1 à a.

Code du programme :

Nous donnons maintenant des exemples de code d’opérateurs d’affectation en Python. Le code est donné ci-dessous -

 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) 

Sortir:

java datetime locale

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

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

Opérateurs au niveau du bit

Les valeurs des deux opérandes sont traitées petit à petit par les opérateurs bit à bit. Les exemples d'opérateurs au niveau du bit sont OR au niveau du bit (|), AND au niveau du bit (&), XOR au niveau du bit (^), négation (~), décalage vers la gauche (<>). Considérons le cas ci-dessous.

Par exemple,

 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 

Dans le tableau ci-dessous, nous expliquons le travail des opérateurs au niveau du bit.

Opérateur Description
& (binaire et) Un 1 est copié dans le résultat si les deux bits de deux opérandes au même emplacement sont 1. Sinon, 0 est copié.
| (binaire ou) Le bit résultant sera 0 si les deux bits sont nuls ; sinon, le bit résultant sera 1.
^ (xor binaire) Si les deux bits sont différents, le bit résultat sera 1, sinon il sera 0.
~ (négation) Les bits de l'opérande sont calculés comme leurs négations, donc si un bit est 0, le bit suivant sera 1, et vice versa.
<< (décalage à gauche) Le nombre de bits de l'opérande de droite est multiplié par le décalage vers la gauche de la valeur de l'opérande de gauche.
>> (décalage à droite) L'opérande de gauche est déplacé vers la droite du nombre de bits présents dans l'opérande de droite.

Code du programme :

tableau de réaction

Nous donnons maintenant des exemples de code d'opérateurs Bitwise en Python. Le code est donné ci-dessous -

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

Sortir:

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

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

Opérateurs logiques

L'évaluation des expressions pour prendre des décisions utilise généralement des opérateurs logiques. Les exemples d'opérateurs logiques sont et, ou, et non. Dans le cas du ET logique, si le premier est 0, cela ne dépend pas du second. Dans le cas du OU logique, si le premier vaut 1, cela ne dépend pas du second. Python prend en charge les opérateurs logiques suivants. Dans le tableau ci-dessous, nous expliquons le travail des opérateurs logiques.

Opérateur Description
et La condition sera également vraie si l’expression est vraie. Si les deux expressions a et b sont identiques, alors a et b doivent être tous deux vrais.
ou La condition sera vraie si l’une des phrases est vraie. Si a et b sont les deux expressions, alors an ou b doit être vrai si et est vrai et b est faux.
pas Si une expression un est vrai, alors non (a) sera faux et vice versa.

Code du programme :

Nous donnons maintenant des exemples de code d'opérateurs arithmétiques en Python. Le code est donné ci-dessous -

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

Opérateurs d'adhésion

L'appartenance d'une valeur à l'intérieur d'une structure de données Python peut être vérifiée à l'aide des opérateurs d'appartenance Python. Le résultat est vrai si la valeur est dans la structure de données ; sinon, il renvoie false.

Opérateur Description
dans Si le premier opérande est introuvable dans le deuxième opérande, il est évalué comme étant vrai (liste, tuple ou dictionnaire).
pas dedans Si le premier opérande n'est pas présent dans le deuxième opérande, l'évaluation est vraie (liste, tuple ou dictionnaire).

Code du programme :

Nous donnons maintenant des exemples de code d’opérateurs d’adhésion en Python. Le code est donné ci-dessous -

 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) 

Sortir:

comment afficher une application dans Android

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

 Is value Present? True Is value not Present? True 

Opérateurs d'identité

Opérateur Description
est Si les références des deux côtés pointent vers le même objet, cela est considéré comme vrai.
n'est pas Si les références des deux côtés ne pointent pas vers le même objet, cela est considéré comme vrai.

Code du programme :

Nous donnons maintenant des exemples de code d'opérateurs d'identité en Python. Le code est donné ci-dessous -

 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) 

Sortir:

10 sur 100

Maintenant, nous compilons le code ci-dessus en python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

 True False False True True False 

Priorité des opérateurs

L'ordre dans lequel les opérateurs sont examinés est crucial à comprendre car il nous indique quel opérateur doit être considéré en premier. Vous trouverez ci-dessous une liste des tables de priorité des opérateurs Python.

Opérateur Description
** Dans l'ensemble des autres opérateurs utilisés dans l'expression, l'opérateur exposant est prioritaire.
~ + - le moins, le plus unaire et la négation.
*/% // la division de l'étage, les modules, la division et la multiplication.
+ - Binaire plus et moins
>> << Décalage à gauche. et décalage à droite
& Binaire et.
^ | Xor binaire, et ou
<=>= Opérateurs de comparaison (inférieur à, inférieur à égal à, supérieur à, supérieur à égal à).
== != Opérateurs d’égalité.
= %= /= //= -= +=
*= **=
Opérateurs d'affectation
ce n'est pas Opérateurs d'identité
pas dans Opérateurs d'adhésion
pas ou et Opérateurs logiques

Conclusion:

Ainsi, dans cet article, nous discutons de tous les opérateurs Python. Nous discutons brièvement de leur fonctionnement et partageons le code du programme en utilisant chaque opérateur en Python.