Numeric data

Python has four built-in numeric data types, as shown in Table 3-1.

Table 3-1: Python's Built-In Numeric Data Types "=X)pen table as spreadsheet

Type (keyword)


Used for...

Plain integers (int) and long integers (long)

7 6666666666L

Whole numbers (long integers are very large whole numbers.)

Floating point numbers



Real numbers

Complex numbers (complex)


Imaginary numbers

Decimal numbers



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 =



x *



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.

Table 3-2: Python's Built-in Sequence Data Types ■=>Open table as spreadsheet

Type (Name)



Used for...

str (String)


x = "monty python"



x = ("a", 2, "33")

Storing a set of items you want fast access to

list (List)


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?

0 0

Post a comment