Python Python Sequences Sequence Operations Creating Slices

pthon sequence challenge

Question :

Create a slice of the provided student_gpas list that includes the 3rd through the 5th elements in the list. Assign the slice to a variable called sliced_gpas.

student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0] sliced_gpas=student_gpas[3:6]

I am getting the below error

Bummer: Whoops, looks like your slice contains incorrect values. Remember that slices are exclusive of the stop value.

Please let me know what was wrong in this code ?
student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]

sliced_gpas = student_gpas[3:6]

2 Answers

Sean Layton
Sean Layton
1,645 Points
sliced_gpas = student_gpas[2:5]
#remeber that python sequences start from 0 so when it asked for the third element it's 2. 
#ya its kinda weird but you get used to it
Steven Parker
Steven Parker
205,348 Points

It looks like you remembered about slice ranges being "exclusive of the stop value", but forgot that indexes in Python begin at 0 instead of 1.

So to slice the "3rd through the 5th elements", the index values would be 2 and 5:

sliced_gpas = student_gpas[2:5]