Python Tutorials - Herong's Tutorial Examples - v2.15, by Herong Yang
Data Type - 'dict' for Dictionary Table
This section describes the 'dict' data type, which stores a collection of key-value pairs. 'list' data objects are mutable.
What Is the "dict" Data Type? "dict" is the mutable dictionary data type in Python. Each element in a "dict" object is a key-value pair, where the key refers to a hashable object, and the value refers an arbitrary object.
"dict" data type is also called hash table or associated array in other programming languages.
"dict" data type has the following main Features.
1. "dict" data objects can be created in several ways:
2. "dict" data type provides an array-style expression to access the element of a given key.
>>> x = {'foo': 100, 'bar': 200} >>> type(x) <class 'dict'> >>> e = x['foo'] >>> e 100 >>> type(e) <class 'int'>
3. "dict" data type is a mutable data type. Once a "dict" data object is created to store key-value pairs, Its content can be changed: key-value pairs can be added, removed and/or updated.
>>> x = {'foo': 100, 'bar': 200} >>> id(x) 4457650624 >>> x['new'] = 300 >>> x {'foo': 100, 'bar': 200, 'new': 300} >>> del x['bar'] >>> x {'foo': 100, 'new': 300} >>> x['foo'] = 101 >>> x {'foo': 101, 'new': 300} >>> id(x) 4457650624
4. "dict" objects support the following operations:
Syntax Operation Note ---------- ------------- ----------------------- x | y Merge Creates a new "dict" x |= y Merge Updates x k in x Look up k is an object k not in x Look up Same as !(k in x) x[k] = o Update Update pair del x[k] Delete Delete pair
5. Some built-in functions are provided for "dict" objects.
6. Some instance/class methods are provided for "dict" objects.
Table of Contents
Common Features of All Data Types
Data Type - NoneType for Nothing
Data Type - 'bool' for Boolean Values
Data Type - 'int' for Integer Values
Data Type - 'float' for Real Numbers
Data Type - 'bytes' for Byte Sequence
Data Type - 'str' for Character String
Data Type - 'tuple' for Immutable List
Data Type - 'list' for Mutable List
Data Type - 'set' for Unordered Collection
►Data Type - 'dict' for Dictionary Table
Variables, Operations and Expressions
Function Statement and Function Call
List, Set and Dictionary Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager