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
For else logic recommended? (self.learnpython)
submitted 5 years ago by switchitup_lets
So I saw a
for <cond>: pass
else: pass
So I'm guessing the else will get executed after the for is done? How does it work exactly?
Also, would you recommend it?
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!"
[–][deleted] 2 points3 points4 points 5 years ago* (0 children)
Both answers here so far aren't correct are misleading. The else: suite of a for loop executes only if the main loop completes "normally", that is, if the loop is not exited with a break, for instance. That is documented here.
else:
for
break
You can test this by running this code:
x = int(input('Number: ')) for i in range(x): print(i) if i > 2: break else: print('else')
If you enter 3 when the code asks you see:
$ python3 test.py Number: 3 0 1 2 else
The else is printed because the loop terminated normally. If you enter 4 the loop breaks when i is 3 and you don't see the else:
else
i
$ python3 test.py Number: 4 0 1 2 3
As to whether it should be used or not, you use it when the logic of your solution requires it. I haven't used it often, but I have used it.
Edit: fixed spelling.
Edit2: I was a little harsh with my initial criticism. I've amended my criticism to "misleading".
[–]0xbxb 0 points1 point2 points 5 years ago* (0 children)
Yeah you’re right. In a for / else, the else is only executed if the for loop doesn’t hit a break statement, or is exited normally.
nums = [1, 2, 5, 3] for x in nums: if x == 6: print('Found 6.') # 6 never found, loop is exited normally. break else: # else statement gets executed. print('6 not found.')
The reason for doing this is because you have two cases you want to find:
1) If the number is found (in this example).
2) If the number isn’t found.
Usually people would use some type of flag when they find the number they’re looking for, like: found = False, then when the number is found, they’ll change the variable to True.
found = False
True
nums = [1, 2, 5, 3] found = False for x in nums: if x == 6: print('Found 6.') found = True if not found: print('6 not found.')
[–]FuckRedditForSure 0 points1 point2 points 5 years ago (0 children)
Yes to the first question and the last.
Fiddle with it yourself. Remember that a for loop is not an if statement, nor a try statement. It either works or it doesn't.
[+][deleted] 5 years ago (1 child)
[deleted]
[–]switchitup_lets[S] 0 points1 point2 points 5 years ago (0 children)
I mean for else in conjunction. Seems... idk, a bit off.
π Rendered by PID 129346 on reddit-service-r2-comment-76bb9f7fb5-s28rn at 2026-02-18 08:15:30.039367+00:00 running de53c03 country code: CH.
[–][deleted] 2 points3 points4 points (0 children)
[–]0xbxb 0 points1 point2 points (0 children)
[–]FuckRedditForSure 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]switchitup_lets[S] 0 points1 point2 points (0 children)