Python Operators

Python Operators

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:

Operator Name Example Try It
+ Addition x + y Try It
- Subtraction x - y Try It
* Multiplication x * y Try It
/ Division x / y Try It
% Modulo x % y Try It
** Power x ** y Try It
// Floor division (integer division) x // y Try It

Python assignment operators

Assignment operators are used to assign values to variables:

Operator Example Equivalent to Try It
= x = 5 x = 5 Try It
+= x += 3 x = x + 3 Try It
-= x -= 3 x = x - 3 Try It
*= x *= 3 x = x * 3 Try It
/= x /= 3 x = x / 3 Try It
%= x %= 3 x = x % 3 Try It
//= x //= 3 x = x // 3 Try It
**= x **= 3 x = x ** 3 Try It
&= x &= 3 x = x & 3 Try It
|= x |= 3 x = x | 3 Try It
^= x ^= 3 x = x ^ 3 Try It
>>= x >>= 3 x = x >> 3 Try It
<<= x <<= 3 x = x << 3 Try It

Python comparison operators

Comparison operators are used to compare two values:

Operator Name Example Try It
== Equal to x == y Try It
!= Not equal to x != y Try It
> Greater than x > y Try It
< Less than x < y Try It
>= Greater than or equal to x >= y Try It
<= Less than or equal to x <= y Try It

Python logical operators

Logical operators are used to combine conditional statements:

Operator Description Example Try It
and If both statements are true, then return True. x > 3 and x < 10 Try It
or If one of the statements is true, then return True. x > 3 or x < 4 Try It
not Invert the result, if the result is true, then return False not(x > 3 and x < 10) Try It

Python Identity Operators

Identity operators are used to compare objects, not to compare whether they are equal, but if they are actually the same object, they have the same memory location:

Operator Description Example Try It
is Return true if the two variables are the same object. x is y Try It
is not Return true if the two variables are not the same object. x is not y Try It

Python Membership Operators

Membership operators are used to test whether a sequence appears in an object:

Operator Description Example Try It
in Return True if the specified value exists in the sequence of the object. x in y Try It
not in Return True if the specified value does not exist in the sequence of the object. x not in y Try It

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

Operator Description Example
& AND Set each bit to 1 if both bits are 1.
| OR Set each bit to 1 if one of the two bits is 1.
^ XOR Set each bit to 1 if only one of the two bits is 1.
~ NOT Reverse all bits.
<< Zero fill left shift Move to the left by pushing zero to the right, pushing off the leftmost position.
>> Signed right shift Move to the right by pushing the copy of the leftmost position to the right, pushing off the rightmost position.