Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching Numeric

0.1 + 0.1 + 0.1 - 0.3

Im very confused why this dosent return a 0, instead some crazy exponent

Its very simple if you were to add 1 + 1 + 1 and then subtract 3 this would give you Zero 0 This is because you are dealing with integers not floats which hold decimals So .01 + .01 is adding two floats together the result is 0.2 However, as soon as you add the third float this gives us a small variance remainder .1 +.1 + .1 = 0.30000000000000004 then we subtract .3 from this and thats why we get the crazy decimal that is almost zero .1 + .1+ .1 - .3 = 5.551115123125783e-17 this actually means .00000000000000005551115123125783

4 Answers

Matt Farrelly
PLUS
Matt Farrelly
Courses Plus Student 333 Points

It's do with the way that python stores floating point numbers using binary as a base. More info here: https://docs.python.org/3.3/tutorial/floatingpoint.html

it’s basic math. it works in python like in real math if u substract something even if it’s below zero it will give back the result of that substraction

wouldn't a calculator give 0?

why we use underscore with round in in this round(_) what is use of underscore how its working with results which is come up which was 5.2334934 e-17 etc can you define how underscore going to be assgined to the up result thanks

Is there an answer to this question please?

Tim Albertson
Tim Albertson
2,823 Points

The underscore just stores the result of the last expression in the Python Interpreter (REPL) https://hackernoon.com/understanding-the-underscore-of-python-309d1a029edc

In the case of the video lecture, instead of typing the long number, its value was already stored in _, so he used that to save time.

we would use _ , to extract the same value as previously loaded?