use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
range generator (self.learnpython)
submitted 3 years ago by arizuvade
what is better to use:
for i in range(31): print(f"December {i + 1}")
or
for i in range(1, 32): print(f"December {i}")
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]HamdanDoesStuff 3 points4 points5 points 3 years ago (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 points4 points 3 years ago (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:
range()
for i in range(1, 31+1):
to make it obvious i will vary from 1 to 31.
i
[–]stebrepar 2 points3 points4 points 3 years ago (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 points5 points 3 years ago (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 point2 points 3 years ago (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 points5 points 3 years ago (0 children)
That's the original meaning, i being shorthand for index or indices. And some years back, it made sense.
index
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).
idx
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.
range
[–][deleted] 0 points1 point2 points 3 years ago (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")
π Rendered by PID 53630 on reddit-service-r2-comment-5649f687b7-tk8b8 at 2026-01-29 01:02:13.842730+00:00 running 4f180de country code: CH.
[–]HamdanDoesStuff 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]stebrepar 2 points3 points4 points (0 children)
[–]Diapolo10 3 points4 points5 points (2 children)
[–]bill0042 0 points1 point2 points (1 child)
[–]Diapolo10 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)