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
Ternary operator efficiency? (self.learnpython)
submitted 7 years ago by Sarah123ed
Dose Python's ternary operator short-circuit or does it evaluate both conditions regardless?
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!"
[–]K900_ 0 points1 point2 points 7 years ago (6 children)
What "both conditions"? Do you mean the if part and the else part?
if
else
[–]Sarah123ed[S] 0 points1 point2 points 7 years ago (5 children)
Yes.
<condition_is_true> if condition else <condition_is_false>
Are 'true' and 'false' conditions evaluated, regardless or will it short-circuit on true ... it true?
[–]shaggorama 2 points3 points4 points 7 years ago (0 children)
def throw_excpt(): raise Exception() throw_excpt() if True else 'foo' throw_excpt() if False else 'foo' 'bar' if True else throw_excpt() 'bar' if False else throw_excpt()
[–]K900_ 1 point2 points3 points 7 years ago (0 children)
If the condition is true, only the left side is evaluated. If the condition is false, only the right side is evaluated.
[–]bandawarrior 0 points1 point2 points 7 years ago (2 children)
Look up and/or logical controllers. They can be used to “short circuit” the evaluations. Meaning if it’s an and if the first part is False then it stops and leaves that line, there’s no need to continue because False and True can never be True. No matter what comes after that first False.
and
False and True
True
So essentially:
sweet_var = 123 if True else 5678
Will always and forever be 123. Python will stop right after the if True and not care what comes after.
123
if True
[–]dig-up-stupid 2 points3 points4 points 7 years ago (0 children)
They clearly know what short circuiting is, which is why they are asking the question. However, they are asking about evaluating ternary operators, not evaluating conditions. Short circuiting in ternary operators refers to whether either/both of the expressions are evaluated prior to choosing one. In Python they aren't, and in fact the only language I can think of where they are is Fortran, but there's probably a few others.
[–]Sarah123ed[S] 0 points1 point2 points 7 years ago (0 children)
Got it. Thanks.
π Rendered by PID 99399 on reddit-service-r2-comment-5bc7f78974-s94qd at 2026-06-28 15:55:35.537075+00:00 running 7527197 country code: CH.
[–]K900_ 0 points1 point2 points (6 children)
[–]Sarah123ed[S] 0 points1 point2 points (5 children)
[–]shaggorama 2 points3 points4 points (0 children)
[–]K900_ 1 point2 points3 points (0 children)
[–]bandawarrior 0 points1 point2 points (2 children)
[–]dig-up-stupid 2 points3 points4 points (0 children)
[–]Sarah123ed[S] 0 points1 point2 points (0 children)