-
SequenceDynamicPL/Python 2019. 10. 26. 16:26
1. Definition
In Math: $S=x_{1},x_{2},x_{3},\cdots $
Note The sequence of indices: 1, 2, 3, ...
We can refer to any item in the sequence by using its index number S[2]
So we have a concept of the first element, the second element, and so on
Python lists have a concept of positional order, but set do not
- A list is a sequence type
- A set is not a sequence type
2. Built-In Sequence Types
2.1 Mutable
- lists
- bytearrays
2.2 Immutable
- strings
- tuples: in reality, a tuple is more than just a sequence type
- range: more limited than lists, strings, and tuples
- bytes
2.3 Additional Standard Type
2.3.1 collections package
- namedtuple
- deque
2.3.2 array module
- array
3. Homogeneous vs Heterogeneous Sequences
3.1 homogeneous
- each elements is of the same type(a character)
- 'python'
- Strings
3.2 heterogeneous
- each element may be a different type
- [1,10.5,'python']
- lists
Homogeneous sequence types usually more efficient in storage wise at least. e.g. prefer using a string of characters, rather than a list or tuple of characters.
4. Features
Sequences are a very common type of iterable. So any sequence type is iterable. But an iterable is not necessarily a sequence type. iterables are more general. Some examples of built-in sequence types are lists, strings, and tuples.
numbers = [10, 12, 15, 18, 20] fruits = ("apple", "pineapple", "blueberry") message = "I love Python ❤️"
They support efficient element access using integer indices via the __getitem()__ special method (indexing) and define a __length()__ method that returns the length of the sequence.
Also, we can use the slicing technique on them.
# Slicing the sequences print(numbers[:2]) print(fruits[1:]) print(message[2:])
5. Methods
6. References
'DynamicPL > Python' 카테고리의 다른 글
First-Class Object and High-Order function (0) 2019.11.02 Slice (0) 2019.10.28 List, Dictionary, Set Comprehensions, and Difference between List Comprehensions and Generator expressions (0) 2019.10.22 iterator, iterable, iteration, iterator protocol, generator functions, generator expressions, and lazy evaluation (0) 2019.10.22 Asterisk(*) of Python (0) 2019.10.18