Python -- Slicing Flashcards
How to know how many values are in a list?
len() function
names = [‘Ran’, ‘Shinyan’, ‘Emilee’, ‘Amy’]
How to access ‘Shinyan’ in the list?
print(names[1])
names = [‘Ran’, ‘Shinyan’, ‘Emilee’, ‘Amy’]
How to access ‘Amy’ in the list using 2 ways?
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.
What happens when you try to access an index that doesn’t exist?
You get an IndexError
names = [‘Ran’, ‘Shinyan’, ‘Emilee’, ‘Amy’]
print(names[0:2])
What does this code evaluate?
Execute: [‘Ran’, ‘Shinyan’]
Up to but not including index number 2.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[-7: -2])
What does this code evaluate?
Execute:[3, 4, 5, 6, 7]
Up to but not including -2
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[ : ])
What does this code evaluate?
Execute: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tell it to print in increments of 2
print(my_list[0:-1:2])
Execute: [0, 2, 4, 6, 8]