Indexing elements of sequences
numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six']
print(numbers[4])
#
# four
print(numbers[2:5])
#
# ['two', 'three', 'four']
print(numbers[-2])
#
# five
print(numbers[-3:-1])
#
# ['four', 'five']
print(numbers[-3:])
#
# ['four', 'five', 'six']
print(numbers[5:])
#
# ['five', 'six']
print(numbers[1:6:2])
#
# ['one', 'three', 'five']
print(numbers[:])
#
# ['zero', 'one', 'two', 'three', 'four', 'five', 'six']
Sequence unpacking
Sequence unpacking assigns the value of each element in a sequence to a
variable.
seq = ['A', 2, 'three']
x, y, z = seq
print(x) # A
print(y) # 2
print(z) # three
The number of variables must correspond to the number of elements in the sequence, otherwise, the Python interpreter raises either a ValueError
exception with the explanatory message too many values to unpack or not enough values to unpack.