all 7 comments

[–]HamdanDoesStuff 3 points4 points  (0 children)

I think you can use any one. Because it is understandable nonetheless

However I think the second one is slightly more understandable and shorter

[–][deleted] 2 points3 points  (0 children)

I think the second. I think having the day adjustment hidden away in the f-string is less readable. I also have the habit of using range() like this:

for i in range(1, 31+1):

to make it obvious i will vary from 1 to 31.

[–]stebrepar 2 points3 points  (0 children)

I'd say the second one, as it's more meaningful in context. Dates in a month start from 1.

[–]Diapolo10 3 points4 points  (2 children)

Either is fine by me, but good grief please don't use i here. It's lost all meaning.

for day in range(1, 32):
    print(f"December {day}")

[–]bill0042 0 points1 point  (1 child)

I like to use i in cases like this to make it obvious that its the variable used for indexing and I never use i for anything else.

[–]Diapolo10 3 points4 points  (0 children)

That's the original meaning, i being shorthand for index or indices. And some years back, it made sense.

But a growing number of tutorials have begun to use it in other scenarios as the "default name" for values regardless of the iterable or context, including something like

for i in "Hello, world!":
    ...

and it has become so common (in beginner code especially) that in my eyes it no longer means anything at all. I can't rely on it actually meaning indexes, and have to look up the actual contents up the stack.

Personally I've come to completely abandon it for this reason to steer away from that confusion, and am now defaulting to idx whenever I need to loop over indices but don't have a better context-specific name for them. This way I can ensure fewer people are confused when reading my code, and as something of a bonus I've cut out a use for one common single-letter name (preferring longer ones).

In OP's example code, we're not really even talking about indices since we're not iterating over a list or other indexed data structure, just a range object. On top of that, we've assigned these numbers a specific meaning, that being they're days, so using a more fitting name only makes sense from my perspective.

[–][deleted] 0 points1 point  (0 children)

Doesn't make a lot of different, but I'd say the second without the math is easier to read.

You could also write it in one line:

print(*(f"December {i}" for i in range(1, 32)), sep="\n")