Skip to Content
Introducing Python, 2nd Edition
book

Introducing Python, 2nd Edition

by Bill Lubanovic
November 2019
Beginner
630 pages
12h 20m
English
O'Reilly Media, Inc.
Book available
Content preview from Introducing Python, 2nd Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Fluent Python, 2nd Edition

Fluent Python, 2nd Edition

Luciano Ramalho

Publisher Resources

ISBN: 9781492051374Errata PageSupplemental Content