3.2. Operations On Sequences¶
Next, we will look at how to interpret expressions involving sequences and various operators.
3.2.1. Concatenation and Repetition¶
In general, you cannot perform mathematical operations on lists. Interestingly,
the +
and *
operator does work with lists, but for lists, the +
operator concatenates lists and the *
operator repeats the items in
a list a given number of times. Concatenation means joining the two
operands by linking them end-to-end. For example:
In [1]: fruit = ["apple", "orange", "banana", "cherry"]
In [2]: [1, 2] + [3, 4]
Out[2]: [1, 2, 3, 4]
In [3]: fruit + [6, 7, 8, 9]