Python has four built-in numeric data types, as shown in Table 3-1.
Type (keyword) |
Example |
Used for... |
Plain integers (int) and long integers (long) |
7 6666666666L |
Whole numbers (long integers are very large whole numbers.) |
Floating point numbers (float) |
1.1714285714285713 |
Real numbers |
Complex numbers (complex) |
(3+4j) |
Imaginary numbers |
Decimal numbers (decimal.Decimal) |
decimaLDecimal("18L2") |
Calculations requiring decimal arithmetic |
Except when you're doing division with integers or using the decimal module (see Chapter 7), you don't have to worry about what kind of number data type you're using. Python converts numbers into compatible types automatically. For example, if you multiply an integer and a floating point number, Python automatically gives the answer as a floating point number:
>>> |
y = |
1.5 |
>>> |
x * |
y |
7.5 |
For more information about numbers and number data types, see Chapter 7. Sequential data
Sequential data types contain multiple pieces of data, each of which is numbered, or indexed. Each piece of data inside a sequence is called an element.
REMEMBER The cool thing about sequential data types is that you can manipulate the whole sequence, chunks of the sequence, or individual elements inside the sequence.
Three sequential data types are built into Python:
• Lists can store multiple kinds of data (both text and numbers, for example). You can change elements inside a list, and you can organize the data in various ways (for example, by sorting).
• Tuples, like lists, can include different kinds of data, but they can't be changed. In Python terminology, they are immutable.
• Strings store text or binary data. Strings are immutable (like tuples).
Table 3-2 introduces Python's built-in sequential data types.
Type (Name) |
Kind |
Example |
Used for... | |||
str (String) |
Immutable |
x = "monty python" |
(Tuple) |
Immutable |
x = ("a", 2, "33") |
Storing a set of items you want fast access to |
list (List) |
Mutable |
x = ["here", "is", "my", "list", 47] |
Storing a set of items you want to be able to change readily |
To see the data type of a Python object, use the type() function, like this:
Was this article helpful?
Post a comment