Topic 1 - List and list slicing Flashcards

1
Q

What is a list in Python?

A

A list contains multiple values in an ordered sequence.

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

The values in lists are sometimes called _____

A

Items

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

Lists use what kind of brackets?

A

Square brackets [ ]

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

Provide an example of three values which are assigned to one variable.

A

pets = [ ‘dog’, ‘cat’ , ‘rabbit’ ]

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

How do you access specific values in a list?

A

You use the integer index value.

pets[0]
»»dog

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

True or False

  1. Lists can contain lists
A

True

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

What integer value would be used to access the value ‘bat’

pets = [ [‘cat’, ‘bat’], [10,20,20] ]

A

pets[0] [1]

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

What is a negative index used for in lists?

A

A negative index is used to start at the end of a list and work backwards.

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

pets = [‘rat’, ‘cat’, ‘dog’, ‘elephant’]

What value would this statement produce?

pets[-2]

A

dog

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

How many indexes does a slice have and what do they represent?

A

A slice has TWO indexes separated by a colon

These indexes represent the start and end index.

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

pets = [‘rat’, ‘cat’, ‘dog’, ‘elephant’]

What values would this slice produce?

pets [1:3}

A

‘cat’, ‘dog’

Explanation:
The slice would begin at index 1, go to index 3, BUT, not include index 3.

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

How can you update the value in a list?

A

You can assign a new value in a list by referencing the index value.

Example:
pets[0] = ‘bat’

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

How can you update multiple values in a list?

A

You use a slice to specify the starting index and the end index.

pets[1:3] = [‘rat’, ‘cat’, ‘dog’, ]

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

What does this slice shortcut do?

pets[:3] = [‘rat’, ‘cat’, ‘dog’, ]

A

Leaving a blank number is the same as entering ‘0’.

This tells python to start at the first index.

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

How would you delete the value of ‘rat’ from the list

pets = [‘rat’, ‘cat’, ‘dog’, ‘elephant’]

A

del pets[0]

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

What operators are used to determine if something is present in a list of not?

A
  1. in

2. not in

17
Q

What would this statement evaluate to?

‘howdy’ in [‘hello’, ‘hi’, ‘howdy’, ‘hey’]

A

It would evaluate to True

18
Q

What would this statement evaluate to?

‘howdy’ not in [‘hello’, ‘hi’, ‘howdy’, ‘hey’]

A

It would evaluate to False