A sequence in Python is an ordered group of values that can be represented by a single variable. We can address individual elements or subgroups of a sequence with square brackets and possibly a range of indices. Square brackets hold the index or range.
A A[2:5] A[i] A[1:j+k+1]
Python is zero based (the first element is numbered 0) and the upper bound of any range is always noninclusive. Python defines several intrinsic sequences: strings, Unicode strings, lists, tuples, and a few others that we will not cover.
An iterator is a data type that can be traversed in order. Sequences either are directly iterators or can be readily converted to iterators.
x in Sreturns True or False if x is or is not an element of the sequence S.
S1 is S2returns True or False if S1 and S2 are exactly the same or different. “Exactly the same” is quite rigid in Python so check documentation for the behavior of
iswith different objects.
iscan be negated.
S1 is not S2;
A not in B.
- Range extraction
iland goes to
ul. The colon is here called a range operator.
- Starting from the end
S[-1]is the last element,
S[-2]the next to last, and so forth.
S1*Nreplicates the sequence
Examine the results of the following:
A=[1.,2,3.,4.,5,6] 1 in A 1. in A 9 in A 9 not in A
Remember that 1 and 1. are different types.
A B=A[:] C=A B is A C is A B is not A