-1

I'm trying to clarify my understanding regarding situations in the code in which the singular seems to extract from the plural. I'm not sure if this is standard among different languages, or, unique to specific situations in python. Regardless, in the example below I refer to square within the squares list:

squares=['white', 'black', 'red', 'blue', 'green']

for i, square in enumerate(squares):
    print(i, square)
0 white
1 black
2 red
3 blue
4 green

Is python intuitive enough to know I am referring to the singular square from the "squares" list? Is it unique to just this type of coding request or is it more broad and standard? etc.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 5
    No, of course not. This has nothing to do with the names. You could use `a` and `b` and this would work exactly the same. – Daniel Roseman Oct 27 '19 at 20:14
  • 1
    You seem to be referring to variable names. Python is completely unaware of your conventions in naming. – quamrana Oct 27 '19 at 20:15
  • 1
    `square = [...]` with `for i, squares in enumerate(square): print(i, squares)` would work exactly the same. It would just be more confusing to the reader because they would expect a plural name to refer to (for example) a list. – chepner Oct 27 '19 at 20:18

1 Answers1

3

The variables square and squares are completely unrelated. In this case, the use of the variable square (singular) is an arbitrary decision by the coder. I.e.

squares=['white', 'black', 'red', 'blue', 'green']
for i, square in enumerate(squares): print(i, square)

...would work identically had the coder chosen ...

wombats=['white', 'black', 'red', 'blue', 'green']
for oodles, noodles in enumerate(wombats): print(oodles, noodles)

The enumerate method is a generator which iterates through a list, and yields each item in the list's index, and value in that order. By convention only, coders often choose i to stand for index and will frequently give the list a plural name, and the variable intended to hold the value the singular of that plural.

But it is entirely arbitrary and up to the coder's preferences what the variable's names are.

RightmireM
  • 2,381
  • 2
  • 24
  • 42
  • 1
    Ah I see thanks RightmireM this clarifies a great deal. As all the videos and training material I use there seems to be a lack of explanation on the description used which may appear more precise than it actually is. Cheers. – Foreverlearning Oct 27 '19 at 20:28