logo

Operadores de Python

Introducción:

En este artículo, analizamos los operadores de Python. El operador es un símbolo que realiza una operación específica entre dos operandos, según una definición. Los operadores sirven como base sobre la cual se construye la lógica en un programa en un lenguaje de programación particular. En cada lenguaje de programación, algunos operadores realizan varias tareas. Al igual que otros lenguajes, Python también tiene algunos operadores, que se detallan a continuación:

  • Operadores aritméticos
  • Operadores de comparación
  • Operadores de Asignación
  • Operadores logicos
  • Operadores bit a bit
  • Operadores de membresía
  • Operadores de identidad
  • Operadores aritméticos

Operadores aritméticos

Operadores aritméticos utilizados entre dos operandos para una operación particular. Hay muchos operadores aritméticos. Incluye el operador exponente (**), así como los operadores + (suma), - (resta), * (multiplicación), / (división), % (recordatorio) y // (división de piso).

Considere la siguiente tabla para obtener una explicación detallada de los operadores aritméticos.

Operador Descripción
+ (Adición) Se utiliza para sumar dos operandos. Por ejemplo, si a = 10, b = 10 => a+b = 20
- (Resta) Se utiliza para restar el segundo operando del primer operando. Si el primer operando es menor que el segundo operando, el valor resulta negativo. Por ejemplo, si a = 20, b = 5 => a - b = 15
/ (dividir) Devuelve el cociente después de dividir el primer operando por el segundo operando. Por ejemplo, si a = 20, b = 10 => a/b = 2,0
* (Multiplicación) Se utiliza para multiplicar un operando por otro. Por ejemplo, si a = 20, b = 4 => a * b = 80
% (recordatorio) Devuelve el recordatorio después de dividir el primer operando por el segundo operando. Por ejemplo, si a = 20, b = 10 => a%b = 0
** (Exponente) Como calcula la potencia del primer operando con respecto al segundo operando, es un operador exponente.
// (División de piso) Proporciona el valor mínimo del cociente, que se obtiene dividiendo los dos operandos.

Código de programa:

Ahora damos ejemplos de código de operadores aritméticos en Python. El código se proporciona a continuación:

 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) 

Producción:

Ahora compilamos el código anterior en Python y, después de una compilación exitosa, lo ejecutamos. Entonces el resultado se proporciona a continuación:

fuente de látex
 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 

Operador de comparación

Los operadores de comparación se utilizan principalmente con fines de comparación. Los operadores de comparación comparan los valores de los dos operandos y devuelven un valor booleano verdadero o falso de acuerdo. El ejemplo de operadores de comparación son ==, !=, =, >,<. in the below table, we explain works of operators.< p>

Operador Descripción
== Si el valor de dos operandos es igual, entonces la condición se vuelve verdadera.
!= Si el valor de dos operandos no es igual, entonces la condición se vuelve verdadera.
<=< td> La condición se cumple si el primer operando es menor o igual que el segundo operando.
>= La condición se cumple si el primer operando es mayor o igual que el segundo operando.
> Si el primer operando es mayor que el segundo operando, entonces la condición se vuelve verdadera.
< Si el primer operando es menor que el segundo operando, entonces la condición se vuelve verdadera.

Código de programa:

Ahora damos ejemplos de código de operadores de comparación en Python. El código se proporciona a continuación:

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

Operadores de Asignación

Usando los operadores de asignación, el valor de la expresión derecha se asigna al operando izquierdo. Hay algunos ejemplos de operadores de asignación como =, +=, -=, *=, %=, **=, //=. En la siguiente tabla explicamos el trabajo de los operadores.

estructura en estructura de datos
Operador Descripción
= Asigna el valor de la expresión derecha al operando izquierdo.
+= Al multiplicar el valor del operando derecho por el valor del operando izquierdo, el operando izquierdo recibe un valor modificado. Por ejemplo, si a = 10, b = 20 => a+ = b será igual a a = a+ b y por tanto, a = 30.
-= Disminuye el valor del operando izquierdo por el valor del operando derecho y asigna el valor modificado nuevamente al operando izquierdo. Por ejemplo, si a = 20, b = 10 => a- = b será igual a a = a- b y por tanto, a = 10.
*= Multiplica el valor del operando izquierdo por el valor del operando derecho y asigna el valor modificado al operando izquierdo. Por ejemplo, si a = 10, b = 20 => a* = b será igual a a = a* b y por tanto, a = 200.
%= Divide el valor del operando izquierdo por el valor del operando derecho y asigna el recordatorio al operando izquierdo. Por ejemplo, si a = 20, b = 10 => a % = b será igual a a = a % b y por tanto, a = 0.
**= a**=b será igual a a=a**b, por ejemplo, si a = 4, b =2, a**=b asignará 4**2 = 16 a a.
//= A//=b será igual a a = a// b, por ejemplo, si a = 4, b = 3, a//=b asignará 4//3 = 1 a a.

Código de programa:

Ahora damos ejemplos de código de operadores de asignación en Python. El código se proporciona a continuación:

 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) 

Producción:

Ahora compilamos el código anterior en Python y, después de una compilación exitosa, lo ejecutamos. Entonces el resultado se proporciona a continuación:

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

Operadores bit a bit

Los valores de los dos operandos son procesados ​​bit a bit por los operadores bit a bit. Los ejemplos de operadores bit a bit son OR bit a bit (|), AND bit a bit (&), XOR bit a bit (^), negación (~), desplazamiento a la izquierda (<>). Considere el caso siguiente.

Por ejemplo,

 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 

En la siguiente tabla, explicamos el trabajo de los operadores bit a bit.

Operador Descripción
& (binario y) Se copia un 1 al resultado si ambos bits de dos operandos en la misma ubicación son 1. De lo contrario, se copia 0.
| (binario o) El bit resultante será 0 si ambos bits son cero; de lo contrario, el bit resultante será 1.
^ (xor binario) Si los dos bits son diferentes, el bit de resultado será 1; en caso contrario, será 0.
~ (negación) Los bits del operando se calculan como sus negaciones, por lo que si un bit es 0, el siguiente bit será 1 y viceversa.
<< (desplazamiento a la izquierda) El número de bits del operando derecho se multiplica por el desplazamiento hacia la izquierda del valor del operando izquierdo.
>> (desplazamiento a la derecha) El operando izquierdo se mueve hacia la derecha la cantidad de bits presentes en el operando derecho.

Código de programa:

javafx

Ahora damos ejemplos de código de operadores Bitwise en Python. El código se proporciona a continuación:

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

Producción:

Ahora compilamos el código anterior en Python y, después de una compilación exitosa, lo ejecutamos. Entonces el resultado se proporciona a continuación:

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

Operadores logicos

La evaluación de expresiones para la toma de decisiones suele utilizar operadores lógicos. Los ejemplos de operadores lógicos son y, o, y no. En el caso del AND lógico, si el primero es 0, no depende del segundo. En el caso del OR lógico, si el primero es 1, no depende del segundo. Python admite los siguientes operadores lógicos. En la siguiente tabla, explicamos el trabajo de los operadores lógicos.

Operador Descripción
y La condición también será verdadera si la expresión es verdadera. Si las dos expresiones a y b son iguales, entonces a y b deben ser ambas verdaderas.
o La condición será verdadera si una de las frases es verdadera. Si a y b son dos expresiones, entonces an o b deben ser verdaderas si y es verdadera y b es falsa.
no Si una expresión a es verdadera, entonces no (a) será falsa y viceversa.

Código de programa:

Ahora damos ejemplos de código de operadores aritméticos en Python. El código se proporciona a continuación:

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

Operadores de membresía

La membresía de un valor dentro de una estructura de datos de Python se puede verificar utilizando operadores de membresía de Python. El resultado es verdadero si el valor está en la estructura de datos; de lo contrario, devuelve falso.

cadena.valor de java
Operador Descripción
en Si el primer operando no se puede encontrar en el segundo operando, se evalúa como verdadero (lista, tupla o diccionario).
no en Si el primer operando no está presente en el segundo operando, la evaluación es verdadera (lista, tupla o diccionario).

Código de programa:

Ahora damos ejemplos de código de operadores de membresía en Python. El código se proporciona a continuación:

 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) 

Producción:

Ahora compilamos el código anterior en Python y, después de una compilación exitosa, lo ejecutamos. Entonces el resultado se proporciona a continuación:

verma dhanashree
 Is value Present? True Is value not Present? True 

Operadores de identidad

Operador Descripción
es Si las referencias de ambos lados apuntan al mismo objeto, se determina que es verdadero.
no es Si las referencias de ambos lados no apuntan al mismo objeto, se determina que es verdadero.

Código de programa:

Ahora damos ejemplos de código de operadores de identidad en Python. El código se proporciona a continuación:

 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) 

Producción:

Ahora compilamos el código anterior en Python y, después de una compilación exitosa, lo ejecutamos. Entonces el resultado se proporciona a continuación:

 True False False True True False 

Precedencia del operador

Es fundamental comprender el orden en el que se examinan los operadores, ya que nos dice qué operador debe considerarse primero. A continuación se muestra una lista de las tablas de precedencia de los operadores de Python.

Operador Descripción
** En general, sobre otros operadores empleados en la expresión, se da prioridad al operador exponente.
~ + - el menos, el más unario y la negación.
* / % // la división del piso, los módulos, la división y la multiplicación.
+ - Binario más y menos
>> << Shift izquierdo. y desplazamiento a la derecha
& Binario y.
^ | xor binario, y o
<=>= Operadores de comparación (menor que, menor que igual que, mayor que, mayor que igual que).
== != Operadores de igualdad.
= %= /= //= -= +=
*= **=
Operadores de Asignación
no lo es Operadores de identidad
en no en Operadores de membresía
no o y Operadores logicos

Conclusión:

Entonces, en este artículo, analizamos todos los operadores de Python. Discutimos brevemente cómo funcionan y compartimos el código del programa usando cada operador en Python.