Numeric Types in Python
If you’d like to learn more about decimal numbers and fractions, then check out:
00:00 In this lesson, you’ll get a bird’s-eye view of the available numeric types in Python.
00:07
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 -3
, 0
, or 42
.
00:22
Python calls them int
for short. Then there are floating-point numbers, which might have a fractional part, such as -2.72
or 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:42
These are slightly more advanced and rarely used in practice, so you’ll cover complex numbers in a later lesson. The corresponding data type in Python is called complex
.
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.
01:22
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: True
or False
.
01:35
These values can be represented as 1
and 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.
01:49 In most applications, you’ll use integers and floating-point numbers almost exclusively because they cover the widest range of problems while being the most efficient.
01:58 One notable exception though are financial operations on fractional values, which require exact precision, especially when you work with big and small quantities at the same time.
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.
03:45 It represents a rational number or quotient of two integers, like one-third.
03:51 Fractions have infinite precision, which lets you represent numbers exactly, even if they have a recurring decimal or binary expansion, without any loss of information due to rounding.
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.
04:16 Now that you have a general idea about the numeric types in Python, it’s time to take a closer look at integers.
Bartosz Zaczyński RP Team on June 7, 2023
@deanwaters1991 In a nutshell, you’ll want to use floating-point numbers for engineering purposes where exact precision is less important than performance. The IEEE 7554 standard defines the floating-point data type and its operations, which most CPUs implement in hardware. On the other hand, you’d prefer the decimal data type for financial calculations that do require exact precision while incurring a slight performance penalty.
You can’t mix floating-point numbers and decimals in a single expression in Python, though:
>>> from decimal import Decimal
>>> Decimal("3.14") + 0.0015
Traceback (most recent call last):
File "<input>", line 1, in <module>
Decimal("3.14") + 0.0015
~~~~~~~~~~~~~~~~^~~~~~~~
TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'
deanwaters1991 on June 13, 2023
Thank you for your reply 😊👍.
Become a Member to join the conversation.
deanwaters1991 on June 6, 2023
What is the difference between using float and decimal?