Python -- Slicing Flashcards

1
Q

How to know how many values are in a list?

A

len() function

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

names = [‘Ran’, ‘Shinyan’, ‘Emilee’, ‘Amy’]
How to access ‘Shinyan’ in the list?

A

print(names[1])

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

names = [‘Ran’, ‘Shinyan’, ‘Emilee’, ‘Amy’]
How to access ‘Amy’ in the list using 2 ways?

A

Method 1: print(names[3])
Method 2: print(names[-1])

Because 0 is the first value, if we count backward, ‘Amy’ will be our -1 value.

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

What happens when you try to access an index that doesn’t exist?

A

You get an IndexError

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

names = [‘Ran’, ‘Shinyan’, ‘Emilee’, ‘Amy’]
print(names[0:2])

What does this code evaluate?

A

Execute: [‘Ran’, ‘Shinyan’]
Up to but not including index number 2.

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

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[-7: -2])

What does this code evaluate?

A

Execute:[3, 4, 5, 6, 7]
Up to but not including -2

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

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[ : ])

What does this code evaluate?

A

Execute: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

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

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tell it to print in increments of 2

A

print(my_list[0:-1:2])
Execute: [0, 2, 4, 6, 8]

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