X Modulo Y In Python. The modulo operator is used to carry out euclidean division. How to calculate the modulo y in python?
X = (x // y) * y + (x % y). If the result is nonzero and y is of different sign then y is added to the result. Python modulo operator is used for formatting strings i.e for embedding values of other variables and data types in strings.
Let us look at a few examples to understand this better.
If you do 17 % 5, you will get 2 as the answer. In most languages, both operands of this modulo operator have to be an integer. However, if the first operand is 0, then the result of modulo operation will always be zero. We can create a small function to print out the full result of euclidean division like so:
Nessun commento:
Posta un commento