According to the documentation module random
, random.choice
returns one of the elements of the list, chosen randomly. In its first code, this return was placed in the variable a
(that is, it contains only one of the elements of the list). But the list itself is not modified, random.choice
only takes one of the elements and returns.
The strange thing is that if I use the random.choice()
without putting it into the variable a
, does not work, it returns the entire list.
If you do not assign the return of random.choice()
a variable, its value is lost:
# retorna um dos elementos da lista, mas este não é atribuído a nenhuma variável
random.choice(alunos)
print(alunos) # imprime a lista, sem modificações
That is, the function returned one of the elements, but where was it stored? Nowhere. And how random.choice
does not modify the list, it remains unchanged (actually the function does not "return the entire list", what happens is that the list is not modified in this case).
Already random.shuffle
shuffles the list by modifying it. That’s why you don’t need a variable to store the result, because the list itself is being modified within the function.
Inclusive, random.shuffle
returns None
:
print(random.shuffle(alunos)) # imprime "None"
So it doesn’t even make sense to assign your return to a variable.
Anyway, there is this difference in behavior precisely because each one does a different thing.