[Python] For-loop with the walrus operator inside the range

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    Why are the negative k values being printed as the original value, but when we enter the positive realm, it gets strange?

    In:

    def j(k):
    for i in range(i:=k):
    i+=i
    print(i)
    for i in [-5,-4,-3,-2,-1,0,1,2,3,4,5]:
    j(i)


    Out:

    -5
    -4
    -3
    -2
    -1
    0
    0
    0
    2
    4

    Continue reading...

Compartilhe esta Página