Sequence
- Sequence
- iterable objects visited through index
__len__()
len()
Sequence's basic operation
Sequence objects have a method named __getitem__(self, key)
, so it can be visited by s[i]
.
1 | b = b'ABCDEFG' |
slice
The basic form of slice is s[i:j]
and s[i:j:k]
. slice
object is used to save index information of slice, such as slice(start, stop, step)
. slice
object has attributes such as '.start', '.stop' and '.step', method such as 'indice(len)' which return a tuple.
1 | s[::-1] ==> 'fedcba' |
1 | s1 + s2 |
Judge a object wehther or not exist in sequence s
.
1 | x in s |
1 | sorted(iterable, key = None, reverse = False) |
1 | len() |
1 | all(iterable) |
sequence unpacking
1 | a, b = (1, 2) |
tuple variable *
assign a number of variable to a tuple variable
1 | first, *middles, last = range(10) |
temporary variable_
obtain partial data
1 | _, b, _ = (1, 2, 3) |
tuple
define a tuple
x1, [x2, ..., xn]
(x1, [x2, ..., xn])
tuple()
tuple(iterable)
(1,)
list
define a list
[x1, [x2, ..., xn]]
list()
list(iterable)
basic operations
del s[index]
s[i:j] = x
dd s[i:j]
, equal ass[i:j] = []
s[i:j] = []
list's methods
s.append(x)
s.clear()
s.copy()
s.extend(t)
s.insert(i, x)
s.pop()
s.remove(x)
s.reverse()
s.sort()
list comprehension
1 | [i**2 for i in range(10)] |
string
1 | ord('A') ==> unicode |
1 | str.strip() |
1 | test, find and replace |
1 | str.split() ==> list |
1 | str.maketrans() |
1 | b.decode(encoding, errors) |
NOT SUGGESTING
1 | string % (values) |
1 | '%(lang)s has %(num)03d quote types.' % {'lang':Python, 'num':2} |
bytes and bytearray
- bytes
- bytearray
- memoryview
1 | b = s.encode() |