Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

พจนานุกรม


A dictionary เป็นประเภทข้อมูลที่คล้ายกับ arrays แต่ใช้คีย์และค่าแทนที่จะใช้ดัชนี แต่ละค่าที่เก็บไว้ใน dictionary สามารถเข้าถึงได้โดยใช้คีย์ซึ่งสามารถเป็นวัตถุประเภทใดก็ได้ (เช่น สายอักขระ ตัวเลข รายการ ฯลฯ) แทนที่จะใช้ดัชนีในการเข้าถึง

ตัวอย่างเช่น ฐานข้อมูลหมายเลขโทรศัพท์สามารถเก็บไว้โดยใช้ dictionary แบบนี้:

หรือ dictionary สามารถกำหนดค่าเริ่มต้นด้วยค่าที่มีอยู่ในโน๊ตเทชันดังนี้:

การทำซ้ำใน dictionary

dictionary สามารถทำซ้ำได้เหมือนกับรายการ แต่ dictionary ต่างจากรายการตรงที่ไม่ได้จัดเก็บค่าในลำดับที่แน่นอน ในการทำซ้ำผ่านคู่คีย์และค่า ให้ใช้ไวยากรณ์ดังนี้:

การลบค่า

ในการลบค่าที่ระบุ สามารถใช้ได้หนึ่งในโน๊ตเทชันต่อไปนี้:

หรือ:

Exercise

เพิ่ม "Jake" ลงใน phonebook พร้อมหมายเลขโทรศัพท์ 938273443 และลบ Jill ออกจาก phonebook.

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy