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:

图片[1]-Python's arithmetic operations and its order of precedence - 拾光赋-拾光赋
Here, 1 and 1 are operands, and “+” is the operator computing addition.

Multiplication:

图片[2]-Python's arithmetic operations and its order of precedence - 拾光赋-拾光赋
Here, 1 and 3 are operands, and “*” is the operator computing multiplication in python. We cannot use “x” or “X” for this command.

Division:

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:

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.

图片[3]-Python's arithmetic operations and its order of precedence - 拾光赋-拾光赋
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

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:
图片[4]-Python's arithmetic operations and its order of precedence - 拾光赋-拾光赋
We can see that 4 divide by 2 yields quotient 2 and remainder 0. So the number is even.


Order of Precedence:

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

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

图片[5]-Python's arithmetic operations and its order of precedence - 拾光赋-拾光赋
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.

原文链接:Python’s arithmetic operations and its order of precedence

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
With the wonder of your love, the sun above always shines.
拥有你美丽的爱情,太阳就永远明媚
评论 抢沙发

请登录后发表评论

    暂无评论内容