60 likes | 197 Vues
Discover the fundamentals of Python dictionaries, a powerful data structure that allows data retrieval using keys instead of indices. Learn how to create dictionaries, such as mapping students to their courses, and understand key characteristics like immutability and uniqueness of keys. Explore important dictionary methods including clear(), get(), items(), keys(), and values(), which enhance data management in Python programming. This guide is tailored for beginners and will provide you with essential knowledge for working with dictionaries efficiently.
E N D
Dictionaries • Allow you to retrieve data using a key (instead of an index). • Classic dictionary: • Search/retrieve using a word • Get the definition of the word • Python dictionary: • Retrieve using a key • Get data associated with the key
Creating a dictionary • students = {‘Joe’:[CPSC231,MATH271],’Katie:[PSYC205,ENGL201]} • dict = {}
Adding and Retrieving Values • students[‘Bob’] = [CPSC231,PHIL279] • x = students[‘Joe’]
Keys • Type must be immutable • Strings • Integers • Tuples • etc • Keys in a dictionary must be unique
Methods • clear • get • items • keys • values