Q:

Meaning of r[( s== 'even'::2) in python function

Question that asks:

"Create a function that returns the characters from a list or string r
on odd or even positions, depending on the specifier s. The specifier
will be "odd" for items on odd positions (1, 3, 5, ...) and "even" for
items on even positions (2, 4, 6, ...)."
E.g. char_at_pos([2, 4, 6, 8, 10], "even") ➞ [4, 8]

Have managed to solve, but saw a quicker way to solve would be to use:
def char_at_pos(r, s):
    return r[s == 'even'::2]

am wondering how to interpret r[s == 'even'::2], is that adding a condition inside the index bracket before we pick the even index numbers in the list? How does it then also pick the odd numbers?

A:

r[s == 'even'::2]
s == 'even' evaluates to 1 (which is the numerical equivalent for True) if s is 'even', otherwise it evaluates to zero (which is the numerical equivalent for False). Now lets break it down based on examples:

r[1:10] takes all items startig at 1 till item 10 (excluded).

r[1:] takes all items starting at 1 till the end.

r[1:10:2] takes every second item starting at 1 till item 10, and

r[1::2] correspondingly takes every second item starting at 1 till the end.

