Numeric Types#

Nearly every program uses numbers at some point, whether you are keeping track of a game score or doing calculations for graphics. In this lesson we’ll be learning about the numeric types provided by Python.

Table of Contents

Introduction#

Numbers are primitive data types, meaning they are the basic building blocks that make up other types.

Python provides four numeric types:

Name

Type

Description

Example

Integer

int

whole numbers

1

Float

float

floating point numbers

1.0

Boolean

bool

True or False

True

Complex

complex

imaginary numbers

1+1j

More on each of these later, but first lets take a look at how numbers in Python are alike.

Part 1: Common traits#

All numeric types are based on the numbers.Number class, so they have some things in common.

They can be positive or negative.

Listing 149 Python shell#
>>> -5
-5

>>> +5.0
5.0

>>> -3+5j
(-3+5j)

They almost always play nicely together–they can be compared to one another, used interchangeably with most operators, and converted into one another.

Listing 150 Python shell#
>>> .5 < 5
True

>>> 5 - 0.5
4.5

>>> 1 + True
2

>>> 22 + .5
22.5

>>> 1.25 + 3+2j
(4.25+2j)

>>> int(3.5)
3

>>> float(False)
0.0

>>> bool(2)
True

>>> complex(42)
(42+0j)

Underscores (_) can be used as a delimiter to group digits for better readability.1 This is usually used to indicate thousands, but in fact underscores can be used anywhere between digits.

Listing 151 Python shell#
>>> 1_000_000
1000000

>>> 0.001_001
0.001001

>>> 1_1_1_1
1111

>>>  3.14_15_93j
3.141593j

Part 1.1: Operators#

The following operators are available to all numeric types.

Table 4 Arithmetic operators#

Sign

Meaning

Example

+

addition

1+2

-

subtraction

2-1

*

multiplication

2*2

/

division

4/2

%

modulus (remainder)

5%2

**

exponent (power)

2**4

//

floor division (quotient)

5//2

Table 5 Unary sign operators#

Sign

Meaning

Example

-

negative

-1

+

positive or unchanged

+1

Part 1.2: Functions#

Numeric types can all be used as arguments to the following built-in functions.

  • abs(x) – Return the absolute value of x.

    >>> abs(-100)
    100
    
    >>> abs(-0.5)
    0.5
    
    >>> abs(100)
    100
    
    >>> abs(0.5)
    0.5
    
  • divmod(x, y) – Return the tuple containing the quotient and remainder of x divided by y. Equivalent to (x//y, x%y).

    >>> divmod(9, 2)
    (4, 1)
    
  • pow(base, exp, mod=None) – Return base to the power of exp. (Equivalent to base**exp.) If mod is present, return base to the power exp, modulo mod. (Equivalent to base**exp % mod.)

    >>> pow(3, 4)
    81
    
    >>> pow(3, 4, 10)
    1
    
  • round(number, ndigits=None) – Round a number to ndigits precision, or nearest integer if ndigits is None.

    >>> round(3.75)
    4
    
    >>> math.pi
    3.141592653589793
    
    >>> round(3.14159, 2)
    3.14
    

Part 2: Integers#

An integer is a whole number with no decimal places.

Type

int

Base

numbers.Integral

Listing 152 Python shell#
>>> -10
-10

>>> 1_000_000
1000000

>>> int("25")
25

Part 2.1: Alternate bases#

The decimal is base 10, which means it uses ten individual digits to represent all numbers. This is the most commonly used system and the one we use in our daily lives.

Python also makes it easy to use other bases. In particular, there is built in syntax and functions for hexadecimal (base-16), octal (base-8), and binary (base-2).

System

Function

Base

Prefix

Example

hexadecimal

hex()

16

0x or 0X

0x12

octal

oct()

8

0o or 0O

0o12

binary

bin()

2

0b or 0B

0b10

Listing 153 Python shell#
>>> 0xff
255

>>> hex(255)
'0xff'

>>> 0o74
60

>>> oct(60)
'0o74'

>>> 0b110110
54

>>> bin(54)
'0b110110'

Moreover, the int() type takes an optional keyword argument base that can be used to convert to any base.

Listing 154 Python shell#
>>> int('C', base=36)
12

>>> int('12', base=6)
8

Part 3: Boolean#

The boolean data type has just two values: True and False.

Type

bool

Base

numbers.Integral

Listing 155 Python shell#
>>> True
True

>>> False
False

>>> bool(0)
False

While the boolean type is in some ways the simplest, is is also one of the most fundamental.

It is the type that is returned when we use comparison operators:

Listing 156 Python shell#
>>> 1 == 2
False

It is the type that expressions are converted to to determine their truthiness.

Listing 157 Python shell#
>>> bool("")
False

>>> bool("a")
True

>>> bool([])
False

Which is what is used when evaluating a conditional statement. For example, the following two are equivalent.

if []:
  # do stuff
if bool([]):
  # do stuff

Under the hood True is equal to 1 and False is equal to 0. Since 1 and 0 are integers, the bool type is just a special kind of integer. That means that we can usually use booleans as numbers for all intents and purposes.

Listing 158 Python shell#
>>> True + True + True
3

>>> words = ["No", "Yes"]

>>> words[False]
'No'

>>> words[True]
'Yes'

Part 4: Floating point numbers#

Floating point values are numbers with a decimal point.

Type

float

Base

numbers.Real

>>> 1.
1.0

>>> .2
0.2

>>> float(1)
1.0

Part 4.1: Members#

  • .is_integer() – Return True if the float is a whole number.

    >>> f = .25
    
    >>> f.is_integer()
    False
    
    >>> f = 5.0
    
    >>> f.is_integer()
    True
    
  • .as_integer_ratio() – Return a tuple containing the numerator and denominator.

    >>> f = .25
    
    >>> f.as_integer_ratio()
    (1, 4)
    

Part 4.2: E notation#

Exponent notation or E notation is a shorthand to express very large or very small numbers by using the letter E to mean “times 10 to the power of”.

For example, 8e4 means “8 times 10 to the power of 4” (or 8.0 * 10**4) which can also be expressed as 80000.0.

Python supports E notation syntax for float values using with either a capital or lowercase E. Large or small numbers may also be displayed in E notation.

>>> 3e5
300000.0

>>> 1E-4
0.0001

>>> 10000000000000000.0
1e+16

Part 4.3: Limitations#

Float values have limitations depending on your system. You can look at sys.float_info to see the limitations of your system.

Listing 159 Python shell#
>>> import sys

>>> sys.float_info.max
1.7976931348623157e+308

>>> sys.float_info.min
2.2250738585072014e-308

>>> sys.float_info.dig
15

If you attempt to create a float outside of your systems limitations, you will get the special float value inf for infinity.

Listing 160 Python shell#
>>> 2e400
inf

The limitations inherent to the way float values are stored can cause unexpected results.

Listing 161 Python shell#
>>> 0.1 + 0.2
0.30000000000000004

Python does provide ways to do exact decimal point math that behaves as expected which we’ll learn about in a future math lesson. But the simple answer for now is to use the round() function.

Listing 162 Python shell#
>>> round(0.1 + 0.2, 1)
0.3

Part 5: Complex numbers#

Complex numbers provide support for imaginary numbers, primarily used in scientific, geometry, or calculus calculations.

Type

complex

Base

numbers.Complex

They are comprised of a real component combined with a + to an imaginary component indicated with a j or J. The most commonly syntax is real+imagJ but the order of the real and imaginary parts can be switched.

Listing 163 Python shell#
>>> x = 3+5j

>>> x
(3+5j)

>>> x.real, x.imag
(3.0, 5.0)

>>> y = 5j+3

>>> y
(3+5j)

>>> y.real, y.imag
(3.0, 5.0)

A complex number with a negative imaginary part can be expressed as real-imagj which is the same as real+-imagj.

Listing 164 Python shell#
>>> 3-5j
(3-5j)

>>> 3+-5j
(3-5j)

One with a real part of 0 can be expressed as imagj which is the same as 0+imagj.

Listing 165 Python shell#
>>> x = 5j

>>> x
5j

>>> x.real, x.imag
(0.0, 5.0)

>>> x = 0+5j

>>> x
5j

>>> x.real, x.imag
(0.0, 5.0)

Alternately use the complex() constructor with the arguments real and imag.

Listing 166 Python shell#
>>> a = complex(3, 5)

>>> a
(3+5j)

>>> a.real, a.imag
(3.0, 5.0)

Part 5.1: Members#

  • .conjugate() – Return the complex conjugate

    Listing 167 Python shell#
    >>> x = 3+5j
    >>> x.conjugate()
    (3-5j)
    
  • imag – the imaginary part of a complex number

    Listing 168 Python shell#
    >>> x = 3+5j
    >>> x.imag
    5.0
    
  • real – the real part of a complex number

    Listing 169 Python shell#
    >>> x = 3+5j
    >>> x.real
    3.0
    

Reference#

Glossary#

Numeric Types#

primitive data type#

A built-in type that where each instance contains a single value not made up of any other values. For example int is a primitive type, while list is not.

exponent notation#
E notation#

A compact way to represent very large or very small numbers by using an e to to indicate powers of ten. For example 3e5 indicates 3 times 10 to the power of 5 or 300000.0.

unary operator#

An operator that acts on a single input.

See also#


1

Python 3.6+