Python 101 - Numeric Types
Python is a little different than some languages in that it only has three built-in numeric types. A built-in data type means that you don’t have to do anything to use them other than typing out their name.
The built-in numeric types are:
intfloatcomplex
Python 2 also had the long numeric type, which was an integer able to represent values larger than an int could. In Python 3, int and long were combined so that Python 3 only has int. You can create an int by simply typing the number or by using int(). 2, 3, int(4), and int(”5”) are all integers.
If you are familiar with C++, you probably know that floating-point numbers are defined using the double keyword. In Python, you can create a float by typing it or by using float(). 3.14, 5.0, float(7.9), and float(”8.1”) are all floating point numbers.
A complex number has a real and an imaginary part. The real and imaginary parts are accessed using attribute notation: .real and .imag, respectively. Complex numbers can be created by either typing them or using complex(). 2+1j, 2-1j, 5j, complex(7+2j), complex(”7+2j”), and complex(7, 2)` are all complex numbers.
There are two other numeric types that are included with Python in its standard library. They are as follows:
decimal- for holding floating-point numbers that allow the user to define their precisionfractions- rational numbers
You can import these libraries using Python’s import keyword, which you will learn about in chapter 16. You might also be interested in checking out Python’s round() keyword or its math module.
Let’s go ahead and learn a little bit more about how you can create and use numeric types in Python!
Integers
You can create an integer in two ways in Python. The most common way is to assign an integer to a variable:
my_integer = 3The equals sign (=) is Python’s assignment operator. It “assigns” the value on the right to the variable name on the left. So in the code above, you are assigning the value 3 to the variable my_integer.
The other way to create an integer is to use the int callable, like this:
my_integer = int(3)Most of the time, you won’t use int() to create an integer. In fact, int() is usually used for converting a string or other type to an integer. Another term for this is casting.
A little-known feature about int() is that it takes an optional second argument for the base in which the first argument is to be interpreted. In other words, you can tell Python to convert to base2, base8, base16, etc.
Here’s an example:
The first argument has to be a string while the second argument is the base, which in this case is 2.
Now let’s move on and learn how to create a float!
Floats
A float in Python refers to a number that has a decimal point in it. For example, 2.0 is a float while 2 is an int.
You can create a float in Python like this:
my_float = 2.0This code will assign the number, 2.0, to the variable my_float.
You can also create a float like this:
my_float = float(2.0)Python’s float() built-in will convert an integer or even a string into a float if it can. Here’s an example of converting a string to a float:
my_float = float(”2.0”)This code converts the string, “2.0”, to a float. You can also cast string or floats to int using the int() built-in from the previous section.
Note: The float numeric type is inexact and may differ across platforms. You shouldn’t use the float type when dealing with sensitive numeric types, such as money values, due to rounding issues. Instead, it is recommended that you use Python’s decimal module.
Complex Numbers
A complex number consists of a real and an imaginary part, each of which is a floating-point number. Let’s look at an example with a complex number object named comp to see how you can access each of these parts by using comp.real and comp.imag to extract the real and imaginary parts, respectively, from the number:
In the code sample above, you created a complex number. To verify that it is a complex number, you can use Python’s built-in type function on the variable. Then you extract the real and imag parts from the complex number.
You can also use the complex() built-in callable to create a complex number:
>>> complex(10, 12)
(10+12j)Here you created a complex number in the interpreter, but you don’t assign the result to a variable.
Numeric Operations
All the numeric types, with the exception of complex, support a set of numeric operations.
Here is a list of the operations that you can do:
You should check out the full documentation for additional details about how numeric types work (scroll down to the Numeric Types section):
Augmented Assignment
Python supports doing some types of arithmetic using a concept called Augmented Assignment. This idea was first proposed in PEP 203:
The syntax allows you to do various arithmetic operations using the following operators:
+= -= *= /= %= **= <<= >>= &= ^= |=
This syntax is a shortcut for doing common arithmetic in Python. With it you can replace the following code:
with this:
This code is the equivalent of the previous example.
Wrapping Up
In this chapter, you learned the basics of Python’s Numeric types. Here, you learned a little about how Python handles int, float, and complex number types. You can use these types for working with most operations that involve numbers. However, if you are working with floating-point numbers that need to be precise, you will want to check out Python’s decimal module. It is tailor-made for working with that type of number.
Get the Book
This article comes from my book, Python 101. You can get the entire book at the following:






