分享至
不好意思 想請問以下python code:
countries = ["USA", "Canada", "India"]countries[0], countries[1] = countries[1], countries[0]print(countries)Output:['Canada', 'USA', 'India']
想請問output為什麼是這樣?
謝謝!
已邀請的邦友 {{ invite_list.length }}/5
試試看觀察以下的
a = 1 b = 2 a, b = b, a a b
因為Python有提供語法糖,無需使用暫存變數,就能做變數內容的交換.
countries = ["USA", "Canada", "India"] countries[0], countries[1] = countries[1], countries[0]
就是把 "USA", "Canada" 做交換.
語法糖
https://zh.wikipedia.org/wiki/%E8%AF%AD%E6%B3%95%E7%B3%96
IT邦幫忙