If you’d like to learn more about decimal numbers and fractions, then check out:
Numeric Types in Python
Python has three numeric types built into its syntax, which means that you can start using them right away when you run IDLE. The three numeric types native to Python are integers, or whole numbers, such as
Python calls them
int for short. Then there are floating-point numbers, which might have a fractional part, such as
3.14, for example. These are known as floats, and you’ll learn why they are called like that. Finally, there are complex numbers composed of the real and imaginary parts.
00:53 It’s worth mentioning that all three numeric types built into Python are signed, which means they can store both positive or negative values along with the neutral zero. Additionally, integers don’t have a maximum or minimum value, which is only limited by the available memory on your computer. In contrast, floating-point numbers and their complex number cousins do have a fixed range of values as well as precision. Nevertheless, they’re big enough to accommodate most real-life use cases.
Strictly speaking, Python has yet another numeric type, which is a special kind of the integer data type. It’s called Boolean, or bool for short, and can only store one of two values:
These values can be represented as
0, respectively. However, you shouldn’t really think of Booleans as numbers because they’re used for a different purpose, which you’ll learn about in a separate video course about conditional logic.
02:09 Since floating-point numbers have a limited precision, using them to represent currency amounts would inevitably lead to significant rounding errors. You should never use floats to represent financial data in real-world applications unless you don’t mind losing information about your customers’ money.
02:27 A common pattern for representing amounts of money is storing them as integers in terms of the smallest currency unit, such as cents, and then converting them back to dollars. However, that approach won’t work across multiple currencies, which might use different units. For example, one US dollar has exactly 100 cents, while one Bitcoin can be subdivided into almost any fraction. To address the problem with floats, Python provides a few additional numeric types through these standard-library modules, which you’ll learn about at another time.
03:01 You won’t need those extra numeric types during this course, but it’s worth knowing about them. The first one is called decimal because it internally stores numbers using the decimal positional system instead of the binary one, like most other numeric types. Aside from that, it behaves mostly like a floating-point number. Unlike a floating-point number, however, it has an arbitrary yet finite precision, which defaults to twenty-eight decimal places, so you must decide up front how many digits you’d like to keep. Also, while a decimal number works with integers, you can’t mix it with floating-point numbers easily. If that’s a requirement, then you can use another numeric type that comes with Python, called fraction.
04:03 If you’d like to learn more about decimal numbers and fractions, then you can check out Real Python tutorials on How to Round Numbers in Python and Representing Rational Numbers With Python Fractions.
Become a Member to join the conversation.