開始使用免費開始

Mutable 或 immutable?

以下函式會在字典中新增一個對應,將某個字串與其小寫版本配對。呼叫函式之後,你預期 ds 的值會是什麼?

def store_lower(_dict, _string):
  """Add a mapping between `_string` and a lowercased version of `_string` to `_dict`

  Args:
    _dict (dict): The dictionary to update.
    _string (str): The string to add.
  """
  orig_string = _string
  _string = _string.lower()
  _dict[orig_string] = _string

d = {}
s = 'Hello'

store_lower(d, s)

本練習屬於課程

Python 函式寫作

檢視課程

動手互動練習

將理論付諸實踐,立即體驗我們的互動練習

開始練習