6.1.3 Data Types
In a computer program, every piece of data, compound or atomic, has a data type. For one last moment, let us recall that data in a computer is, at a low level of abstraction that we don’t usually worry about, made up of binary 0’s and 1’s (we call such numbers bits). The data type of a piece of data tells the computer how to interpret those bits. For example, one can interpret the same sequence of 0’s and 1’s as a character (or letter) from your keyboard, or as a whole or fractional number. The computer needs to know which interpretation to choose, so that’s why Python and most other programming languages have a notion of data types.
Atomic Data Types
In this section, we describe the most commonly used atomic data types in Python.
- Integer data are whole numbers, that is, numbers without a fractional component. Integers include both positive and negative numbers, as well as the number zero. In Python, there is no limit to the size of an integer number.
- Floating-point data are real numbers, that is, numbers that are not necessarily whole numbers, such as the number 42.5. Floating-point data in Python, and any other language, have limited precision, and limited range, meaning that numbers with infinite representations, such as 1/3=0.33333..., √2, or π, cannot be represented exactly, and we can’t represent numbers that are too big either. In Python, floating-point numbers can range between 10-308 to 10308 (positive or negative) with at most 16 to 17 digits of precision. For brevity, we will sometimes refer to floating-point numbers simply as floats.
- Boolean data can only be one of two values: True or False. Note again that capitalization matters – true and false are not valid boolean values.
Compound Data Types
In this section, we briefly describe some of the standard compound data types built into Python. However, we will save the details of most of these until later chapters.
- Strings are sequences of characters (e.g. letters of the alphabet, digits, and punctuation) and are usually used to store text. We’ll say more about strings and characters later in this chapter.
- Lists are a sequence of data items. Each item in a list can be of any data type.
- A dictionary consists of key-value pairs in no particular order. You can look up values by their key.