Python Operators

Operators are the constructs which can manipulate the value of operands.

Python supports following operators.

- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operator
- Conditional Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

Operator | Meaning | Example |
---|---|---|

+ | Add two operands or unary plus | x + y |

- | Subtract right operand from the left or unary minus | x - y |

* | Multiply two operands | x * y |

/ | Divide left operand by the right one (always results into float) | x / y |

% | Modulus - remainder of the division of left operand by the right | x % y (remainder of x/y) |

// | Floor division - division that results into whole number adjusted to the left in the number line | x // y |

** | Exponent - left operand raised to the power of right | x**y (x to the power y) |

Example :

```
>>> x = 25
>>> y = 14
>>> print('x + y =',x+y)
x + y = 39
>>> print('x - y =',x-y)
x - y = 11
>>> print('x * y =',x*y)
x * y = 350
>>> print('x / y =',x/y)
x / y = 1.7857142857142858
>>> print('x // y =',x//y)
x // y = 1
>>> print('x ** y =',x**y)
x ** y = 37252902984619140625
>>>
```

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

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

== | If the values of two operands are equal, then the condition becomes true. | (a == b) is not true. |

!= | If values of two operands are not equal, then condition becomes true. | |

> | If the value of left operand is greater than the value of right operand, then condition becomes true. | (a > b) is not true. |

< | If the value of left operand is less than the value of right operand, then condition becomes true. | (a < b) is true. |

>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | (a >= b) is not true. |

<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | (a <= b) is true. |

Example :

```
>>> x = 15
>>> y = 20
>>> print('x > y is',x>y)
x > y is False
>>> print('x < y is',x<y)
x < y is True
>>> print('x == y is',x==y)
x == y is False
>>> print('x != y is',x!=y)
x != y is True
>>> print('x >= y is',x>=y)
x >= y is False
>>> print('x <= y is',x<=y)
x <= y is True
>>>
```

Logical operators are the and, or, not operators.

Operator |
Example |
Result |
---|---|---|

and | (x and y) | is True if both x and y are true. |

or | (x or y) | is True if either x or y is true. |

not | (x not y) | If a condition is true then Logical not operator will make false. |

Example :

```
>>> x = True
>>> y = False
>>> print('x and y is',x and y)
x and y is False
>>> print('x or y is',x or y)
x or y is True
>>> print('not x is',not x)
not x is False
>>>
```

Python assignment operators are used for assigning the value of the right operand to the left operand. Various assignment operators used in Python are (+=, - = , *=, /= , etc.)

Operator |
Shorthand |
Expression |
Description |
---|---|---|---|

+= | x+=y | x = x + y | Adds 2 numbers and assigns the result to left operand. |

-= | x-= y | x = x -y | Subtracts 2 numbers and assigns the result to left operand. |

*= | x*= y | x = x*y | Multiplies 2 numbers and assigns the result to left operand. |

/= | x/= y | x = x/y | Divides 2 numbers and assigns the result to left operand. |

%= | x%= y | x = x%y | Computes the modulus of 2 numbers and assigns the result to left operand. |

**= | x**=y | x = x**y | Performs exponential (power) calculation on operators and assign value to the equivalent to left operand. |

//= | x//=y | x = x//y | Performs floor division on operators and assign value to the left operand. |

Example :

```
>>> x = 12
>>> y = 7
>>> x+=y
>>> print(x)
19
>>> x-=y
>>> print(x)
12
>>> x*=y
>>> print(x)
84
>>> x/=y
>>> print(x)
12.0
>>> x%=y
>>> print(x)
5.0
>>> x**=y
>>> print(x)
78125.0
>>> x//=y
>>> print(x)
11160.0
>>>
```

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

Operator |
Shorthand |
Expression |
Description |
---|---|---|---|

& | And | x & y | Bits that are set in both x and y are set. |

| | Or | x | y | Bits that are set in either x or y are set. |

^ | Xor | x ^ y | Bits that are set in x or y but not both are set. |

~ | Not | ~x | Bits that are set in x are not set, and vice versa. |

<< | Shift left | x <<y | Shift the bits of x, y steps to the left |

>> | Shift right | x >>y | Shift the bits of x, y steps to the right. |

Example :

```
>>> x = 10
>>> y = 7
>>> print(x&y)
2
>>> print (x|y)
15
>>> print (~x)
-11
>>> print (x^y)
13
>>> print (x>>2)
2
>>> print (x<<2)
40
>>>
```

Pythonâ€™s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below

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

in | Evaluates to true if it finds a variable in the specified sequence and false otherwise. | x in y, here in results in a 1 if x is a member of sequence y. |

not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. | x not in y, here not in results in a 1 if x is not a member of sequence y. |

Example :

```
>>> x = 'Hello world'
>>> y = {1:'a',2:'b'}
>>> print('H' in x)
True
>>> print('hello' not in x)
True
>>> print(1 in y)
True
>>> print('a' in y)
False
>>>
```

Identity operators compare the memory locations of two objects. There are two Identity operators explained below :

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

is | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. | x is y, here is results in 1 if id(x) equals id(y). |

is not | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. | x is not y, here is not results in 1 if id(x) is not equal to id(y). |

Example :

```
>>> x1 = 9
>>> y1 = 9
>>> x2 = 'FTL'
>>> y2 = 'FTL'
>>> x3 = [1,2,3,4,5]
>>> y3 = [1,2,3,4,5]
>>> print(x1 is not y1)
False
>>> print(x2 is y2)
True
>>> print(x3 is y3)
False
>>>
```

Follow Us on Facebook

Follow Us on Google Plus