Python Operators Reference

Quick reference to the operators of Python.

Assignment Operators
Operator Does what
= Assigns values from right side operands to left side operand.
c = a + b assigns value of a + b into c
+= Add AND: Adds right operand to the left operand and assigns the result to left operand.
c += a is equivalent to c = c + a
-= Subtract AND: Subtracts right operand from the left operand and assigns the result to left operand.
c -= a is equivalent to c = c - a
*= Multiply AND: Multiplies right operand with the left operand and assigns the result to left operand.
c *= a is equivalent to c = c * a
/= Divide AND: Divides left operand with the right operand and assigns the result to left operand.
c /= a is equivalent to c = c. ac /= a is equivalent to c = c / a
%= Modulus AND: Takes modulus using two operands and assigns the result to left operand.
c %= a is equivalent to c = c % a
**= Exponent AND: Performs exponential (power) calculation on operators and assigns the result to the left operand.
c **= a is equivalent to c = c ** a
//= Floor Division: Performs floor division on operators and assigns the result to the left operand.
c //= a is equivalent to c = c // a
Bitwise Operators
Operator Does what
Usage: Bitwise operators works on bits and perform bit by bit operation.
& Binary AND: Copies a bit to the result if it exists in both operands.
| Binary OR: Copies a bit if it exists in either operand.
^ Binary XOR: Copies the bit if it is set in one operand but not both.
~ Binary Ones Complement: Is unary and has the effect of 'flipping' bits.
<< Binary Left Shift: The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift: The left operands value is moved right by the number of bits specified by the right operand.
Comparison / Relational Operators
Operator Does what
== Returns True if the values of the two operands are equal.
!= Returns True if the values of the two operands are not equal.
<> Returns True if the values of the two operands are not equal. This is similar to the != operator.
> Returns True if the value of the left operand is greater than the value of the right operand.
< Returns True if the value of the left operand is less than the value of the right operand.
>= Returns True if the value of the left operand is greater than or equal to the value of the right operand.
<= Returns True if the value of the left operand is less than or equal to the value of the right operand.
Logical / Membership / Identity Operators
Operator Does what
and Logical AND: If both the operands are true then condition becomes true.
or Logical OR: If any of the two operands are non-zero then condition becomes true.
in Membership: Evaluates to true if it finds a variable in the specified sequence and false otherwise.
is Identity: Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
is not Identity: Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.
not in Membership: Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
not Logical NOT: Used to reverse the logical state of its operand.
Math / Arithmetic Operators
Operator Does what
+ Addition
Adds values on either side of the operator.
- Subtraction
Subtracts right hand operand from left hand operand.
* Multiplication
Multiplies values on either side of the operator
/ Division
Divides left hand operand by right hand operand
% Modulus
Divides left hand operand by right hand operand and returns remainder
** Exponent
Performs exponential (power) calculation on operators
// Floor Division
A division where the digits after the decimal point are removed from the result. But if one of the operands is negative, the result is floored, i.e. rounded away from zero (towards negative infinity)
String Operators
Operator Does what
+ Concatenation - Adds values on either side of the operator
* Repetition - Creates new strings, concatenating multiple copies of the same string
[] Slice - Returns the character from the given index
[ : ] Range Slice - Returns the characters from the given range
in Membership - Returns true if a character exists in the given string
not in Membership - Returns true if a character does not exist in the given string
r/R Raw String - Suppresses escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator (the letter "r" or "R") which precedes the quotation marks. The "r" must be placed immediately preceding the first quote mark.
% Format - Performs string formatting

More: