Sequence Operations
Python supports a variety of operations that can be applied to sequence types, including strings, lists, and tuples.
Sequences in General
Sequences are containers with items accessible by indexing or
slicing, as we’ll discuss shortly. The built-in
len function takes a container as an argument and
returns the number of items in the container. The built-in
min and max functions take one
argument, a non-empty sequence (or other iterable) whose items are
comparable, and they return the smallest and largest items in the
sequence, respectively. You can also call min and
max with multiple arguments, in which case they
return the smallest and largest arguments,
respectively.
Coercion and conversions
There
is no implicit coercion between different sequence types except that
normal strings are coerced to Unicode strings if needed. Conversion
to strings is covered in detail in Chapter 9. You
can call the built-in tuple and
list functions with a single argument (a sequence
or other iterable) to get an instance of the type
you’re calling, with the same items in the same
order as in the argument.
Concatenation
You can concatenate sequences of
the same type with the + operator. You can also
multiply any sequence S by an integer
n with the * operator.
The result of
S
*
n
or
n
*
S
is the concatenation of n copies of
S. If n is zero
or less than zero, the result is an empty sequence of the same type
as S.
Sequence membership
The x
in
S operator tests to see whether object ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access