Thursday, May 21, 2020

Arithmetic Operators – Part I


An operator is a symbol of the programming language, which is able to operate on the values.

Exponentiation

Double Asterisk (**) sign is the exponentiation (power) operator.


Pure text editors don't accept 23, in Python uses ** instead.







When both arguments are integers, the result is an integer. When at least one argument is a float, the result is a float too.









Multiplication

Asterisk (*) sign is the multiplication operator.










Division

Slash (/) sign is the divisional operator.












The result produced by the division operator is always a float. Actually, this is a problem. It happens sometimes that we really need a division that provides an integer value, not a float.

But, Python has solution for this problem.


Integer Division (Floor Division)


Double slash (//) sign is the integer divisional operator. It differs from the standard operator (/) in two ways.

  1. The results are always rounded.
  2. It conforms to the integer vs. float rule.













Let’s do some experiments.








What we get is two ones - one integer and one float.

The result of integer division is always rounded to the nearest integer value that is less than the real (not rounded) result.

In Python, rounding always goes to the lesser integer.


But in here, 








This is ok.


Hence, in below codes some of the values are negative. This will too affect to the result. The real (not rounded) result is -1.5 in both cases.








The rounding goes toward the lesser integer value.

No comments:

Post a Comment