DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

Lists are similar to strings in that each is a set of items
indexed by Python that you can interact with and slice
and dice. To pull out values, you just use the variable
name with brackets and the index number, which starts
at 0, as in this example:


>>> print(kids[1])
Sydney

Figure 3-4 shows a list from the perspective of the index.


Figure 3-4 List Index


Unlike strings, lists are mutable objects, which means
you can change parts of the list at will. With a string, you
can’t change parts of the string without creating a new
string. This is not the case with lists, where you have a
number of ways to make changes. If you have a
misspelling, for example, you can change just one
element of the list, leaving the rest untouched, as in this
example:


Click here to view code image


>>> kids
['Caleb', 'Sidney', 'Savannah']
>>> kids[1]="Sydney"
>>> kids
['Caleb', 'Sydney', 'Savannah']
>>>

You can concatenate lists as well by using the + operator
to join two lists together. The list items do not need to be

Free download pdf