# Basic operations – Python programming

By | November 22, 2018

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,

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.

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:

Dividing any number by zero will raise ZeroDivisionError in python.

#### More operations

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.

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

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 operandsare not equal. > Condition becomes true if left operand is greaterthan right operand. < Condition becomes true if left operand is smallerthan right operand. >= Condition becomes true if left operand is greaterthan or equal to right operand. <= Condition becomes true if left operand is less thanright operand.

#### Logical operators

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

#### 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

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

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 arepointing the same object. is not Outputs true if variables on either sides of the operatorare not pointing the same object.

#### 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 🙂