FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 16
PAGE 1 / 5
Dictionaries in Python
Home >>> Free Online Lessons, Python 3

What is a Dictionary?

In Python, a dictionary (data type dict) is an unordered, indexed, and mutable collection of data that contains any number of key-value pairs (obviously, the key is unique):



Below is an initial example of a dictionary:
Editor - lesson_16.py
       
Console/Output done
DETAILS

For each key word on the left, we have associated a value (an antonym) on the right. We use the colon character ":" to make the association, and to separate pairs, the comma character ",". All are written within curly braces, similar to a data set.

The length of the dictionary is not 6, but of course 3, which is the number of key (pairs) we have entered.

A dictionary is not ordered with actual indexes, but access is done using the key words from the pairs. Thus, we write antonyms['tall'] to get the value 'short'.

If we try to write antonyms[1], we will get an interpretation error:



Moreover, if a key word is not found in the data structure, an exception/error will be displayed:



So, the index (key) was not found.
Proceed to the next page.
 home   list  CONTENTS   perm_identity   arrow_upward