Python's arithmetic operations and its order of precedence
Tags: python, beginners, tutorial, programming
Published: 2022-11-14 16:45:44

## Intro:
Guido van Rossum created 'Python' which is a high-level and object oriented programming language in 1991. With emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code and almost like human readable language.
That's how the journey of learning the language of snake starts!

---

## 🔵 Numbers:
There are various data types in Python. Today we will only talk about **Numbers** and how to do basic math in Python. We know that there are **two** types of basic numbers in arithmetic. They are as follow:

1. <u>Integer (int):</u>
 Positive or negative whole numbers (without a fractional part).
For example: 10,5000000,-35

2. <u>Floating-point (float):</u>
 Any real numbers with “decimal” points or floating-point representation.
For example: -5.678,190.90

And with either of these numbers you can perform basic arithmetic as you would expect. 

---

## 🟣Arithmetic Operations:


## <u>Addition:</u>
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9czvkc00zhclh2wobsy.png)
Here, 1 and 1 are operands, and "+" is the operator computing addition. 


## <u>Multiplication:</u>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sxttbd2zom2e1nth5y1.png)
Here, 1 and 3 are operands, and "*" is the operator computing multiplication in python. We cannot use "x" or "X" for this command.


## <u>Division:</u>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnhcwosicn9rndqv8d8t.png)

Here, 1 and 2 are operands, and "/" is the operator computing multiplication in python. We cannot use **÷** for this command. Division of numbers always yields a **float.**

## <u>Exponentiation:</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95b21e1t2nrmqbmwl2ub.png)

Basically it's the power operator. Here, 2 and 4 are operands, and "**" is the operator computing exponents in python. 

---


🟢Now we will introduce to two more exciting arithmetic operations that we will use regularly in Python. i.e. **floor division** and **modulus.**

## <u>**Floor Division:**</u>


In this arithmetic operation, the double-backslash operator (//) is the floor division operator.
Floor division of integers results in an integer. If one of the operands is float, then the floor division results in a float.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5chcqq8tzs1hoz9rbfn8.png)
Here we can see, 7 divided by 5 yields 1.4 . But while using the floor division (//), it basically discards the part after the decimal point and returns a whole number by flooring the value to the nearest integer number. i.e. 1 
                 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lm6anrpnsj1fopnyv5u.jpg)




## <u>**Modulus:**</u>


In this arithmetic operation, the percentage sign (%) is the modulus operator  and it basically returns the remainder after division. **It works as a brilliant checker for finding odd or even number. **
To check whether the number is odd or even, we have to **mod** by 2 and if the remainder yields zero then its a even number. Otherwise, the number is odd.
**For example:**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5hyt2ye22t1fv68bdoxg.png)
We can see that 4 divide by 2 yields quotient 2 and remainder 0. So the number is even. 
            
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swrpvoa89y1xhhfs03iz.jpg)

---



##🟡 Order of Precedence:

After learning all the arithmetic operations, we need to know the order of precedence of them.<u> i.e. PEMDAS 
</u>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dxt73yb9ndcabemh3y8.png)

 We know that Python executes from <u>top to bottom</u> and reads code from **left** to **right**. Now, let us see a compound expression below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0rf3p081tb20txzsajk.png)
**Here, according to PEMDAS, operations in brackets should be carried out first.** After that, from left to right we can see that next in line is multiplication and then division. Lastly, addition is done.

That's all for today! Feel free to ask me any question if you don't understand.