Chapter 4: Sequence types

List operators

face Josiah Wang

The operators + and * have been overloaded for lists.

So, you can concatenate two lists by ‘adding’ the list together. Try this and see what happens.

>>> print([1, 2] + [3, 4, 5])
???

You can also replicate elements in a list by ‘multiplying’ it with a scalar. Try this and see what happens.

>>> print([1, 2] * 3)
???

The membership operator (in) is also useful for checking whether a particular object is in a list. This returns a bool (True or False).

>>> print(3 in [1, 2, 3, 4])
True
>>> print(3 not in [1, 2, 3, 4])
False
>>> print("snake" in ["I", "am", "not", "afraid", "of", "Python"])
False