+
usually means addition, but when used on strings or lists, it means “concatenate”. Given that, what do you think the multiplication operator *
does on lists? In particular, what will be the output of the following code?
counts = [2, 4, 6, 8, 10]
repeats = counts * 2
print(repeats)
[2, 4, 6, 8, 10, 2, 4, 6, 8, 10]
[4, 8, 12, 16, 20]
[[2, 4, 6, 8, 10],[2, 4, 6, 8, 10]]
[2, 4, 6, 8, 10, 4, 8, 12, 16, 20]
The technical term for this is operator overloading: a single operator, like +
or *
, can do different things depending on what it’s applied to.
*
used on a list replicates elements of the list and concatenates them together:
[2, 4, 6, 8, 10, 2, 4, 6, 8, 10]
counts + counts