Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Basic Math Operators

Welcome to our Mathematical Operators lecture.

As we have learned how to work with numbers(integers and floats), we can start to learn mathematical operators by using
them.

Let's start with Addition.

► Addition (+):
Addition operator performs just as they do in mathematics.

In [1]: a = 2
b = 3
a + b

Out[1]: 5

In [2]: x = 1.6
y = 3.7
z = 6.5
x + y + z

Out[2]: 11.8

► Substraction (-) :
Calculates difference of x and y as in math.

In [3]: a = 7
b = 8
c = 11
a - b - c

Out[3]: -12

In [4]: a = 4
b = 7
a - b

Out[4]: -3

In [5]: x = 1.4
y = 4.7
x - y

Out[5]: -3.3000000000000003
► Multiplication (*):
Calculates multiplication of x and y.

In [6]: a = 3
b = 8
a * b

Out[6]: 24

In [7]: i = 1.14
j = 3.46
i * j

Out[7]: 3.9443999999999995

In [8]: a = 2
b = 3
c = 5
a * b * c

Out[8]: 30

In [9]: a = 6
b = 7.2
a * b

Out[9]: 43.2

► Division (/):
Calculates division of x by y.

In [10]: 4 / 2

Out[10]: 2.0

In [11]: 9 / 6

Out[11]: 1.5

In [12]: 22 / 9

Out[12]: 2.4444444444444446

► Floor Division (//) :


The division of operands where the result is the quotient in which the digits after the decimal point are removed.

In [13]: 4 // 2

Out[13]: 2

In [14]: # 13'ün 4 ile bölümünden kalan bölüm sonucu 3tür.


13 // 4

Out[14]: 3

In [15]: 22 // 7

Out[15]: 3
In [16]: 40 // 7

Out[16]: 5

► Modulo (%)
The % operator is the modulo, which returns the remainder rather than the quotient after division.

This is useful for finding even, odd numbers or numbers that are multiples of the same number.

In [17]: # 1 is the remainder of division of 10 by 3


10 % 3

Out[17]: 1

In [18]: 5 % 2

Out[18]: 1

In [19]: 222 % 111

Out[19]: 0

► Power (**)
This operator in Python is used to raise the number on the left to the power of the exponent of the right. That is, in the
expression 5 ** 3, 5 is being raised to the 3rd power. In mathematics, we often see this expression rendered as 5³, and
what is really going on is 5 is being multiplied by itself 3 times.

In Python, we would get the same result of 125 by running either expression 5 ** 3, or 5 * 5 * 5.

In [20]: #5 ^ 3
5 ** 3

Out[20]: 125

In [21]: 2 ** 8

Out[21]: 256

In [22]: 3 ** 4

Out[22]: 81

So, what if we want to square root of a number. how can we do that? The answer is simple, as you know from math
lessons, the square root of a number is equal to power of 1/2 (0.5) of that number.

Let's try make an example for that:

In [23]: # Square root of


36 ** 0.5

Out[23]: 6.0

► Negation (-)
Sometimes, we need to change signs of our number. For example, let's say that we have positive numbers, and we want
to convert into them negative numbers or vice versa. In such situations, we can use this operator practically.
In [24]: num = 3
-num

Out[24]: -3

In [25]: x = -5
-x

Out[25]: 5

► Operator Precedence
In Python, as in mathematics, we need to keep in mind that operators will be evaluated in order of precedence.

1) First of all parantheses have the highest precedence.

2) Multiplication and Dividing are always done before Addition and Subtraction.

3) Operations are evaluated not from left to right or right to left.

These are the main features of Operator Precedence in Python above.

But, there is a full-list of operator precende in Python following lines:

Important Tip: If you may get confused in some operations, in order to guarantee for your calculation's accuracy,

you should use parentheses in that part of your operations.

Priority Operation

1 Parentheses

2 Exponent

3 Multiplication

4 Division

5 Addition

6 Subtraction

In [26]: 2 + 3 * 4 / 5 - 20

Out[26]: -15.6

In [27]: #Use parentheses


2 + ((3 * 4) /5) - 20

Out[27]: -15.6

We can use math operators in Python programming this way.

That's all for this lesson!

Thanks for your partipication.

See you in our next lessons!

In [ ]:

You might also like