Chapter 4- Lists Flashcards

1
Q

Indexing list

A
>>> 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.'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A list is

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List of other lists

A
>>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
>>> spam[0]
['cat', 'bat']
>>> spam[0][1]
'bat'
>>> spam[1][4]
50
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Negative indexing

A

> > > 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.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Slicing lists

A
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0:4]
['cat', 'bat', 'rat', 'elephant']
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Len()

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

A

spam. len()

2spam. len()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Asiago g values within lists

A
>>> 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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Del()

A

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']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Cat naming programming

A

Go review

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Iterating over a list

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Multiple variable assignment 
What'd easier than 
>> cat = ['fat', 'orange', 'loud']
>>> size = cat[0]
>>> color = cat[1]
>>> disposition = cat[2]
A

> > > cat = [‘fat’, ‘orange’, ‘loud’]

|&raquo_space;> size, color, disposition = cat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Augmented variables

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Methods

A

Called on values

Similar to functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Index()

First appearance is returned

A
>>> 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Append()

A

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

Adds to the end of list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Insert()

A

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

17
Q

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

A

None

So you do not want to have them assigned as variables

18
Q

Remove()

A

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

19
Q

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

A

Del() when you know index

Remove() when you know value

20
Q

Sort()

A
>>> 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
Q

Keyword for sort()

A

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

22
Q

3 things about sort()

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