A.5 Lists

As in Scheme, but unlike in ML and Haskell, lists in Python are heterogeneous, meaning all elements of the list need not be of the same type. For example, the list [2,2.1, "hello"] in Python is heterogeneous while the list [2, 3, 4] in Haskell is homogeneous. Like ML and Haskell, Python is type safe. However, Python is dynamically typed, unlike ML and Haskell. The semantics of [] is the empty list. Tuples (Section A.6) are more appropriate to store unordered items of different types. Lists in Python are indexed using zero-based indexing. The + is the append operator that accepts two lists and appends them to each other.

Examples:

A set of 15 code lines in Python consisting of a list.
Description
Continuation of the code in Python consisting of a list with 19 lines.
Description

Pattern matching. Python supports a form of pattern matching with lists:

A set of 23 code lines in Python for pattern matching.
Description

Lists in Python vis-à-vis lists in Lisp. There is not a direct analog of the cons operator in Python. The append list operator + can be used to simulate cons, but its time complexity is O(n). For instance,

A list with four items showing the analog of the cons operator.
Description

Examples:

A set of six code lines in Python consisting of the Python analog in M L.
Description
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.254