# Explain the output of each of the following

squares=[0,1,4,9,16,25,36,49,64,81] print(squares[-1:-4:-2]) print(squares[-1:-4:2]) print(squares[-1:4:2]) print(squares[1:4:-2]) print(squares[-1:4:-2])

1/15/2017 11:51:48 AM

Pragya Priya4 Answers

New AnswerRemember negative indexes refer to the going in reverse. 1) squares[-1:-4:-2] # 36, 49, 64, 81 # -4 -3 -2 -1 That one says, start at the last index which is 81. Go up until the 4th from last. Then the third specifier tells us how many we'll "skip" by. It wants us to skip by two. Which results in us getting a slice of [ 81, 49] Now I would assume you're question was mainly targeting the ones in which you'll get an empty list. So here goes: 2) squares[-1: -4: 2] Alright, let's break this one down. We see that negative 1 so that means we'll begin at the end of the list. Then we see that we're going up to the 4th from last element. Then, we see the specifier that tells us how many times to skip, but that specifier is a positive 2. So it'll count from the beginning of the list which... doesn't work out. So we get an empty list. 3) squares[-1: 4: 2] Let's do this one. Start at the back of the list. Go up to the 4th index( 5th element, remember counting begins at 0 ). Then it wants us to skip by two. Still doesn't work out. So we get an empty list. 4) squares[1: 4: -2] Begins at the 1st index ( 2nd element). Go up to the 4th index(5th element). Then it wants us to go backwards by 2....Empty list 5) squares [-1: 4: -2] Begin at the last element. Go up to the 4th index (5th element). Then skip by two (but backwards). We get a list of [81, 49, 25].