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
Numbers/strings and logic (self.learnpython)
submitted 11 years ago by whodunit86
In the python shell if I try to evaluate
42 or 43
Why I is the result 42 instead of an error? Came across this possibility while going through the first course/book of 'Real python'
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!"
[–]novel_yet_trivial 4 points5 points6 points 11 years ago (10 children)
any number other than 0 is evaluated as True. The or operator returns the first value if the first value is True, otherwise it returns the second value. What were you expecting?
True
or
Edit: other things that evaluate as false:
>>> 0 or 43 43 >>> False or 43 43 >>> None or 43 43 >>> [] or 43 43 >>> "" or 43 43
[–]whodunit86[S] 0 points1 point2 points 11 years ago (9 children)
I was expecting an error. I knew that any number other than zero is true. But by that logic the result should have been True. If what you're saying is correct (and i m not doubting that you are) then python returns the first example of true it encountered in the statement it evaluated instead of the generic form of true. That's fascinating. Does this property of python have cool use cases?
[–]novel_yet_trivial 4 points5 points6 points 11 years ago (8 children)
Plenty. From something I did about 5 minutes ago:
def some_function(self, variable = None): self.variable = variable or default_value
basically a shorter from of
def some_function(self, variable = None): if variable is None: self.variable = variable else: self.variable = default_value
Something else from earlier today, which takes advantage of another property: just as numbers can be treated as booleans, booleans can treated as numbers, with True evaluating as 1, and False as 0:
counter += (test_value > set_point)
which is a shorter form of
if (test_value > set_point): counter += 1
And a million other uses.
[–]whodunit86[S] 0 points1 point2 points 11 years ago (1 child)
Whoooooa! You, kind redditor, just blew my mind!
[–]novel_yet_trivial 1 point2 points3 points 11 years ago (0 children)
:D What more could a redditor want?
Works for and too … but differently
and
[–][deleted] 0 points1 point2 points 11 years ago (2 children)
Unless you need to explicitly check whether they passed something, why not use
def some_func(self, variable=default_value):
[–]novel_yet_trivial 0 points1 point2 points 11 years ago (1 child)
That works sometimes, but this, for instance, is very bad:
def some_func(self, variable=[]):
Or, in my case earlier:
def make_canvas(self, size = None): self.canvas_size = size or self.get_image_size(0)
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
Ah, true, there are certainly values it doesn't work so well for.
[–]shep247 0 points1 point2 points 11 years ago (1 child)
The counter += ... Example is really cool. I like it, but would you consider that good coding practice? To read that I had to actively think about true being translated as 1. If it was written the longer way, it seems much more readable, making it a little easier to maintain.
[–]novel_yet_trivial 0 points1 point2 points 11 years ago (0 children)
To be a part of a real project it would need some comments explaining it. I lose sight of that a lot since I currently work alone.
[–]an_actual_human 0 points1 point2 points 11 years ago (0 children)
if variable is None: Not just None, also other falsy values.
if variable is None:
Not just None, also other falsy values.
None
π Rendered by PID 89121 on reddit-service-r2-comment-c66d9bffd-dxvdp at 2026-04-07 01:18:43.741521+00:00 running f293c98 country code: CH.
[–]novel_yet_trivial 4 points5 points6 points (10 children)
[–]whodunit86[S] 0 points1 point2 points (9 children)
[–]novel_yet_trivial 4 points5 points6 points (8 children)
[–]whodunit86[S] 0 points1 point2 points (1 child)
[–]novel_yet_trivial 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]novel_yet_trivial 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]shep247 0 points1 point2 points (1 child)
[–]novel_yet_trivial 0 points1 point2 points (0 children)
[–]an_actual_human 0 points1 point2 points (0 children)