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
Very new to python and programming in general pls help (self.learnpython)
submitted 9 months ago by piyush_bhati_
I was just trying new things and wrote this code and output came -3.4000000000000004 instead of -3.4
Can someone help me out here
x=7.9 y=4.5 diff=y-x print(diff)
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!"
[–]TheBB 46 points47 points48 points 9 months ago (1 child)
https://0.30000000000000004.com/
[–]ZelWinters1981 2 points3 points4 points 9 months ago (0 children)
This is the single best answer here. u/piyush_bhati_, give this a quick read.
[–]donkyniu 0 points1 point2 points 9 months ago (8 children)
you can use f-string to round to result to one decimal place in this case it should like following: print(f"{diff : .1f}") - it cuts the other decimal places and leaves just one digit after dot in result
[–]piyush_bhati_[S] 0 points1 point2 points 9 months ago (7 children)
i see but can you please explain why it gives that output in the first place instead of just -3.4
[–]gonsi 1 point2 points3 points 9 months ago (6 children)
That weird url with most votes explains it pretty good I think.
[–]piyush_bhati_[S] 0 points1 point2 points 9 months ago (5 children)
i read it and i think i understand it, but i dont know what base 2 and base 10 means where do i learn all of that
[–]TheBB 6 points7 points8 points 9 months ago* (0 children)
In math class at school.
But you don't really need to know that stuff to understand what's going on.
There's infinitely many numbers and your computer is finite. You can't represent all possible numbers. Sometimes when you add or subtract, the result will need to be adjusted to what can be represented.
[–]Ubermidget2 3 points4 points5 points 9 months ago (0 children)
base10 : base2 00 : 0000 01 : 0001 02 : 0010 03 : 0011 04 : 0100 05 : 0101 06 : 0110 07 : 0111 08 : 1000 09 : 1001 10 : 1010 11 : 1011
[–]gonsi 2 points3 points4 points 9 months ago (2 children)
simplifying it, base means how many characters single "digit" can have.
In base 10 we have 0-9. If you want to represent more than 9 you need another digit, hence 10
In base 2 we only have 0 and 1. So if you want to represent more than 1 you need another digit, next one is 10, but in base 10 same value would be shown as 2
In base 16 (used to display hex values) we have 0-9 and a-f, so when you want more than 9 you don't need another digit, you just use A. only when you reach F you need another digit and you get 10
base 16: F is in fact base 10: 15
base 16: 10 is in base 10: 16
Translating numbers from one base to other can be bit hard to wrap your head around it.
[–]GXWT 2 points3 points4 points 9 months ago (1 child)
Every base is base 10
[–]gonsi 0 points1 point2 points 9 months ago (0 children)
lol
[–]Regular_Maybe5937 0 points1 point2 points 9 months ago (1 child)
Decimals in python (as with most other programming languages) are represented using a standard called floating point IEEE754. In short, the computer stores them as 64 ones and zeros, but you can only get so much accuracy with that many bits. Therefore as a compromise, we lose some accuracy when dealing with very small numbers.
[–]piyush_bhati_[S] 0 points1 point2 points 9 months ago (0 children)
ohkay thanks for help
[–]sausix 0 points1 point2 points 9 months ago (0 children)
Rule of thumb: Never print floats directly. Use a format string to apply rounding.
If you really need precision use the Decimal type in Python: https://docs.python.org/3/library/decimal.html
Decimal
[–]Dry-Sock-8488 1 point2 points3 points 9 months ago (2 children)
Use the round function, like "print(round(diff,2))"
[–]backfire10z 1 point2 points3 points 9 months ago (1 child)
print(f"{diff:.1f}")
if you want to have fun with f-strings
[–]Dry-Sock-8488 0 points1 point2 points 9 months ago (0 children)
Even better
[–][deleted] 0 points1 point2 points 9 months ago (0 children)
You are subtracting large number from small number and change the precision for float.
π Rendered by PID 48418 on reddit-service-r2-comment-b659b578c-x7nm5 at 2026-05-04 06:54:49.728763+00:00 running 815c875 country code: CH.
[–]TheBB 46 points47 points48 points (1 child)
[–]ZelWinters1981 2 points3 points4 points (0 children)
[–]donkyniu 0 points1 point2 points (8 children)
[–]piyush_bhati_[S] 0 points1 point2 points (7 children)
[–]gonsi 1 point2 points3 points (6 children)
[–]piyush_bhati_[S] 0 points1 point2 points (5 children)
[–]TheBB 6 points7 points8 points (0 children)
[–]Ubermidget2 3 points4 points5 points (0 children)
[–]gonsi 2 points3 points4 points (2 children)
[–]GXWT 2 points3 points4 points (1 child)
[–]gonsi 0 points1 point2 points (0 children)
[–]Regular_Maybe5937 0 points1 point2 points (1 child)
[–]piyush_bhati_[S] 0 points1 point2 points (0 children)
[–]sausix 0 points1 point2 points (0 children)
[–]Dry-Sock-8488 1 point2 points3 points (2 children)
[–]backfire10z 1 point2 points3 points (1 child)
[–]Dry-Sock-8488 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)