Featured Post

How To Use Python Arithmetic Operators

How To Use Python Arithmetic Operators





Introduction


Arithmetic Operators are used in Python to carry out mathematical operations on numerical values. It is very important to understand this operator well in order to complete Python projects that require you to do carry out mathematical calculations. In this guide, you will come across all the different types of arithmetic operators that will be explained in an easy-to-understanad manner.

Types of Python Arithmetic Operators


There are six types of arithmetic operators in Python and in this guide, I will be explaining every one of them with detailed explanation as well as with simple example exercises, for your easy understanding! So, keep reading this post until the very end , so that you do not miss out any valid points.

The 6 types of arithmetic operators are : 

1. Addition Operator 
2. Subtraction Operator  
3. Multiplication Operator 
4. Division Operator
5. Modulus Operator 
6. Exponential Operator

Addition Operator


The addition operator , denoted by the symbol,  "+" , helps you to add different numerical values. Below is provided a simple addition exercise, to show you how addition is carried on using Python code. 

Code  
num1 = 7
num2 = 4
#addition 
solution = num1 + num2
print(solution)



Subtraction Operator 


The Subtraction Operator is denoted by the symbol, "-", and it is used for subtracting one numerical value from the other.
In the below example, we have two variables num1 and num2 with values 7 and 4 respectively and we create a variable called solution and assign the value of subtracting num2 from num1. Then print the solution.  We get the output as 3.

Code 
num1 = 7
num2 = 4
#Subtraction
solution = num2 - num1
print(solution)



Multiplication Operator  


The Python Multiplication Operator is denoted by using the " * "  symbol and it can be used to multiply different numerical values.

The below example shows num1 multiplied by num2  to get the output of 28 as shown below :

Code
num1 = 7
num2 = 4

#Multiplication

solution = num1 * num2
print(solution)

#output is 28

If one of the numbers is a negative number, then the output is also a negative number as shown in the example below:

Code
num1 = -7
num2 = 4

solution = num1 * num2
print(solution)

#output is -28




Division Operator 


The Division Operator is shown by using the symbol "/ " , in the below example, we divide num1 by num2 

You will notice that the quotient of the Division is always a float even if both the numbers or operands are integers.
In the first case, when num1 and num2 are respectively 8 and 4, and we assign the resultant value of num1 divided by num2 to a variable  solution, and then print the solution,  we get the output as 2.0 which is a float, instead of integer 2

In the second example code, you will get a quotient of 1.0 , not 1 as is expected by Division of integers.

In the third example, by dividing -12 by 2, we get the output as -6.0

Hence we observe that division of two integers always yield a resultant quotient which is a float and not an integer! In case, any of the divident or divisor is a negative number , we will get a resultant quotient that is a negative float as shown in the code below.


Code 
num1 = 8
num2 = 4
solution = num1/num2
print(solution)

#output 2.0

num1 = 10
num2 = 10
solution =num2/num1
print(solution)

#output 1.0

num1 = -12
num2 = 2
solution = num1/num2
print(solution)

#output -6.0







Division Operator (Floor Division)


In floor division, we get resultant quotient as an integer, however , if any of the operands is a negative number,  then, the resultant quotient will be a decimal number, and, it will be floored.

Let's consider two numbers, 10 and 3. In the first example case, we divide 10 by 3 and as we know, for floor division, we use  double forward slashes as shown below. The output will be 3 since the resultant quotient be  3 and not 3.3 as you get in integer division, because, here, in float operations, the result ignores the numbers to the right of the decimal place, in order to give the resultant quotient as an integer.

In the second example, below, we divide -10 by 3 and the resultant quotient is -4

Now, you must be wondering why the same numbers 10 and 3 give a different value in the second example? This is due to the fact that as part of the operation rule here, when one of the operands is a negative number, that is, -10 in this case,  and when we divide the negative number -10 by 3, the resultant quotient value of 3.33(float), gets rounded or floored to  the integer 4. So, for negative operands, the resultant quotient will be floored if the result is a float. 

In the third case, when we divide -10 by 2, the resultant quotient is an integer with a negative sign.

In the fourth case, when we floor divide -10.0 which is a negative float,  divided by 3, we get a resultant quotient as -4.0, since the resultant float  -3.33 gets rounded or floored to - 4 and then we get the output as -4.0 which is a negative float since 10.0 is a float.


Hence, when one of the operands is a negative float, then, the resultant quotient is a negative float and gets floored as well if the divident is not fully divisible by the divisor as is in the case of -10.0 / 3 and we get the output as -4.0



 Code


print(10//3)
#output #3

print(-10//3)
#output -4

print(-10//2)
#output -5

print(-10.0//3)
#output -4.0



Modulus Operator 


The Modulus Operator will be denoted by the percentage sign "%" and this operation will hrlp to yield a remainder when the two operands are divided.  
When 9 is divided by 2, we get a remainder of 1 which is displayed as the output in the console.
When one of the operands is a negative number, that is, -9 divide by 2, then also the output is 1 . The remainder value doesn’t get affected by negative numbers. 



Code  
#num1 = 9
#num1 = 2
#sol = num1 % num2
#print(sol)

print(9%2) #output 1 print(9%3) #output 0 print(-9%2) #output 1






Exponential Operator



Exponential Operator  is indicated by the  double asterisk symbol, "**" and is used to raise a number exponentially. A number can be raised to the power of 2, 3, 4 etc.
In the below code, when 4 is raised to the power 2, then, the result or output in the console is 16 . Raised to the power 2 means the number is multiplied by itself two times, that is, 4 * 4 = 16 is the same as 4 ** 2

This operation is also called squaring a number and in this case, squaring the number 4 that yields 16 in the console output.
When you raise the same number 4 to the power of 3, it gives a result of 64   (Multiplication of 4 three times yields 64)


Code 
num1 = 4
num2 = 2
sol = num1 ** num2
print(sol)
#16
print(4 *** 3)
#64




Order of Precedence

For performing Modulus, Multiplication and Division operations, the order of movement or associativity we follow is usually from left-to-right and for exponentiation operations, we follow movement from right-to-left.
For performing arithmetic operation, we normally follow the BODMAS rule of precedence , that is, there is a certain order of operation that is to be followed while doing any arithmetic operation. Let us see more in detail here.

Following the order of precedence of BODMAS:
1) Brackets
2)Order of exponents, roots
3)Division
4)Multiplication
5)Addition
6)Subtraction



Let us consider an example to solve a mathematical operation by solving it using the BODMAS order of precedence of operation :

Consider the arithmetic operationthat we are going to solve manually without using Python  :

 (8x3/8+1)x3

Perform operation inside the bracket by doing the multiplication of 8 x 3
(24/8+1)x3

Perform division of 24/8 first inside the bracket as division to be done before addition
(3+1)x3

Perform addition inside the bracket adding 3 and 1
4x3

Multiply the remaining numbers 4 and 3
12


Next we perform the same operation of the same expression by using Python :

a = (8*3/8+1)*3

We assign the above expression to a variable a

Next we print(a)

print(a)

Let us see the result if it is the same as what we got by doing arithmetic manually by following the BODMAS rule!

a = (8*3/8+1)*3
we get the result as 12.0



Quiz


(9 x 3  ÷  9 + 1) x 3

(27  ÷  9 + 1) x 3

(3 + 1) x 3

4  x 3

solution = 12

In the above calculation, I highlighted the parts where we are performing the operations in order of precedence so that you understand how the BODMAS rule is being observed in the operation. As you can see above we are moving from left to right in the calculation process. First solve what is in the bracket while moving from left to right and also consider the operations to be done first in the bracket.

Now as part of the quiz that I solved it for you manually following the BODMAS principles as shown above, I want you to solve the same expression using the Python coding. If you are unable to solve it using the python code, do let me know in the comments section below the post. If you are able to solve it using Python, also, comment in the comment section so that I get to know if you were able to do the same. Remember to use the python operator symbols while solving the above problem in your code editor or else it will show you a bug in your python console. 

Remember that you need to replace the operators' signs to the python operators' signs not the arithmetic signs. For example, for multiplication, you need to use the python multiplication operator sign * and for division, you need to use the python operator / and for exponents you need to use the python operator ** 

This will be a good practice for you 


Conclusion


We have learnt all there is to be learnt in Python arithmetic operations for performing addition, subtraction, division, multiplication, exponentiation etc. We also learnt the order of precedence, that is the order in which we move in a direction from left to right to perform the calculation , usually  following the BODMAS rule of mathematics. We also learnt that we can get the same solution by doing the same calculation using our python code editor, only that we need to replace the arithmetic operator symbols with the python arithmetic operator symbols.



More posts for you to read on Python Programming for Beginners:









 

Comments

Popular Posts

Build A Random Quote Machine in React

Build A Calculator in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Guide to Learn Coding Efficiently and Effectively