Python Essentials
上QQ阅读APP看书,第一时间看更新

Chapter 2. Simple Data Types

Now we'll look at a number of data types that are built-in as well as some that are part of Python's standard library. We'll start with Python's numeric types. These include three built-in types: int, float, and complex, plus the standard library types Fraction and Decimal.

We'll also look at strings, str, and simple collections, tuple. These are more complex than numbers because they contain multiple items. Since their behavior is less complex than the kinds of objects we'll see in later chapters, they serve as a good introduction to the general concept of sequences in Python.

Note the capitalization of the names of Fraction and Decimal. The built-in type names start with a lowercase letter. Types that we must import have a module name that starts with a lowercase letter, but the type name starts with a capital letter. This convention is widespread, but not universal.

All of the types we'll look at in this chapter have the common feature of immutability. This concept applies to the two collections we'll look at: once built, a string or a tuple cannot be changed. Rather than change it, we create a new object. In Chapter 6, More Complex Data Types, we'll look at collections which can be updated without creating a new object.

In this chapter, we'll look at the built-in functions for converting to and from string representations. This will help us when displaying output or converting input from a string to a useful Python object.

Note that we're continuing to play fast and loose with formal Python syntax. We'll defer a detailed examination of the syntax rules until Chapter 3, Expressions and Output. For now, the kinds of simple expression statements we're focusing on must be restricted to a single line.