DEV Community

Cover image for Python's arithmetic operations and its order of precedence
Mehreen Mallick Fiona
Mehreen Mallick Fiona

Posted on • Updated on

Python's arithmetic operations and its order of precedence

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. Integer (int):
    Positive or negative whole numbers (without a fractional part).
    For example: 10,5000000,-35

  2. Floating-point (float):
    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:

Addition:

Image description
Here, 1 and 1 are operands, and "+" is the operator computing addition.

Multiplication:

Image description
Here, 1 and 3 are operands, and "*" is the operator computing multiplication in python. We cannot use "x" or "X" for this command.

Division:

Image description

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.

Exponentiation:

Image description

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.

Floor Division:

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

Modulus:

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
We can see that 4 divide by 2 yields quotient 2 and remainder 0. So the number is even.

Image description


🟡 Order of Precedence:

After learning all the arithmetic operations, we need to know the order of precedence of them. i.e. PEMDAS

Image description

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

Image description
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.

Top comments (0)