Game Development Reference
In-Depth Information
9 - Hangman
When joining lists, this is known as list concatenation . Try typing [1, 2, 3, 4]
+ ['apples', 'oranges'] + ['Alice', 'Bob'] into the shell:
>>> [1, 2, 3, 4] + ['apples', 'oranges'] +
['Alice', 'Bob']
[1, 2, 3, 4, 'apples', 'oranges', 'Alice', 'Bob']
>>>
Notice that lists do not have to store values of the same data types. The example above
has a list with both integers and strings in it.
The in Operator
The in operator makes it easy to see if a value is inside a list or not. Expressions that use
the in operator return a boolean value: True if the value is in the list and False if the
value is not in the list. Try typing 'antelope' in animals into the shell:
>>> animals = ['aardvark', 'anteater', 'antelope',
'albert']
>>> 'antelope' in animals
True
>>>
The expression 'antelope' in animals returns True because the string
'antelope' can be found in the list, animals . (It is located at index 2.)
But if we type the expression 'ant' in animals , this will return False because
the string 'ant' does not exist in the list. We can try the expression 'ant' in
['beetle', 'wasp', 'ant'] , and see that it will return True .
>>> animals = ['aardvark', 'anteater', 'antelope',
'albert']
>>> 'antelope' in animals
True
>>> 'ant' in animals
False
>>> 'ant' in ['beetle', 'wasp', 'ant']
True
>>>
The in operator also works for strings as well as lists. You can check if one string exists
in another the same way you can check if a value exists in a list. Try typing 'hello' in
Search WWH ::




Custom Search