  Introduction to Python Arithmetic Operator

Python Arithmetic Operator – Python supports various arithmetic operators or mathematical operators like + , – , / , = , < , > etc. and used to compute arithmetic solution.

Python has built-in function operator and it divides operator in different types –

1. Arithmetic operators

2. Comparison operators

3. Logical operators

4. Assignment operators

5. Identity operators

6. Membership operators

7. Bitwise operators

Let’s begin with operators one by one –

1. Arithmetic operators – It is used to find out mathematical solution like addition, subtraction, multiplication, etc.

Mathematical operators are follows as –

addition (+) – It returns addition of two variables (operands) like x and y or unary plus.

Unary Plus – Addition operator (+) in Python can be used as unary plus. It refers copy of original operand (variable) with its value and perform new operator’s action in it.

Example 1 – Using + operator as unary plus.

```x = 2
x+=4

print('Sum of  4 + 2 is:' ,  x)```

Explanation – Defined a variable with value 2. In the next step, additionally added value 2 with the existing value i.e., 4 in the equation x+=2 (means, x = 4+2) and returned value 6 as sum of 4+6.

Example 2- Addition of two variables.

```x = 2
y = 1
z = x+y

print(z)```

Example 3- Addition of two variables with numerical value.

```x = 2
y = 1
z = x+y+3

print(z)``` As it is shown clearly that it added variable value together and as well as a value and returned 6 as sum.

– (subtraction) – It returns subtraction of two variables (operands) like x and y or unary minus.

Unary Subtraction – Subtract operator (-) in Python can be used as unary subtraction. It refers copy of original operand (variable) with its value and perform new operator’s action in it.

Example 1 – Using – operator as unary subtraction.

```x = 4
x-=2

print('Sum of  4 - 2 is:' ,  x)```

Explanation – Defined a variable with value 4. In the next step, additionally subtracted value 2 with the existing value i.e., 4 in the equation x-=2 (means, x = 4-2) and returned value 2 as subtraction of 4-2.

Example 2- Subtraction of two variables.

```x = 2
y = 1
z = x-y

print(z)                   ```

Example 3- Subtraction of two variables with numerical value.

```x = 5
y = 1
z = x-y-2

print(z)``` As it is shown clearly that it subtracted variable value together and as well as a value and returned 2.

* (Multiplication) – It returns multiplication of two operands like x and y or unary multiply.

Unary Multiplication – Multiplication operator (*) in Python can be used as unary multiplication. It refers copy of original operand (variable) with its value and perform new operator’s action in it.

Example 1 – Using * operator as unary multiplication.

```x = 4
x*=2

print('Sum of  4 * 2 is:'  ,  x)```

Explanation – Defined a variable with value 4. In the next step, additionally multiplied value 2 with the existing value i.e., 4 in the equation x*=2 (means, x = 4*2) and returned value 8 as multiplication of 4*2.

Example 2- Multiplication of two variables.

```x = 2
y = 1
z = x*y

print(z)                      ```

Example 3- Multiplication of two variables with numerical value.

```x = 5
y = 1
z = x*y*3

print(z)          ``` As it is shown clearly that it multiplied variable value together and as well as a value and returned 15.

/ (Divide) – It returns division of left operand by right one. One symbol of division (/) always returns answer in float ( answer in decimal ).

Unary Division – Division operator (/) in Python can be used as unary division. It refers copy of original operand (variable) with its value and perform new operator’s action in it.

Example 1 – Using / operator as unary Divide.

```x = 4
x/=2

print('Sum of  4 / 2 is:' ,  x)```

Explanation – Defined a variable with value 4. In the next step, additionally divided value 2 with the existing value i.e., 4 in the equation x/=2 (means, x = 4/2) and returned value 2.0 as division of 4/2.

Example 2- Division of two variables.

```x = 4
y = 2
z = x/y

print(z)```

Example 3- Division of two variables with numerical value and returns answer in decimal.

```x = 4
y = 2
z = x/y/2

print(z)``` As it is shown clearly that it divided both variable together as well as divided a value and returned 1.0.

% (Module) – It returns the remainder of two numerical values left after division.

Example 1- Division of left variable by right variable and returns remainder.

```x = 4
y = 2
z = x%y

print(z)``` As it is shown clearly that 4 is divided by 2 and later returned reminder i.e., 0.

// (Division Floor) – It returns division of left operand by right one. Double symbol of division (//) always returns answer into whole number adjusted (answer without decimal).

Unary Floor Division – Floor Division operator (//) in Python can be used as unary division. It refers copy of original operand (variable) with its value and perform new operator’s action in it.

Example 1 – Using // operator as unary Divide.

```x = 4
x//=2

print('Sum of  4 // 2 is:' ,  x)```

ExplanationDefined a variable with value 4. In the next step, additionally divided value 2 with the existing value i.e., 4 in the equation x//=2 (means, x = 4//2) and returned value 2 as division of 4//2.

Example 2- Division of left variable by right one and returns answer in division floor.

```x = 4
y = 2
z = x//y

print(z)```

** (Exponent) – It returns left operand power, raised to the power of right. In other words, left value will be multiplied by the number of times of the right value.

Unary Exponent – Floor Division operator (**) in Python can be used as unary exponent. It refers copy of original operand (variable) with its value and perform new operator’s action in it.

Example 1 – Using ** operator as unary Exponent.

```x = 4
x**=2

print('Sum of  4 ** 2 is:' ,  x)```

Explanation – Defined a variable with value 4. In the next step, additionally exponent value 2 with the existing value i.e., 4 in the equation x**=2 (means, x = 4*1 = 4*4 = 16) and returned value 16 as exponent of 4**2.

Example 2- It returns raised value of left operand.

```x = 2
y = 3
z = x**y

print(z)```

Explanation – Defined two variables with value 2 and 3 respectively. In the next step, multiplied left operand value 2 with the number of times of left operand i.e., 3 in the equation z = x**y (means, x = 2*1 = 2*2 = 4*2 = 8) and returned value 8 as exponent of 2**3.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

Share on