Page 43 -
P. 43
meet python
Create simple Python lists
Let’s start with the following simple list of movie titles and work up from
there:
The Holy Grail
The Life of Brian
The Meaning of Life A short list of some
Monty Python movies
Here’s the same list written in a way that Python understands:
movies = ["The Holy Grail",
"The Life of Brian",
"The Meaning of Life"]
To turn the human-friendly list into a Python-friendly one, follow this four-
step process:
1 Convert each of the names into strings by surrounding the data with quotes.
2 Separate each of the list items from the next with a comma.
3 Surround the list of items with opening and closing square brackets.
4 Assign the list to an identifier (movies in the preceding code) using the
assignment operator (=).
It’s perfectly OK to put your list creation code all on one line, assuming, of
course, that you have room:
movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life"]
This works, too.
you are here 4 7