Previous
Next
Share This Page On :

Python Operators - Operators in Python

Operators in Python

Python Operators

Python provides a rich set of operators to manipulate variables. In other words Python Operators are special symbols used in arithmetic or logical computations. All the Python operators are classified into the following groups.

What are Operators ?

In any programming language operators are used to perform operations on variables and values. For example assignment operations, arithmetic operations, comparison operations and so on.

Python Assignment Operators

Python assignment operators are used to assign a value to variables.

List of Assignment Operators in Python
Operator Example Another Way
= a = 7 a = 7
+= a += 2 a = a + 2
-= a -= 2 a = a - 2
*= a *= 2 a = a * 2
/= a /= 2 a = a / 2
%= a %= 2 a = a % 2
//= a //= 2 a = a // 2
**= a **= 2 a = a ** 2
&= a &= 2 a = a & 2
|= a |= 2 a = a | 2
^= a ^= 2 a = a ^ 2
>>= a >>= 2 a = a >> 2
<<= a <<= 2 a = a << 2

Python Arithmetic Operators

Python arithmetic operators are used with numeric values to perform arithmetic ( mathematical ) operations.

List of Arithmetic Operators in Python
Operator Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y

Python Comparison Operators

Python comparison operators are used to compare two values.

List of Comparison Operators in Python
Operator Operator Name Example
== Equal 3 == 2
!= Not equal 3 != 2
> Greater than 3 > 2
< Less than 3 < 2
>= Greater than or equal to 3 >= 2
<= Less than or equal to 3 <= 2

Python Logical Operators

Python Logical operators are used to combine conditional statements.

List of Logical Operators in Python
Operator Operator Description Example
and Returns True if both statements are true a < 9 and a < 7
or Returns True if one of the statements is true a < 9 or a < 4
not Returns False if the result is True not(a < 9 and a < 7)

Python Identity Operators

Python identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

List of Identity Operators in Python
Operator Operator Description Example
is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y

Python Membership Operators

Python Membership operators are used to test if a sequence is present in an object.

List of Membership Operators in Python
Operator Operator Description Example
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y

Python Bitwise Operators

Python Bitwise operators are used to compare binary numbers.

List of Bitwise Operators in Python
Operator Operator Name Operator Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

More Related Tutorials

Previous
Next