sequence
=== about sequence https://artofproblemsolving.com/wiki/index.php/Sequence_(Python) An Sequence example:In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important.
Strings are a special type of sequence that can only store characters, and they have a special notation. However, all of the sequence operations described below can also be used on strings.
Tuples are like lists, but they are immutable - they can't be changed.
Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted.
Sequences have the following characteristics:
- they can be indexed and sliced, from left to right index starts from 0; from right to left, they start with negative index -1. Slice of seq, seq[start:end], end is not included.
- The same type sequence can be concatenated, means
+
and*
signs work on them like instrings
. - len(seq) returns the length of a sequence, that is, the number of elements in the sequence.
x in seq
returnsTrue
ifx
is an element ofseq
.
So far you have seen two types of sequential collections: strings, which are made up of characters; and lists, which are made up of elements of any type. One of the differences we noted is that the elements of a list can be modified, but the characters in a string cannot. In other words, strings are immutable and lists are mutable.