Operadores de Python
- Página anterior Booleano de Python
- Página siguiente Listas de Python
Operadores de Python
Operators are used to perform operations on variables and values.
Python divides operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Python arithmetic operators
Arithmetic operators are used with numbers to perform common mathematical operations:
Operador | Name | Ejemplo | Prueba |
---|---|---|---|
+ | Addition | x + y | Prueba |
- | Subtraction | x - y | Prueba |
* | Multiplication | x * y | Prueba |
/ | Division | x / y | Prueba |
% | Modulo | x % y | Prueba |
** | Power | x ** y | Prueba |
// | Floor division (integer division) | x // y | Prueba |
Python assignment operators
Assignment operators are used to assign values to variables:
Operador | Ejemplo | Equivalent to | Prueba |
---|---|---|---|
= | x = 5 | x = 5 | Prueba |
+= | x += 3 | x = x + 3 | Prueba |
-= | x -= 3 | x = x - 3 | Prueba |
*= | x *= 3 | x = x * 3 | Prueba |
/= | x /= 3 | x = x / 3 | Prueba |
%= | x %= 3 | x = x % 3 | Prueba |
//= | x //= 3 | x = x // 3 | Prueba |
**= | x **= 3 | x = x ** 3 | Prueba |
&= | x &= 3 | x = x & 3 | Prueba |
|= | x |= 3 | x = x | 3 | Prueba |
^= | x ^= 3 | x = x ^ 3 | Prueba |
>>= | x >>= 3 | x = x >> 3 | Prueba |
<<= | x <<= 3 | x = x << 3 | Prueba |
Python comparison operators
Comparison operators are used to compare two values:
Operador | Name | Ejemplo | Prueba |
---|---|---|---|
== | Equal to | x == y | Prueba |
!= | Not equal | x != y | Prueba |
> | Greater than | x > y | Prueba |
< | Less than | x < y | Prueba |
>= | Greater than or equal to | x >= y | Prueba |
<= | Less than or equal to | x <= y | Prueba |
Python logical operators
Logical operators are used to combine conditional statements:
Operador | Descripción | Ejemplo | Prueba |
---|---|---|---|
and | If both statements are true, then return True. | x > 3 and x < 10 | Prueba |
or | If one of the statements is true, then return True. | x > 3 or x < 4 | Prueba |
not | Invert the result, if the result is true, then return False | not(x > 3 and x < 10) | Prueba |
Operadores de identidad de Python
Los operadores de identidad se utilizan para comparar objetos, no para comparar si son iguales, sino si realmente son el mismo objeto, tienen la misma ubicación en la memoria:
Operador | Descripción | Ejemplo | Prueba |
---|---|---|---|
is | Si dos variables son el mismo objeto, se devuelve true. | x is y | Prueba |
is not | Si dos variables no son el mismo objeto, se devuelve true. | x is not y | Prueba |
Operadores de membresía de Python
Los operadores de membresía se utilizan para probar si la secuencia se encuentra en el objeto:
Operador | Descripción | Ejemplo | Prueba |
---|---|---|---|
in | Si la secuencia existe en el objeto con el valor especificado, se devuelve True. | x in y | Prueba |
not in | Si la secuencia no existe en el objeto con el valor especificado, se devuelve True. | x not in y | Prueba |
Operadores de bits de Python
Los operadores de bits se utilizan para comparar (binario) números:
Operador | Descripción | Ejemplo |
---|---|---|
& | AND | Si ambos bits son 1, se establece cada bit en 1. |
| | OR | Si uno de los dos bits es 1, se establece cada bit en 1. |
^ | XOR | Si solo un bit de dos es 1, se establece cada bit en 1. |
~ | NOT | Invierte todos los bits. |
<< | Desplazamiento a la izquierda con relleno cero | Al desplazar hacia la izquierda introduciendo ceros desde la derecha, se elimina el extremo más izquierdo. |
>> | Desplazamiento a la derecha firmado | Al desplazar hacia la derecha desde la copia del extremo más izquierdo, se elimina el extremo más derecho. |
- Página anterior Booleano de Python
- Página siguiente Listas de Python