In this blog, we will learn about basic operations and operators in python programming.

Python is very easy to learn and to use. You can write mathematical operations right into the console. It gives instant output. Like any other languages, there are several types of operators in python to perform different operations.

### Types of Operators in Python

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

#### Arithmetic operators

Type into python console,

1 2 3 4 |
>>> 2 + 2 4 >>> 9 * 5 45 |

I have found this functionality very useful. It makes debugging super easy. In deep learning, we have to deal with matrix operations. You can print inline a matrix and check elements.

Let’s come back to basic operations. Similarly, division and subtraction can be done in python.

1 2 3 4 5 6 7 8 9 10 11 12 |
>>> 4 / 2 2.0 >>> 4 // 2 2 >>> 3 / 2 1.5 >>> 3 // 2 1 >>> 5 - 3 2 >> 2 * (3 + 2) 10 |

You may have noticed, the division operation in python3 gives a float number.

**A single slash division(‘/’) operation in python outputs float. Whereas a double slash(‘//’) division outputs integer.**

In Python 2, there is integer division. The code below is in Python 2. If you want to use Python 3 like float division in Python 2 then check the code below:

1 2 3 4 5 6 7 8 9 10 |
Python 2.7.10 (default, Aug 17 2018, 17:41:52) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 2/3 0 >>> 4/2 2 >>> from __future__ import division >>> 4/2 2.0 |

Dividing any number by zero will raise ZeroDivisionError in python.

1 2 3 4 |
>>> 5/0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero |

#### More operations

1 2 3 4 |
>>> 25 % 4 1 >>> 3 ** 3 27 |

To perform modulus operation use % sign and for exponents(power operation) use ** sign.

#### Assignment operators

Assigns value of the right operand to the left operand. We can use ‘=’ operator to assign values to variables.

1 2 |
>>> b = 10 >>> c = b + 2 |

To perform short hand operations, the arithmetic operator is written next to left operant before ‘=’ sign. Some examples are shown below:

1 2 |
>>> a += 10 >>> b *= 9 |

Above expressions are similar to a = a + 10 and b = b * 9.

#### Comparison operators

== | Condition becomes true if both the operands are equal. |

!= | Condition becomes true if both the operands are not equal. |

> | Condition becomes true if left operand is greater than right operand. |

< | Condition becomes true if left operand is smaller than right operand. |

>= | Condition becomes true if left operand is greater than or equal to right operand. |

<= | Condition becomes true if left operand is less than right operand. |

#### Logical operators

Logical operators are used to perform logical AND, OR and NOT operations. Example of using logical operators:

1 2 3 4 5 6 7 8 |
>>> a = True >>> b = False >>> a and b False >>> a or b True >>> not (a and b) True |

#### Membership operators

Membership operators of Python checks membership in a sequence like lists, strings or tuples. There are two membership operators as below:

in | Becomes true if a variable value is found in a sequence. |

not in | Becomes true if a variable value in not found in a sequence |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
>>> a_list = [1,2,3,4,5] >>> a = 3 >>> b = 8 >>> if a in a_list: ... print("Yuppiee") ... Yuppiee >>> if b in a_list: ... print("Yuppiee") ... else: ... print("nope") ... nope >>> if b not in a_list: ... print("There's no b") ... There's no b >>> basket=["banana","apple","orange"] >>> if "banana" in basket: ... print("Yes") ... Yes >>> if "chocolate" in basket: ... print("Yes") ... else: ... print("No") ... No |

#### Bitwise operators

Bitwise operators perform operation on bits. For example, a = 12 and b = 15. Then, 12 in binary is 1100 and 15 in binary is 1111.

Let’s try bitwise AND operation. So, a & b will be;

1100

1111

——–

1100

which equals to 12 in decimal.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>>> a=12 >>> b=15 >>> a & b 12 >>> a | b 15 >>> a ^ b 3 >>> ~a -13 >>> a << 2 48 >>> a >> 2 3 |

Similarly, bitwise OR (‘ | ‘), XOR ( ‘ ^ ‘ ), binary 1’s compliment ( ‘ ~ ‘ ), left shift operator ( ‘<<‘ ) and right shift operator ( ‘>>’ ) are shown in the above code example.

#### Identity operators

Identity operators compare memory locations of two objects. Python in-built function id() returns a unique number as identity of object.

There are two identity operators:

is | Outputs true if variables on either sides of the operator are pointing the same object. |

is not | Outputs true if variables on either sides of the operator are not pointing the same object. |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
>>> id(a) 4304939744 >>> id(b) 4304939840 >>> a = b >>> id(a) 4304939840 >>> id(b) 4304939840 >>> a is b True >>> a is c False >>> a is not c True >>> c is not a True >>> a is not b False |

#### Summary

In this blog, we learnt about basic operations in python using different types of operators. It was really interesting to learn about operators like identity and membership. Please check the syntax notes in this blog. Also, keep revisiting the blog to revise these concepts.

I hope you enjoyed learning as much as I enjoyed writing it 🙂