Hi, Happy learning for everyone! Could anyone please explain the code below? `c . . .

Rasul Hamidli:
Hi, Happy learning for everyone! Could anyone please explain the code below?

countries=["USA", "Canada", "India", "Turkey"]
countries[0],countries[1]=countries[1], countries[0] I specially can not understand how this line works

print(countries)
ANSWER: ['Canada', 'USA', 'India']

Andrei Andriushin:
Hi! you forgot Turkey)
Check out this link: https://stackoverflow.com/a/41615799

Alistair Mackay:
In answer to the question raise by @Rasul Hamidli

countries=["USA", "Canada", "India", "Turkey"]

is a list containing four elements. At the start countries[0] is “USA” and countries[1] is “Canada”

countries[0],countries[1]=countries[1], countries[0]

is a “swap” assignment. It is replacing the value of countries[0] with the value of countries[1] and at the same time swapping the other way.

so USA becomes Canada and Canada becomes USA

result: ['Canada', 'USA', 'India', 'Turkey']

Rasul Hamidli:
Oh, nice catch @Andrei Andriushin :slightly_smiling_face: To be honest for testing purposes I did it with Turkey several times:smile: It looks I am sleepy a bit

Rasul Hamidli:
Thank you so much @Alistair Mackay!