# Python Operators

Operators are specials symbols which perform certain arithmetic or logical operation on operands. Consider an example, 1+2 =3, here, 1 and 2 are called operands and + (plus) is the operator.

The operators can be categorized into 7 different category in python:

1. Arithmetic operator

2. Comparison ( or Relational) operator

3. Assignment operator

4. Logical operator

5. Bitwise operator

6. Membership operator

7. Identity operator.

### Arithmetic operators

Arithmetic operators are used for performing arithmetic operations between two operands. It includes +(addition), – (subtraction), * (multiplication), / (division), % (reminder), // (floor division), ** ( exponent operators).

### Comparison operator

Comparison operators are used to compare two values and return boolean value( either True or False) based on the comparison. It includes == (equals to), != (not equals to ) , < (less than), > (greater than), <= (less than or equal to), >= (greater than or equals to).

### Assignment operator

Assignment operator is used to assign the the value of right expression to left operand. It includes,  = (equals), += (plus equals), -=(minus equals) , *= (multiply equals), /= (divide equals), %= ( remainder equals), **= (exponential value equals), //= (floor division equals). The left operand has to be a variable. It can not be a numeric value.For example a = 4, is correct, but 3 = 4.5 is incorrect and results in error.

### Logical operator

Logical operators are used to evaluate two expression. These operators are used mostly in decision making.

It includes and, or and not operator.

We have just compared the boolean values we can also compare two numeric values, and numeric values with and expression or string.

Number vs Number comparison

We might not use this type of scenario in day to day life, but it is worth wondering what happens when we compare two number with and and or operator .

For these comparison let’s open up python shell by typing python in command prompt or terminal. These concepts are tested in python version 3.7.

In the case of and operator, if we compare two numbers it will always return second number. In case of or operator it returns the first number.

For example

``````Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)]
on win32
>>> 3 and 4
4
>>> 44 and 3
3
>>> 3+5j and 4
4
>>> 4 and 3+5j
(3+5j)
>>> 3 or 4
3
>>> 44 or 3
44
``````

Here in first two cases of and the second operand value(i.e. 4, 3 and (3+5j)) is returned, and in case of or first operand value is returned.

Number vs String comparison

1. and operator : It will always print the string value in case of and operator.
2. or operator: it return the first value(whatever it is) in case of or operator
Example:

``````>>> 3 and 'hello'
'hello'
>>> 'hello' and 3
3
>>> 3 or 'hello'
3
>>> 'hello' or  3
'hello'
``````

Boolean value vs print statements

• and operator: if first statement is true it will execute second statement, if first is not true second statement is not executed.
• or operator : If the first statement is false it will print second statement, else if the first is True it will not execute second statement.
``````>>> False and print("hello world !")
False
>>> True and print("hello world !")
hello world !
>>> False or  print("hello world !")
hello world !
>>> True or  print("hello world !")
True
``````

You might be wondering where can I use it. This kind of statement can be used to perform conditional rendering.

For example

``````>>> True and print("hello world !")
hello world !``````

This statement will only execute if first statement is True, in other words, this is equivalent to,

``````>>> if (True):
...     print("hello world !")
...
hello world !
``````

In case of True statement we can have some expression evaluating to True.

### Bitwise operator

Bitwise operator performs bit by bit operation on the values of operands. To understand this we need to be able to think in terms of binary numbers.

It includes & (binary and), | (binary or) , ^ (binary xor), ~ (negation) , << (left shift), >> (right shift).

Example:

To test this let’s open up python shell.

In python binary is represented by starting zero followed by ‘b’ by ‘0b…’. For example. 0b111 = 7, 0b110 = 6. To convert number to binary we can use bin(number) e.g bin(7) , results in 0b111. To get number value of binary just type the binary in shell it will return equivalent numeric value. Now we have the basics lets do the operation.

``````Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
>>> bin(6)
'0b110'
>>> bin(7)
'0b111'
>>> 6 & 7
6
>>> 0b110 & 0b111
6
>>> 6 | 7
7
>>> 0b110 | 0b111
7
>>> 6 ^ 7
1
>>> 0b110 ^ 0b111
1
>>> ~ 6
-7
>>> ~ 0b110
-7
>>> 6 << 2
24
>>> 0b110 << 2
24
>>> 6 >> 2
1
>>> 0b110 >>  2
1
``````

### Membership operator

These operator are used to check whether a value exist in sequence data type(such as list, tuple, and sets). It includes in and not in operator

For example

``````>>> list_values = [1,2,3,3]
>>> 2 in list_values
True
>>> 55 in list_values
False
>>> 33 not in list_values
True
>>> 2 not in list_values
False
>>> tuple_values = (1,2,3,4)
>>> 2 in tuple_values
True
>>> set_values = {1,2,3}
>>> 2 in set_values
True
``````

### Identity operator

This operator is used to compare whether both values are identical or not. It compares values based on id() of the value. In python each literals and variables have unique id assigned to them.

It includes is and is not

For example:

Note we can find id of a value using id() function. For example id(3), id(3.0).

``````>>> a = 'hi'
>>> b = 'hi'
>>> a is b
True
>>> c = '3'
>>> d = 3
>>> c is d
False
>>> 3 is 3
True
>>> 3 is 3.0
False
>>> id(3)
1888838784
>>> id(3.0)
18930928
>>> c is not d
True
``````

Difference between == (equals to ) and is  :

The difference between is and == is that == compares value of two operand and is compares id() of two values. As value 3 and 3.0 is save but they have different id so 3 == 3.0  returns True and 3 is 3.0 returns false.

For example,

``````>>> id (3)
1888838784
>>> id(3.0)
18930928
>>> 3 == 3.0
True
>>> 3 is 3.0
False
``````

### Operator precedence

We should know in case of multiple operator which operator will be evaluated at first. Following is the operator precedence table.