Chapter 4- Lists Flashcards Preview

Automate The Boring Stuff python > Chapter 4- Lists > Flashcards

Flashcards in Chapter 4- Lists Deck (22):
1

Indexing list

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0]
'cat'
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'
>>> ['cat', 'bat', 'rat', 'elephant'][3]
'elephant'
➊ >>> 'Hello ' + spam[0]
➋ 'Hello cat'
>>> 'The ' + spam[1] + ' ate the ' + spam[0] + '.'
'The bat ate the cat.'

2

A list is

list is a value that contains multiple values in an ordered sequence. The term list value refers to the list itself (which is a value that can be stored in a variable or passed to a function like any other value), not the values inside the list value.

Uses [ ]

Values inside the list are also called items. Items are separated with commas (that is, they are comma-delimited).

3

List of other lists



>>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
>>> spam[0]
['cat', 'bat']
>>> spam[0][1]
'bat'
>>> spam[1][4]
50

4

Negative indexing

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[-1]
'elephant'
>>> spam[-3]
'bat'
>>> 'The ' + spam[-1] + ' is afraid of the ' + spam[-3] + '.'
'The elephant is afraid of the bat.'

5

Slicing lists

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0:4]
['cat', 'bat', 'rat', 'elephant']
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']

6

Len()

What is length of
spam = ['cat', 'bat', 'rat', 'elephant']
2spam = [['cat', 'bat'], [10, 20, 30, 40, 50]

spam.len()
2spam.len()

7

Asiago g values within lists

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[1] = 'aardvark'
>>> spam
['cat', 'aardvark', 'rat', 'elephant']
>>> spam[2] = spam[1]
>>> spam
['cat', 'aardvark', 'aardvark', 'elephant']
>>> spam[-1] = 12345
>>> spam
['cat', 'aardvark', 'aardvark', 12345]

8

Del()

will delete values at an index in a list. All of the values in the list after the deleted value will be moved up one index

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']

9

Cat naming programming

Go review

10

Iterating over a list

>>> supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
>>> for i in range(len(supplies)):
print('Index ' + str(i) + ' in supplies is: ' + supplies[i])

11

Multiple variable assignment
What'd easier than
>> cat = ['fat', 'orange', 'loud']
>>> size = cat[0]
>>> color = cat[1]
>>> disposition = cat[2]

>>> cat = ['fat', 'orange', 'loud']
>>> size, color, disposition = cat

12

Augmented variables

spam += 1
spam = spam + 1

spam -= 1
spam = spam - 1

spam *= 1
spam = spam * 1

spam /= 1
spam = spam / 1

spam %= 1
Spam =spam%1

13

Methods

Called on values
Similar to functions

14

Index()
First appearance is returned



>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> spam.index('hello')
0
>>> spam.index('heyas')
3
>>> spam.index('howdy howdy howdy')
Traceback (most recent call last):
File "", line 1, in
spam.index('howdy howdy howdy')
ValueError: 'howdy howdy howdy' is not in list

15

Append()

>>> spam = ['cat', 'dog', 'bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']

Adds to the end of list

16

Insert()

>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']

17

Return value of insert(), append(),index()

None
So you do not want to have them assigned as variables

18

Remove()

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('bat')
>>> spam
['cat', 'rat', 'elephant']

19

When do you use del() and when do you use remove()

Del() when you know index
Remove() when you know value

20

Sort()

>>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> spam.sort()
>>> spam
['ants', 'badgers', 'cats', 'dogs', 'elephants']

21

Keyword for sort()

>>> spam.sort(reverse=True)
>>> spam
['elephants', 'dogs', 'cats', 'badgers', 'ants']

22

3 things about sort()

1.no return value
2.cant sort integers and strings
3.sorts in ASCII betical order so uppercase comes before lower