Operating on sequence objects

The following operators work on lists and tuples. Most of these operators also work on other data types:

• Addition (+): Concatenates the contents of lists together to form a new list, or concatenates the contents of tuples together to form a new tuple. (You can't concatenate a list and a tuple.)

• Sequence multiplication (*): Repeats the contents of a list or a tuple.

• Augmented assignment (+= and *=): Simultaneously concatenates and assigns or repeats and assigns. The statement mylist += newlist is the same as mylist = mylist + newlist. The former works slightly faster and is easier to read.

• Comparison (< >, ==, !=): Tests whether the contents of two or more lists or two or more tuples are the same.

• Contents test (in): The keyword in determines whether a list or tuple contains the given element and returns True or False, as shown here:

>>> phone sounds :

= ('beep', 'ring', 'flight of the bumblebee')

>>> y = 'beep' in

phone sounds

>>> y


Was this article helpful?

0 0

Post a comment