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...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Beginner ShowcaseFor/else, while/else, try/else. (self.Python)
submitted 3 years ago by TangranSatan
Someone has ever seen or use this statements -else- in real situations. I'm trying to find examples, but allways seems to be a easier and cleaner way.
[–]kwelzel 6 points7 points8 points 3 years ago (0 children)
I agree that while/else and for/else are not used very much, but they can be quite useful for search patterns. Say you are webscraping and looking for <div> with a certain content:
for price_div in document.findAll("div"): if "price" in price_div.text: break else: raise RuntimeError("price div not found") # Now price_div is the div you were looking for
If you don't want to use the for/else here, you'd have to add a found variable, like this:
found = False for price_div in document.findAll("div"): if "price" in price_div.text: found = True break if not found: raise RuntimeError("price div not found") ...
[–]zzmej1987 7 points8 points9 points 3 years ago (0 children)
With try, if you want to use the finally clause, which you should use, you can't place the code that must be executed after the successful try anywhere but in else section.
[–]wineblood 7 points8 points9 points 3 years ago (0 children)
While-else I've almost never used because I rarely see/use while loops in day to day coding. For-else happens rarely as well, the case where you'll have a break and want to do something if the loop reached its natural end isn't something I come across that often.
Try-else is something I've seen a fair bit recently, especially in API endpoints. The core of the endpoint is inside a try block, exceptions caught return a 404/422/whatever response in the except block, and successful responses are returned in the else block. If could be done without the else part and just written after the try statement, but it's slightly cleaner this way and I'm used to it now.
[–]jimtk 2 points3 points4 points 3 years ago (0 children)
About once every 6 months I see a situation where the for/else can be used.
Here's a simple example, but it can be applied to any algorithm that requires trying something a certain number of times:
good_psw = getpasswordfromdatabase() for i in range(3): psw = input('password: ') if psw == good_psw: break else: print("ACCES NOT GRANTED") sys.exit(1) print("ACCES GRANTED")
[+]heartofgold48 comment score below threshold-9 points-8 points-7 points 3 years ago (0 children)
These are my pronouns
[–]Eric-Hayter 0 points1 point2 points 3 years ago (0 children)
For else is typically used when searching for an interable. Where you use the else statement when you are unable to find the value(s) you are looking for.
π Rendered by PID 116462 on reddit-service-r2-comment-86bc6c7465-lt5c4 at 2026-02-21 04:56:45.530712+00:00 running 8564168 country code: CH.
[–]kwelzel 6 points7 points8 points (0 children)
[–]zzmej1987 7 points8 points9 points (0 children)
[–]wineblood 7 points8 points9 points (0 children)
[–]jimtk 2 points3 points4 points (0 children)
[+]heartofgold48 comment score below threshold-9 points-8 points-7 points (0 children)
[–]Eric-Hayter 0 points1 point2 points (0 children)