swcarpentry / python-novice-gapminder

Plotting and Programming in Python
http://swcarpentry.github.io/python-novice-gapminder/
Other
163 stars 427 forks source link

In stepping through a list - description and example don't match #534

Open nishreenk opened 3 years ago

nishreenk commented 3 years ago

Stepping Through a List

What does the following program print?

element = 'fluorine'

print(element[::2])

print(element[::-1])

  1. stride is the step size of the slice ... This is OK

  2. The slice 1::2 selects all even-numbered items from a collection: it starts with element 1 (which is the second element, since indexing starts at 0), goes on until the end (since no end is given), and uses a step size of 2 (i.e., selects every second element). {: .solution} {: .challenge}.

    HERE the slice we are talking about is from [: : 2] and not [1: :2]

It should be [: : 2] and not [1::2] The slice ::2 selects all even-numbered items from a collection: it starts with element 0(which is the first element, since indexing starts at 0), goes on until the end (since no end is given), and uses a step size of 2 (i.e., selects every second element). {: .solution} {: .challenge}.

alee commented 3 years ago

(reference: http://swcarpentry.github.io/python-novice-gapminder/11-lists/index.html#stepping-through-a-list)

Thanks for the feedback @nishreenk ! The wording is indeed a bit confusing and could use some clarification. What if we changed the challenge to ask the learner to do both, with text something like:

What expression would select every other item in this collection, starting with the 1st item in the collection? The output should be: ['f', u', 'r', 'n'].

What if you wanted to select every other item in this collection, starting with the 2nd item in the collection? The output should be ['l', 'o', 'i', 'e'].

gfriss commented 4 months ago

Hi everyone,

I agree with @nishreenk that the solution provided for this exercise is not matching the exercise itself entirely as the starting index is different for [::2] (as in exercise) and [1::2] (as in solution).

However, this discussion has given me a thought of referring to the number of element in question.

Long version: Intuitively, the physically first element (index 0) is called the first element by many people who are new to programming. However, Python, and many other languages, starts counting from 0. Would it not be more beneficial to use the index numbers in text to make it clear what we mean and to emphasise how Python counts? What I mean is the introduction of the zeroth element in phrasing the exercises and solutions (as it was introduced at the beginning of the lesson). Additionally, instead of saying that index 1 is the second element, we should keep pushing this idea by saying that the element with index 1 is the first element, as there is a zeroth element. This would clarify your suggested exercise, @alee, whether it is the intuitive or Pythonic 1st and 2nd elements you are asking for (it is nonetheless a good set of practise questions).

In short: use zeroth, first, second, etc. words instead of first, second, third, etc. more consistently.

I am aware that this can cause confusion in early stages but consistency is key to develop this mindset and understanding.

What are your thoughts?

vahtras commented 4 months ago

Oh my goodness, this was an old issue. @nishreenk are you still around and want to do a PR for this? I agree with your remark.

However, @gfriss , I don't recommend using common language "first" for list items with index 1. It is just not common practise.

gfriss commented 4 months ago

@vahtras, I see and accept. At the beginning of the lesson, there is an example under the Use an item’s index to fetch it from a list section using the phrases "zeroth" and "fourth" item (see code snippet below). Would it then be worthwhile to change these to first and fifth item for the sake of consistency?

code snippet: print('zeroth item of pressures:', pressures[0]) print('fourth item of pressures:', pressures[4])