Python Tutorials - Herong's Tutorial Examples - v2.15, by Herong Yang
Data Type - 'list' for Mutable List
This section describes the 'list' data type, which stores a sequence of references to data objects of any data types. 'list' data objects are mutable.
What Is the "list" Data Type? "list" is the mutable list data type in Python. Each element in a "list" object stores a reference to another data object.
"list" data type has the following main Features.
1. "list" data objects can be created in several ways:
Here are some examples on how to create "tuple" objects:
>>> [9, 1, 1] [9, 1, 1] >>> ['nine', 'one', 'one'] ['nine', 'one', 'one'] # elements are of different data types >>> ['nine', b'one', 1] ['nine', b'one', 1] >>> list((9, 1, 1)) [9, 1, 1] # a "str" object is a sequence of multiple characters >>> list('911') ['9', '1', '1'] # ('911') is a single "str" object >>> list(('911')) ['9', '1', '1'] # ('911',) is a "tuple" object >>> list(('911',)) ['911']
2. "list" data type provides an array-style expression to access the element of a given position.
>>> x = ['nine', b'one', 1] >>> type(x) <class 'list'> >>> e = x[0] >>> e 'nine' >>> type(e) <class 'str'>
3. "list" data type is a mutable data type. Once a "list" data object is created to store a sequence of references, this sequence can be changed: elements can be added, removed and/or updated.
>>> x = ['nine', b'one', 1] >>> id(x) 4459207552 >>> x.append('help') >>> x ['nine', b'one', 1, 'help'] >>> x.remove(1) >>> x ['nine', b'one', 'help'] >>> x[0] = 'ten' >>> x ['ten', b'one', 'help'] >>> id(x) 4459207552
4. "list" objects support the following operations:
Syntax Operation Note ---------- ------------- ----------------------- x + y Concatenation Creates a new "list" x += y Append Updates x x * n Repetition n is an "int" n * x Repetition Same as x * n x *= n Repetition Updates x by repetition s in x Look up s is an object s not in x Look up Same as !(s in x) x[i] = o Update del x[i] Delete
5. Some built-in functions are provided for "list" objects.
6. Some instance methods are provided for "list" 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