### Python Operators

In Python programming, operators are used to perform operations on operands (i.e. variables and values).

Python categorizes the operators in the following groups:

1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bit-wise operators

### 1. Arithmetic Operators:

These operators are used with numeric values to perform common mathematical operations.

Table 1: Arithmetic operators in Python
OperatorMeaningExample
Subtract right operand from the leftx – y
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – remainder of the division of left operand by the rightx % y
//Floor division – division that results into whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of rightx**y

#### Example 1

`# Arithmetic operators in Pythonx = 27y = 4print(x+y)  # additionprint(x-y)  # subtractionprint(x*y)  # multiplicationprint(x/y)  # division (float)print(x%y)  # modulus print(x//y) # division (int)print(x**y) # exponent`
##### Output:
`31231086.7536531441`

#### 2. Assignment Operators:

These operators are used to assign values to variables.

Table 2: Assignment operators in Python
OperatorExampleEquivalent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x – 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

#### Example 2

`# Assignment operators in Pythonx = 5x = x + 5  # x += 5print(x)x = x - 5  # x -= 5print(x)x = x * 5  # x *= 5print(x)x = x / 5  # x /= 5print(x)x = x % 5  # x %= 5print(x)`
##### Output:
`105255.00.0`

#### 3. Relational Operators:

These operators are used to compare two values.

Table 3: Relational operators in Python
OperatorMeaningExample
>Greater that – True if left operand is greater than the rightx > y
<Less that – True if left operand is less than the rightx < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the rightx <= y

#### Example 3

`# Relational operators in Pythona = 10b = 5print(a>b)  # Trueprint(a<b)  # Falseprint(a==b) # Falseprint(a!=b) # Trueprint(a>=b) # Trueprint(a<=b) # False`
##### Output:
`TrueFalseFalseTrueTrueFalse`

#### 4. Logical Operators:

These operators are used to combine conditional statements.

Table 4: Logical operators in Python
OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x

#### Example 4

`# Logical operators in Pythonprint("Logical AND : ", (10>5) and (9<7)) # Falseprint("Logical  OR : ", (10>5) or (9<7))  # Trueprint("Logical NOT : ", not(9<7))         # True`

Output:

`Logical AND : FalseLogical  OR : TrueLogical NOT : True`

#### 5. Identity Operators:

These 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.

Table 5: Identity operators in Python
OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

#### Example 5

`# Identity operators in Pythonprint("Identity operators:-")x = Trueprint(x is True)      # Trueprint(x is not True)  # False`
##### Output:
`Identity operators:-TrueFalse`

#### 6. Membership Operators:

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

Table 6: Membership operators in Python
OperatorMeaningExample
inTrue if value/variable is found in the sequence5 is x
not inTrue if value/variable is not found in the sequence5 not in x

#### Example 6

`# Membership operators in Pythonprint("Membership operators:-")arr = [10, 20, 30, 40, 50]x = 40print(x in arr)     # Truex = 60print(x not in arr) # True`
##### Output:
`Membership operators:-TrueTrue`

#### 7. Bit-wise Operators:

These operators are used to compare binary numbers.

For example, in the table below, let x = 10 (`0000 1010` in binary) and y = 4 (`0000 0100` in binary)

Table 7: Bit-wise operators in Python
OperatorMeaningExample
&Bit-wise ANDx& y = 0 (`0000 0000`)
|Bit-wise ORx | y = 14 (`0000 1110`)
~Bit-wise NOT~x = -11 (`1111 0101`)
^Bit-wise XORx ^ y = 14 (`0000 1110`)
>>Bit-wise right shiftx>> 2 = 2 (`0000 0010`)
<<Bit-wise left shiftx<< 2 = 40 (`0010 1000`)​

#### Example 7

`# Bit-wise operators in Pythonx = 10y = 4print(x & y)  # Bit-wise AND: 0print(x | y)  # Bit-wise OR: 14print(~x)     # Bit-wise NOT: -11print(x ^ y)  # Bit-wise XOR: 14print(x>>2)   # Bit-wise right shift: 2print(x<<2)   # Bit-wise left shift: 40`
##### Output:
`014-1114240`