Integer & Float: Numbers

02_numbers

Python Numbers

Python has several built-in types for general programming. It also supports custom types known as Classes. Not all classes are technically Types as in Type Theory, but it is a good way to start thinking about them. A true Pythonista can create their own type and define what math means for that type. Basically, we can invent math. More on this in the classes module.

In this section we will focus on the three most common types of numbers in Python:

  • Integer int: Python supports big integers – this is a colossal understatement. Python can handle an integer so big it takes all of your available RAM.

  • Float float: Python supports 64bit floating point approximation of real numbers.

  • Complex complex: Python supports Complex Numbers, like in most engineering fields the letter j is used in Python to distinguish values on the complex plane.

my_int: int = 42
my_float: float = 3.14159265359
my_complex: complex = 2 + 3j

print(my_int)
print(my_float)
print(my_complex)
42
3.14159265359
(2+3j)

In the above example the colon and type name are optional, Python does not enforce these types! The type hints are for human readers, not Python. Future versions of Python may support strict typing.

The code below is effectively the same as above.

my_int = 42
my_float = 3.14159265359
my_complex = 2 + 3j

print(my_int)
print(my_float)
print(my_complex)
42
3.14159265359
(2+3j)
In [0]: