Python Operators are used to performing operations on values and variables. Operator is nothing but used with Variables to get some satisfying output. Using operators you can perform the operation on variables like `1 + 2 = 3`

where `+, =`

are operators.

## Types of Operators

Python divides the operators in the following groups:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators

Let us have a look at all the operators one by one:

**Python Arithmetic Operators**

Arithmetic Operators are used to perform arithmetic operation.

Operator | Example | Same As |
---|---|---|

+ | Addition | x + y |

– | Subtraction | x – y |

* | Multiplication | x * y |

/ | Division | x / y |

% | Modulus | x % y |

** | Exponentiation | x ** y |

// | Floor division | x // y |

x = 5

y = 3

print("Additon:",x + y)

print("Substraction:",x - y)

print("Multiplication:",x * y)

print("Division:",x / y)

print("Modulus:",x % y)

print("Exponentiation:",x ** y)

print("Floor Division:",x // y)

## Python Assignment Operators

Assignment operators are used to assign values to variables:

Operator | Example | Same As |
---|---|---|

= | x = 5 | x = 5 |

+= | x += 3 | x = x + 3 |

-= | x -= 3 | x = x – 3 |

*= | x *= 3 | x = x * 3 |

/= | x /= 3 | x = x / 3 |

%= | x %= 3 | x = x % 3 |

//= | x //= 3 | x = x // 3 |

= | x **= 3 | x = x ** 3 |

&= | x &= 3 | x = x & 3 |

|= | x |= 3 | x = x | 3 |

^= | x ^= 3 | x = x ^ 3 |

>>= | x >>= 3 | x = x >> 3 |

<<= | x <<= 3 | x = x << 3 |

x = 5

print("Equal Sign:",x )

x = 5

x += 3

print("Addition Assign:",x)

x = 5

x -= 3

print("Substraction Assign:",x)

x = 5

x *= 3

print("Multiplication Assign:",x)

x = 5

x /= 3

print("Division Assign:",x)

x = 5

x %= 3

print("Modulus Assign:",x)

x = 5

x **= 3

print("Exponentiation Assign:",x)

x = 5

x //= 3

print("Floor Division Assign:",x)

## Python Comparison Operators

Comparison operators are used to compare two values:

Operator | Name | Example |
---|---|---|

== | Equal | x == y |

!= | Not equal | x != y |

> | Greater than | x > y |

< | Less than | x < y |

>= | Greater than or equal to | x >= y |

<= | Less than or equal to | x <= y |

x = 5

y = 3

print("Equal:",x == y)

print("Not Equal:",x != y)

print("Greater Than:",x > y)

print("Less Than:",x < y)

print("Greater than or equal to:",x >= y)

print("Less than or equal to:",x <= y)

## Python Logical Operators

Logical operators are used to combine conditional statements:

Operator | Description | Example |
---|---|---|

and | Returns True if both statements are true | x < 5 and x < 10 |

or | Returns True if one of the statements is true | x < 5 or x < 4 |

not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |

x = 5

print(x > 3 and x < 10)

# returns True because 5 is greater than 3 AND 5 is less than 10

print(x > 3 or x < 10)

# returns True because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)

print(not(x > 3 and x < 10))

# returns False because not is used to reverse the result

## Python Identity Operators

Identity operators are used to comparing the objects, not if they are equal, but if they are actually the same object, with the same memory location:

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 |

x = ["apple", "banana"]

y = ["apple", "banana"]

z = x

print(x is z)

# returns True because z is the same object as x

print(x is not z)

# returns False because z is the same object as x

## Python Membership Operators

Membership operators are used to testing if a sequence is presented in an object:

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 |

x = ["apple", "banana"]

print("banana" in x)

# returns True because a sequence with the value "banana" is in the list

print("pineapple" not in x)

# returns True because a sequence with the value "pineapple" is not in the list

## Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

Operator | Name | 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 |

Whats Next – Python – If…Else Condition

## Leave a Reply