In today's article, we will learn about the dictionary in python. In this article, I will explain What a python dictionary is. And explain how to use python dictionary. Okay, without further ado, let’s get into the article.
What is a python dictionary?
Dictionary is another mutable container model and can store any type of object.
Each key value of the dictionary key=>value pair is separated by a colon : , each key value pair is separated by a comma,, and the entire dictionary includes In curly braces {}, the format is as follows:
d = {key1 : value1, key2 : value2 }
The key is generally unique. If the last key-value pair is repeated, the previous one will be replaced, and the value does not need to be unique.
>>>dict = {'a': 1, 'b': 2, 'b': '3'}; >>> dict['b'] '3' >>> dict {'a': 1, 'b': '3'}
The value can be of any data type, but the key must be immutable, such as string, number or tuple.
How to use python dictionary?
A simple dictionary example:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
You can also create a dictionary like this:
dict1 = { 'abc': 456 }; dict2 = { 'abc': 123, 98.6: 37 };
The above is all about this article, dictionary in python . I hope what I said and the examples I gave can be helpful to you.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of What is a python dictionary? How to create and use python dictionary?. For more information, please follow other related articles on the PHP Chinese website!