Chapter 2. Py Ingredients: Numbers, Strings, and Variables
In this chapter we’ll begin by looking at Python’s simplest built-in data types:
-
booleans (which have the value
True
orFalse
) -
integers (whole numbers such as
42
and100000000
) -
floats (numbers with decimal points such as
3.14159
, or sometimes exponents like1.0e8
, which means one times ten to the eighth power, or100000000.0
) -
strings (sequences of text characters)
In a way, they’re like atoms. We’ll use them individually in this chapter. Chapter 3 shows how to combine them into larger “molecules.”
Each type has specific rules for its usage and is handled differently by the computer. We’ll also introduce variables (names that refer to actual data; more on these in a moment).
The code examples in this chapter are all valid Python, but they’re snippets.
We’ll be using the Python interactive interpreter,
typing these snippets and seeing the results immediately.
Try running them yourself with the version of Python on your computer.
You’ll recognize these examples by the >>>
prompt.
In Chapter 4, we start writing Python programs that can run on their own.
Variables, Names, and Objects
In Python, everything—booleans, integers, floats, strings, even large data structures, functions, and programs—is implemented as an object. This gives the language a consistency (and useful features) that some other languages lack.
An object is like a clear plastic box that contains a piece of data (Figure 2-1). The object has a type ...
Get Introducing Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.