Python Numbers
Here's an overview of the various numeric types in Python, and how to work with them.
Python has three distinct numeric types: integers, floating point numbers, and complex numbers. These are usually referred to as int
, float
, and complex
types. Also, the boolean type is a subtype of the integer type.
int
- Refers to an integer. An integer is a whole number (i.e. not a fraction). Integers can be a positive number, a negative one, or zero. Examples of integers: -3, -2, -1, 0, 1, 2, 3
float
- Refers to a floating point number. Floating point numbers represent real numbers and are written with a decimal point dividing the integer and the fractional parts. Floating point numbers can also be in scientific notation, with E or e indicating the power of 10 (eg,
+1e3
is equivalent to1000.0
). Examples of floats: 1.0, 12.45, 10.4567, -10.0, -20.76789, 64.2e18, -64.2e18. complex
- A complex number takes the form
a + bj
wherea
is a real number andb
is an imaginary number. Each argument an be any numeric type (including complex). The first argument can also be a string (but the second argument can't). Examples: 1.4j, -1.4j, 2+18j, -2.18j, 5.14-7j, 5.14e+45j, -5.14e+45j.
In Python, all numeric types are immutable. If you want to change any part of a number you need to reassign the number itself.
Here's an example of creating some number objects, then printing each number along with its type:
# Create the number objects
a = 1
b = -1
c = 1.0
d = -1.0
e = +2e3
f = -2e3
g = 3.14j
h = -3.14j
# Print the numbers and types (we also use a 'tab' character ("\t") - just to align them)
print(type(a), "\t\t", a)
print(type(b), "\t\t", b)
print(type(c), "\t", c)
print(type(d), "\t", d)
print(type(e), "\t", e)
print(type(f), "\t", f)
print(type(g), "\t", g)
print(type(h), "\t", h)
<class 'int'> 1 <class 'int'> -1 <class 'float'> 1.0 <class 'float'> -1.0 <class 'float'> 2000.0 <class 'float'> -2000.0 <class 'complex'> 3.14j <class 'complex'> (-0-3.14j)
Random Numbers
Python provides various functions that allow you to generate random numbers. These are made possible by the random
module. The random
module must be imported using the import
keyword before you can use any of the random number functions. Here are a few:
xxxxxxxxxx
# Import the 'random' module
import random
# Generate a floating point number between 0.0, 1.0
print(random.random())
# Generate an integer between 1 and 100
print(random.randint(1,100))
# Choose a number from the list
print(random.choice([10, 20, 30, 40, 50, 60, 70, 80, 90]))
# Take a sample of 3 numbers from the list
print(random.sample([10, 20, 30, 40, 50, 60, 70, 80, 90], 3))
# Take a sample of 5 numbers from the list
print(random.sample([10, 20, 30, 40, 50, 60, 70, 80, 90], 5))
0.8296271520534051 34 90 [60, 70, 20] [90, 50, 80, 20, 70]
Of course, this is only an example of the results that could be returned. Seeing as the results are randomly generated, they will be different each time it's run.
Minimum and Maximum Numbers
You can use the min()
and max()
functions to return the smallest or largest number within a group of numbers. You can supply the numbers as multiple parameters or as a list. Like this:
xxxxxxxxxx
# Using multiple parameters
print(max(1, 2, 3))
print(min(1, 2, 3))
# Using a list
a = [11, 22, 33, 44, 55, 66, 77]
print(max(a))
print(min(a))
3 1 77 11
Type Conversion
Python has an inbuilt ability to convert numbers to a single type when performing calculations. For example, say you want to do this:
xxxxxxxxxx
100 + 2.5
These are two different number types. The 100
is an integer while the 2.5
is a float. However, Python can handle this. It will make the resulting number a float.
Here's a demo:
xxxxxxxxxx
a = 100
b = 2.5
c = a + b
print(a, type(a))
print(b, type(b))
print(c, type(c))
100 <class 'int'> 2.5 <class 'float'> 102.5 <class 'float'>
Conversion Functions
You can also use functions such as int()
, float()
, and complex()
to make an explicit conversion between one number type and another.
Here are some examples:
xxxxxxxxxx
a = 100
print(type(a))
print(type(float(a)))
print(type(complex(a)))
print(type(bool(a)))
<class 'int'> <class 'float'> <class 'complex'> <class 'bool'>
Numbering Systems
The decimal numbering system is the most widely used system in the modern world. Also called base-ten, the decimal system has 10 as its base, and uses the digits 0 to 9.
There are other numbering systems though, that don't use 10 as its base. The binary system is base-two (uses the digits 1 and 0), the octal system is base-eight (uses digits 0 to 7), and the hexadecimal system is base-sixteen (uses digits 0 to 9, and letters A to F). These numbering systems tend to be more popular in mathematics and computing.
In Python, you can specify the numbering system a number uses by using a two-digit prefix as follows:
Numbering System | Prefix |
---|---|
Binary | 0b or 0B |
Octal | 0o or 0O |
Hexadecimal | 0x or 0X |
The prefix can be uppercase or lowercase.
Here's an example of printing out various numbers from different numbering systems. Although we provide the numbers in binary, octal, and hexadecimal, the output is in decimal.
xxxxxxxxxx
# Binary
print(0b11)
# Octal
print(0o15)
# Hexadecimal
print(0xe)
3 13 14
The following example prints out a range of numbers from each system. You can see how each value in the code maps to a base-ten number in the output:
xxxxxxxxxx
# Binary
print("...", 0b001, 0b010, 0b011, 0b100, 0b101, "...")
# Octal
print("...", 0o05, 0o06, 0o07, 0o10, 0o11, 0o12, 0o13, 0o14, 0o15, "...")
# Hexadecimal
print("...", 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x12, 0x13, "...")
... 1 2 3 4 5 ... ... 5 6 7 8 9 10 11 12 13 ... ... 7 8 9 10 11 12 13 14 15 16 18 19 ...
You can also use functions such as hex()
and oct()
to return an integer as a hexadecimal or octal number.
xxxxxxxxxx
a = 100
print(oct(a))
print(hex(a))
0o144 0x64