112 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
Try typing the following into the interactive shell:
animals = ['aardvark', 'anteater', 'antelope', 'albert']
len(animals)
4
We’ve used the in operator to tell us if a string exists inside another string value. The in
operator also works for checking if a value exists in a list. Try typing the following into the
interactive shell:
animals = ['aardvark', 'anteater', 'antelope', 'albert']
'anteater' in animals
True
'anteater' not in animals
False
'anteat' in animals
False
'delicious spam' in animals
False
Just like how a set of quotes next to each other represents the blank string value, a set of brackets
next to each other represents a blank list. Try typing the following into the interactive shell:
animals = []
len(animals)
0
List Concatenation and Replication with the + and * Operators..............................................................................
Just like how the + and * operators can concatenate and replicate strings, the same operators can
concatenate and replicate lists. Try typing the following into the interactive shell:
['hello'] + ['world']
['hello', 'world']
['hello'] * 5
['hello', 'hello', 'hello', 'hello', 'hello']
That’s enough about the similarities between strings and lists. Just remember that most things you
can do with string values will also work with list values.