Types

All values in programming have types:

  • Integer: -1, 0, 1, 2, 3, 4, 5...
  • Float: -12.3, 0.001, 3.14159, 5.0
  • String: 'Helloooo Newman'
  • List: [1, 2, 3, 4, 5]

These types are important, because they tell the program how to act.

For example, the plus sign does different things to strings and numbers.

In [1]:
'Brad' + 'Pitt'
Out[1]:
'BradPitt'
In [2]:
75 + 2
Out[2]:
77

Types can also cause confusion.

In [3]:
15.0 / 2
Out[3]:
7.5
In [4]:
15 / 2
Out[4]:
7

The surprising result that 15 / 2 = 7 happens because computer folks like commands that involve two objects of the same type to result in another object of that type, but in science we really want 15 / 2 = 7.5. The easy way to make sure that this happens is to add a special line to the beginning of any program that will do math.

In [5]:
from __future__ import division
15 / 2
Out[5]:
7.5

This line will make a little more sense once you learn about modules, but basically it tells Python to use the kind of division that the newest version of Python uses, which is that one that makes sense to everyone but computer scientists.

A full list of Python's built-in types is available at: http://docs.python.org/library/types.html