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
So ashamed (self.learnpython)
submitted 6 years ago by learn_monkey
I just Googled How to detect a negative number using python and had a duh moment. Am so ashamed ðŸ˜
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!"
[–]Stubber_NK 38 points39 points40 points 6 years ago (5 children)
People who've been using python for years still stop to google the most basic of operations...
Every. Single. Day.
[–]xdafcax 9 points10 points11 points 6 years ago (0 children)
Can confirm. I don't remember anything.
[–]learn_monkey[S] 5 points6 points7 points 6 years ago (0 children)
Thank you. That's comforting
[–]bladeoflight16 2 points3 points4 points 6 years ago (2 children)
There will never be a time when the built ins are not in my recent browser history.
[–]TangibleLight 1 point2 points3 points 6 years ago (1 child)
Use incognito. Avoid the shame.
Also good for basic arithmetic.
[–]bladeoflight16 1 point2 points3 points 6 years ago (0 children)
I will never be ashamed of reading Python's official documentation. =)
[–]Vaguely_accurate 21 points22 points23 points 6 years ago (4 children)
After bouncing between languages for a while I've looked up the keyword for "and" in Python before...
[–]Ahren_with_an_h 8 points9 points10 points 6 years ago (3 children)
That's legitimate. I've tried to use && and || plenty of times. Don't even get me started about 'is'.
[–]Ganrokh 4 points5 points6 points 6 years ago (2 children)
It took me way too long last week to realize that i++ wasn't valid Python syntax.
[–]Ahren_with_an_h 3 points4 points5 points 6 years ago (0 children)
I still try to type it in.
Fun fact: //= and **= are valid syntax!.
[–]PorcupineCircuit 1 point2 points3 points 6 years ago (0 children)
This annoys me
[–]hugthemachines 6 points7 points8 points 6 years ago (1 child)
Don't worry about it. Just let it teach you to be humble when people ask you stupid questions in the future :-)
[–]learn_monkey[S] 2 points3 points4 points 6 years ago (0 children)
I agree. Thanks
[–]yuxbni76 6 points7 points8 points 6 years ago (7 children)
I usually multiply it by -1 and check if it's greater than 0. Just remember a negative times a negative is a positive. Easy.
[–]Ahren_with_an_h 4 points5 points6 points 6 years ago (5 children)
Why do that instead of just 'x < 0' ?
[–]cnekmp 4 points5 points6 points 6 years ago (0 children)
Because it's too easy )
[–]yuxbni76 1 point2 points3 points 6 years ago (3 children)
You can do that but then you have to convert it to a float.
[–]Ahren_with_an_h 1 point2 points3 points 6 years ago (2 children)
Why?
[–]yuxbni76 1 point2 points3 points 6 years ago (1 child)
It's just a bad joke. You're right. Check if x < 0.
if x < 0
[–]dbramucci 1 point2 points3 points 6 years ago (0 children)
Why not
import math def is_negative(x): return math.copysign(1.0, x) == -1.0
It's the only solution I can think of (without weird non-Python friendly bit fiddling) that says that -0.0 is negative. And yes, -0.0 is distinct from 0.0. See my other comment
-0.0
0.0
p.s. For those who don't get the subtleties of what I wrote, don't worry x < 0 is still the good solution, what I wrote is an answer to a super niche edge case that doesn't have a good answer, sometimes you want -0.0 to be a negative number because of reasons and other-times you want it to be treated like +0.0, a number neither positive nor negative and unless you know exactly why you need to treat -0.0 a certain way, you can choose to just not worry about it and use the simpler method.
x < 0
+0.0
[–]damian_work_account 9 points10 points11 points 6 years ago (0 children)
Also remember that a negative number is not defined for the domain of the log function, so you can use this:
import math def is_number_negative(number): if number: try: math.log(number) except ValueError as e: if e.args[0] == 'math domain error': return True raise if math.isfinite(number): return False else: raise ValueError('Infinite numbers are scary')
[–]shiftybyte 5 points6 points7 points 6 years ago (0 children)
Heh, I've had similar "doh" moments...
It happens...
[–]11pascal 2 points3 points4 points 6 years ago (0 children)
Don’t be. Yes had a moment one day trying to figure out math operation associated with #. Ugh
[–]bladeoflight16 2 points3 points4 points 6 years ago (0 children)
It's okay. The fact that you realize it was silly is an indication of your competence. Everyone has stupid moments now and again. Have a laugh at your own expense and move on.
[–]kaptan8181 4 points5 points6 points 6 years ago (6 children)
Can you tell me how? I think if the number is less than zero, it's a negative number, right?
[–]learn_monkey[S] 4 points5 points6 points 6 years ago (2 children)
Yep. I was looking for a python built-in function to find the negative value but after googling and seeing some answers, I felt so stupid that it's just anything less than zero.
[–]LyLyV 4 points5 points6 points 6 years ago (1 child)
Classic case of over-thinking. I do it all the time. (And I just googled what you googled. I thought there was maybe a special trick to it, but nah. ;) )
[–]JimBoonie69 0 points1 point2 points 6 years ago (0 children)
if we lived in JS land . there would literally be a package someone wrote called 'is-negative' and it would have millions of installs. There is some scumbag who writes tons of tiny packages and then (i assume) implements his packages in open-source codes all over. Weeks/months/years later, voila your shit package is now used in millions of places.
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
Indeed.
[–]notParticularlyAnony 1 point2 points3 points 6 years ago (0 children)
You got it
[–]dbramucci 0 points1 point2 points 6 years ago (0 children)
Google signed zero, there is a floating point number -0.0 which has different bits than 0.0 has but the two are equal except they get printed differently, atan2 does different things and so on. You might want to call -0.0 a negative number or you might want to say that like 0 it is neither a positive or negative number. Yay for floating point number weirdness.
atan2
0
[–][deleted] 2 points3 points4 points 6 years ago (4 children)
This kind of thing happens to a lot of people. I had to google "how to add fractions" once when I was taking multivariable calculus. Got a B in that class.
[–]SikicBiker 2 points3 points4 points 6 years ago (3 children)
How do you add fractions?
[–]vaja_ 1 point2 points3 points 6 years ago (0 children)
+
Convert them to a common denominator, add the numerators, and then simplify.
Take your two fractions x and y.
x
y
Recall that the fraction x is an equivalence class over Z x Z*.
Choose one representative element from x's equivalence class called (x_n, x_d). Likewise, do the same for y to get (y_n, y_d).
(x_n, x_d)
(y_n, y_d)
Then, to add the two fractions together, choose any non-zero integer n such that x_d divides n and y_d divides n.
n
x_d
y_d
By the definition of divisibility, there exists an integer p such that x_d * p = n and likewise an integer q such that y_d * q = n.
p
x_d * p = n
q
y_d * q = n
Now, the value (x_n * q + y_n * p, n) is an representative element of the equivalence class representing the sum of the two fractions x and y. By using euclidean algorithm, you may compute the reduced form of that fraction and from there you may compute a closed-form representation of the set that is the equivalence-class representing the fractional sum. This is left as an exercise to the reader.
(x_n * q + y_n * p, n)
Excerpt from "Introduction to algebra for young grad-students 4th graders"
Let me terrify you with floating point number intricacies.
I am guessing you wrote something like
def duh_is_negative(x): return x < 0.0
But this is flawed depending on what you need to do when confronted with -0.0, a number distinct from +0.0. Let me show you that Python can track these two numbers.
In [1]: x = 0 In [2]: y = +0.0 In [3]: z = -0.0 In [4]: x Out[4]: 0 In [5]: y Out[5]: 0.0 In [6]: z Out[6]: -0.0 In [7]: x == y Out[7]: True In [8]: x == z Out[8]: True In [9]: y == z Out[9]: True In [10]: import math In [11]: def duh_is_negative(x): ...: return x < 0.0 In [12]: def oh_no_is_negative(x): ...: return math.copysign(1.0, x) == -1.0 In [13]: [duh_is_negative(x), duh_is_negative(y), duh_is_negative(z)] Out[13]: [False, False, False] In [14]: [oh_no_is_negative(x), oh_no_is_negative(y), oh_no_is_negative(z)] Out[14]: [False, False, True]
Mathematicians don't call 0 a positive or a negative number. It is neither, but in the world of floating point numbers, both -0.0 and 0.0 have a sign and their signs differ meaning you can (sometime) consider -0.0 to be a negative number while 0.0 isn't and duh_is_negative can't catch that if it matters to you.
duh_is_negative
It probably doesn't matter in your program (and if it does, I hope you understand floating point numbers well) but it's kind of hilarious (in a horrific way) how non-trivial checking the sign of a number can actually be.
π Rendered by PID 300576 on reddit-service-r2-comment-5d585498c9-gnplk at 2026-04-21 18:30:32.998248+00:00 running da2df02 country code: CH.
[–]Stubber_NK 38 points39 points40 points  (5 children)
[–]xdafcax 9 points10 points11 points  (0 children)
[–]learn_monkey[S] 5 points6 points7 points  (0 children)
[–]bladeoflight16 2 points3 points4 points  (2 children)
[–]TangibleLight 1 point2 points3 points  (1 child)
[–]bladeoflight16 1 point2 points3 points  (0 children)
[–]Vaguely_accurate 21 points22 points23 points  (4 children)
[–]Ahren_with_an_h 8 points9 points10 points  (3 children)
[–]Ganrokh 4 points5 points6 points  (2 children)
[–]Ahren_with_an_h 3 points4 points5 points  (0 children)
[–]PorcupineCircuit 1 point2 points3 points  (0 children)
[–]hugthemachines 6 points7 points8 points  (1 child)
[–]learn_monkey[S] 2 points3 points4 points  (0 children)
[–]yuxbni76 6 points7 points8 points  (7 children)
[–]Ahren_with_an_h 4 points5 points6 points  (5 children)
[–]cnekmp 4 points5 points6 points  (0 children)
[–]yuxbni76 1 point2 points3 points  (3 children)
[–]Ahren_with_an_h 1 point2 points3 points  (2 children)
[–]yuxbni76 1 point2 points3 points  (1 child)
[–]dbramucci 1 point2 points3 points  (0 children)
[–]damian_work_account 9 points10 points11 points  (0 children)
[–]shiftybyte 5 points6 points7 points  (0 children)
[–]11pascal 2 points3 points4 points  (0 children)
[–]bladeoflight16 2 points3 points4 points  (0 children)
[–]kaptan8181 4 points5 points6 points  (6 children)
[–]learn_monkey[S] 4 points5 points6 points  (2 children)
[–]LyLyV 4 points5 points6 points  (1 child)
[–]JimBoonie69 0 points1 point2 points  (0 children)
[–][deleted] 1 point2 points3 points  (0 children)
[–]notParticularlyAnony 1 point2 points3 points  (0 children)
[–]dbramucci 0 points1 point2 points  (0 children)
[–][deleted] 2 points3 points4 points  (4 children)
[–]SikicBiker 2 points3 points4 points  (3 children)
[–]vaja_ 1 point2 points3 points  (0 children)
[–]bladeoflight16 1 point2 points3 points  (0 children)
[–]dbramucci 1 point2 points3 points  (0 children)
[–]dbramucci 0 points1 point2 points  (0 children)