Chapter 3. Numbers

That action is best which procures the greatest happiness for the greatest numbers.

Francis Hutcheson

In this chapter we begin by looking at Python’s simplest built-in data types:

  • Booleans (which have the value True or False)

  • Integers (whole numbers such as 42 and 100000000)

  • Floats (numbers with decimal points such as 3.14159, or sometimes exponents like 1.0e8, which means one times ten to the eighth power, or 100000000.0)

In a way, they’re like atoms. We use them individually in this chapter, and in later chapters you’ll see how to combine them into larger “molecules” like lists and dictionaries.

Each type has specific rules for its usage and is handled differently by the computer. I also show how to use literal values like 97 and 3.1416, and the variables that I mentioned in Chapter 2.

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.

Booleans

In Python, the only values for the boolean data type are True and False. Sometimes, you’ll use these directly; other times you’ll evaluate the “truthiness” of other types from their values. The special Python function bool() can convert any Python data type to a boolean.

Functions get their own chapter in Chapter 9, but for now you just need to know that ...

Get Introducing Python, 2nd Edition 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.