Numeric types include integers and floating point. Use the decimal module for fixed-point arithmetic when working with financial applications.
- [Instructor] Python 3 has two basic numeric types, integer and floating point. There are other types derived from these including the built in complex type. In fact, because everything is an object in Python, you can derive your own types. Here we're going to talk about integer and float. Here in Komodo, I've opened a working copy of types.py from chapter three of the exercise files. And you notice down here on line four, x equals seven, and seven is an integer.
So if I save this and run it you see it says class int. And if I multiply this by three, save and run, we now have a value of 21 and it's still a class int. So it's an integer. On the other hand if I say seven point zero. And save and run it, we now have class float, which is a floating point number. Or, if I say seven times three point one four one five nine, I save this and run it, we now have a floating point value because we used a floating point number in our calculation.
On the other hand, if I divide two integers, say seven divided by three, when I save and run this, I have a floating point number even though I divided two integers. This is a behavior that's new in Python 3. In Python 2 it would've done an integer division and given me the result without the remainder. I can still get that result by using a double divide sign. If I save and run this, I now get two, because you can fit two three's and seven with a remainder of one.
And if I use the modulo operator, the percent sign, then I get that remainder and these types are of course int. Now here's an interesting problem. If I say point one, plus point one, plus point one, minus point three, you would expect my result would be zero. When I save and run this you notice that my result is five point five five etc, e minus 17. That's all 17 places to the right of the decimal point.
We're rather moving the decimal point 17 places to the left. So it's a very very minuscule number, it's almost zero but it's not zero. This is especially a problem when dealing with money. You remember when we do arithmetic in school, if we add these three point one's and we subtract point three, we're going to get a zero and it's not going to have a bunch of numbers 17 places to the right of the decimal point. This is the difference between accuracy and precision.
Because of the way that the computers do floating point, they're sacrificing accuracy for precision and so it may do this arithmetic correctly to 17 decimal places, which is the precision of the floating point processor inside the computer. But it is not accurate. Accuracy is the true value of a calculation. As an illustration, here is an actual cash register, I took this photograph at a movie theater in Phoenix Arizona in December 2011.
And you'll notice that the total cost of my water because I bought water in a movie theater which is ridiculously expensive was $13.50 and a little bit more. So it's 13.50001 and it made everything wrap around weird on the screen because the screen was formatted for two decimal places and instead somebody forgot that floating point numbers don't work well with money.
And in fact, that's my point here. You don't want to use floating point numbers for money because you end up with those kinds of problems because 10 cents plus 10 cents plus 10 cents minus thirty cents needs to be zero cents in order for books to balance properly. So how do we solve this problem? Python comes with a built in module called import. And so I'm going to say from decimal, import, everything.
And now here I can say a equals decimal. I now have a type point one o. So that's 10 cents in decimal. And you notice I have to put that in quote marks. It's going to convert it from a string because we don't want to pass it a floating point number because a floating point number isn't accurate. It has precision but it doesn't have accuracy. And so I say b equals decimal, point three o. And now I can say x equals a plus a plus a minus b, and when I save and run this you notice that the result is zero point zero zero which will balance your books really nicely.
And the type is class decimal dot decimal. So it's the decimal class from the lowercase decimal library. So you're going to want to use something like this. It can either be this module or one that you write yourself or another one but you want to use a proper decimal arithmetic module when you're dealing with money. So understanding the numeric types can save you a great deal of trouble for many purposes, the simple integer and float types will work fine for other purposes the decimal module will be necessary or you may want to drive your own class.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api