Lists
Now that we've spent some time learning about Python's capabilities for working with strings, let's have a little fun with lists.
Before repeating our trick with dir()
to see what methods and attributes we have to work with, let's learn some syntax specific to lists.
First, we define a new list through assignment:
>>> breakfast = ['outmeal', 'eggs', 'toast']
Let's try some of our usual tricks:
>>> breakfast
[oatmeal, 'eggs', 'toast']
>>> type(breakfast)
<class 'list'>
Let's try accessing a specific item within the list using an approach called "slicing." (Notice the square brackets.)
>>> breakfast[0]
oatmeal
>>> breakfast[2]
'toast'
Now let's try our dir()
function on our
breakfastlist, just as we did for our
omelas` string. Take some time to explore how the various methods work. Here are some of the most immediately useful:
'append',
'clear',
'count',
'extend',
'insert',
'remove',
'reverse',
'sort'
Making an Archive
Open VS Code and enter a list of books as strings in a list.
for loop
search in the archive
ask for the search term from the user
What are semantics?