Python Operators
Python has several types of operators for performing operations on variables and values. Here are the main categories of operators in Python:
Used for performing mathematical operations.
+ # Addition
- # Subtraction
* # Multiplication
/ # Division
% # Modulus
** # Exponentiation
// # Floor division
Used to compare two values.
== # Equal to
!= # Not equal to
> # Greater than
< # Less than
>= # Greater than or equal to
<= # Less than or equal to
Used to combine conditional statements.
and # Returns True if both statements are true
or # Returns True if one of the statements is true
not # Reverse the result, returns False if the result is true
Used to assign values to variables.
= # x = 5 is the same as x = 5
+= # x += 3 is the same as x = x + 3
-= # x -= 3 is the same as x = x - 3
*= # x *= 3 is the same as x = x * 3
/= # x /= 3 is the same as x = x / 3
%= # x %= 3 is the same as x = x % 3
//= # x //= 3 is the same as x = x // 3
**= # x **= 3 is the same as x = x ** 3
Used to compare binary numbers.
& # AND
| # OR
^ # XOR
~ # NOT
<< # Zero fill left shift
>> # Signed right shift
Used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.
is # Returns True if both variables are the same object
is not # Returns True if both variables are not the same object
Used to test if a sequence is presented in an object.
in # Returns True if a sequence with the specified value is present in the object
not in # Returns True if a sequence with the specified value is not present in the object