0
What does this % does in python. And what does | does in python.?
2 Réponses
+ 3
% returns a remainder of a division.
a % b
It could in theory subtract or add b from a infinity times and returns the smallest possible natural integer.
Example:
17 % 5
subtracts 5 from 17 3 times, 17 - 5 - 5 - 5 = 2, if you subtracted 1*5 more: 2 - 5 = -3, result would be negative, and negative results are not wanted.
This is a way to imagine it, but not the way how computer does it.
Computer could do it as:
17 - (17 // 5) * 5 = 2
| is an or operator for bits.
9 | 4 = 13
Because in binary:
9 = 1001, 4 = 0101, 13 = 1101
Those binary sequences perform an or operation for each bit:
(1 or 0, 0 or 1, 0 or 0, 1 or 1) = 1, 1, 0, 1 = 1101 = 13