0
import csv
f = open('age.csv',encoding='utf8')
data = csv.reader(f)

for row in data :
    if '신도림' in row[0]:
        for i in row[3:]:
            print(i)

Hello, I just started python data analysis yesterday. csv file that I am using is about population survey based on ages and I got this question now. When I use row[3] it gives me something like this

3
2
6

but when I use row[3:] it gives me the correct answer.

326
457
345
.
.
.

What is different between these two? Thank you for your helping.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
JeeWoong Lee
  • 41
  • 1
  • 7

1 Answers1

0

The difference is this:

row[3] is exactly one item (in this case, row): the fourth item (since counting starts at zero).

row[3:] is a range of many items: from the fourth item in the list to the end of the sequence.

The [<start-index>:<stop-index>:<step>] syntax is shorthand for creating a slice object. This slice object is a sort of generalized way to index many items in a sequence.

NicholasM
  • 4,557
  • 1
  • 20
  • 47
  • ah so that means 3: makes it goes to the end of the item list! Thanks now I perfectly understand what gets me a wrong answer. – JeeWoong Lee Jan 06 '20 at 03:12
  • That’s great. If this answers your question, see this help page for how to update the question: https://stackoverflow.com/help/someone-answers – NicholasM Jan 06 '20 at 03:49