Types

We have seen that Python objects have a ‘type’:

type(5)

int

Floats and integers

Python has two core numeric types, int for integer, and float for real number.

one=1
ten=10
one_float=1.0
ten_float=10.

Zero after a point is optional, but the Dot makes it a float.

Lets look at some operations:

tenth=one_float/ten_float
tenth

0.1

type(one)

int

type(one_float)

float

The meaning of an operator varies depending on the type it is applies to! (And on the Python version.)

print(one//ten)

0

one_float/ten_float)

0.1

print(type(one/ten))

<class ‘float’>

type(tenth)

float

The divided by operator // when applied to floats, means divide by for real numbers.

But when applied to integers, it means divide then round down:

Examples:

10//3

3

10.0/3

3.3333333333333335

10/3.0

3.3333333333333335

10//3.0

3.0

So if I have two integer variables, and I want the float division, I need to change the type first.

There is a function for every type name, which is used to convert the input to an output of the desired type.

x = float(5)
type(x)

float

10/float(3)

3.3333333333333335

I lied when I said that the float type was a real number. It’s actually a computer representation of a real number called a “floating point number”.

Representing or perfectly would be impossible in a computer, so we use a finite amount of memory to do it.

Supplementary material:

Next: Reading - Strings