開始使用免費開始

方法(Methods)

有時候你需要把值加入 list,或更新 dictionary 裡的值。這裡你會看到 Python 的物件導向特性,這點和 R 不同。

在 Python 中,萬物皆為物件(例如 list 物件、dictionary 物件等)。物件都能執行特定的工作(例如將值加入 list、更新 dictionary 的值)。 你不是呼叫作用在物件上的函式,而是用點記號(dot notation),讓物件自己呼叫「方法(method)」來執行動作。

本練習屬於課程

給 R 使用者的 Python

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Append values to a list
person_list.____(____)

# Print person_list
print(person_list)

# Print the last element of person_list
print(____)
編輯並執行程式碼