-8

I'm doing some practice tasks for school and theres one simple one to make a program that returns the last two digits of any integer over 9. I found the solution online and it uses 'abs()' which I haven't seen before. somehow it made 10 % 100 = 10 which I don't get when, to my understanding, abs(10) is literally just 10.

a = int(input())

print(abs(a) % 100)
j_yerbe
  • 111
  • 1
  • 9
  • 3
    Absolute value of 10 is 10. And 10 % (anthing > 10) = 10. It's how the modulo operator works. 10 divided by 100 = zero with a remainder of 10. 10 % 11 = 10. 10 % 10000 = 10. – Gillespie Mar 28 '19 at 17:49
  • `abs` returns the absolute value of its argument. So `abs(10) == abs(-10)` evaluates to `True`. – mypetlion Mar 28 '19 at 17:49
  • 1
    `%` is not division, if that's what you're thinking. – pault Mar 28 '19 at 17:50
  • 3
    The abs is just there for negative numbers, it is the % that matters. – Dave S Mar 28 '19 at 17:51

2 Answers2

0

The modulo operator % can be understood as "outputs the remainder after integer division".

Consider 232 % 100 = 32. After dividing by 100, you're left with 32. In other words, 232 = 100*2 + 32. Integer division gets you the 2, modulo gets you the 32.

Consider 32 % 100 = 32. 32 isn't divisible by 100, so the remainder is 32. In other words, 32 = 100*0 + 32. Integer division gets you the 0, modulo gets you the 32.

Since abs(10) = 10, and 10 % 100 = 10, then abs(10) % 100 = 10.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

The abs operator is the absolute operator. This means that positive values stay positive, while negative values will become positive values. For example:

    abs(10) = 10
    abs(-10) = 10
    abs(0) = 0

The percentage sign is the modulo operator. This means that the answer is the remainder after division. For example:

10 % 10 = 0 (as 10 can be divided by 10 without remainder)
10 % 5 = 0 (as 10 can be divided by 5 without remainder)
10 % 11 = 10 (as 10 cannot be divided by 11)

Therefore, 10 % 100 must be 10 and, thus, also abs(10) % 100 must be 10.

demasterr
  • 1
  • 1
  • 2