Mutable หรือ Immutable?
ฟังก์ชันด้านล่างนี้เพิ่ม mapping ระหว่าง string กับเวอร์ชันตัวพิมพ์เล็กของ string นั้นลงใน dictionary คิดว่าค่าของ d และ s จะเป็นเท่าไรหลังจากเรียกใช้ฟังก์ชันนี้?
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
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำจริง
เปลี่ยนทฤษฎีให้เป็นการลงมือทำด้วยแบบฝึกหัดเชิงโต้ตอบหนึ่งในของเรา
เริ่มแบบฝึกหัด