Allocation of values other than normal

Asked

Viewed 71 times

3

def testes(x, y):
    while y:
        x, y = y, x % y

    return x

I don’t understand this part of the code, what happens with the 'x' and the 'y' on the left side of the equals sign and what the 'y' does on the right side. This line 3 is confusing to me.

2 answers

3


You’re seeing Python’s ability to do multiple assignments.

You know the simple assignment and it is easy to understand. to the left of the assignment signal = there is the name of a variable and on the right side, after that operator, there is an expression that will generate a value that will be assigned to the variable.

In this case you have more than one variable on the left side, so you must have in one way or another the generation of more than one value. It may be that a single expression generates different values or it may be multiple expressions.

In the specific case it has two variables and two expressions. Then the first expression will be the value assigned to the first variable, while the second is assigned to the second variable.

This is a curious example because it still does something we call swap. It exchanges values at the same time. So when executing the value of y is now assigned to x and the value of y becomes a calculation of the rest of x divided by y. Obviously the value of y used in the first assignment is the value before changing right away. And the value of x used in the second expression is the value x previous, not what made the previous assignment.

It can be written that way too:

def testes(x, y):
    while y:
        temp = x
        x = y
        y = temp % y
    return x

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Interesting. So, all values on the right side are calculated before any assignment.

  • Exactly. Python values short codes.

  • Another detail is that while y: means that as y is not zero the loop will not stop running. In this case, with each iteration the variable y receive a lower value until zero.

  • def tests(x, y): while y: x, y = y, x % y Return x # in this case on line 3. x = y and y = x % y so x will show the result of x % y that’s it?

  • @Tiadodev will have a process there, but in the end it will end up being the result of the penultimate rest. Now you can vote on everything on the site also.

0

That line could be broken in two, that way:

def testes(x, y):
    while y:
        x = y
        y = x % y

    return x

That way, while y is different from 0 (condition for the while stop), the value of x will be changed to y and the value of y will be the same as the rest of the x for y.

  • in this case, the x being divided by y will have the same value of y? getting y % y ?

  • @Thiadodev In this case, it was more a representation to demonstrate how that line was being assigned. But if the assignment were done separately, an auxiliary variable would be required. When doing everything in a single line, the value of x and y (after the =) are still the values "original".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.