Chapter Two · The Ground Floor
Values and Names
Every program works with values: numbers, text, and the answers yes and no. This chapter shows what kinds of value Python has, how to give a value a name so you can use it again, where arithmetic surprises people (money especially), how to build text for a person to read, and how to ask the person at the keyboard a question.
The first chapter ended with a program that printed three fixed sentences. That is a start, but a program that always says the same thing is closer to a poster than a tool. To do anything useful, a program has to hold information, work with it, and change it. This chapter builds that ground floor.
Five topics. The four basic kinds of value and how to tell them apart. Names, which let you keep a value and use it later, and a picture of how they work that the rest of the book relies on. Arithmetic, including the one surprise that matters for anyone who handles money. Text, and the modern way to put values into a sentence. And finally input, which lets a program ask a question and use the answer, together with the first crash nearly every beginner meets.
Topics in This Chapter
"5" and 5 look alike and behave completely differently.0.1 + 0.2 is not quite 0.3, which is why money uses Decimal.input always gives back text. Converting it to a number, and the first crash every beginner meets, on purpose.