2. Values, Expressions, And Statements — Beginning Python ...

2.1. Programs and data¶

We can restate our previous definition of a computer program colloquially:

A computer program is a set of instructions to tell a computer to do things to stuff.

We will be spending the rest of this book deepening and refining our understanding of exactly what kinds of things a computer can do. Your ability to program a computer effectively will depend in large part on your ability to understand these things well, so that you can express what you want to accomplish in a language the computer can execute.

Before we get to that, however, we need to talk about the stuff on which computers operate.

Computer programs operate on data. A single piece of data can be called a datum, but we will use the related term, value.

A value is one of the fundamental things — like a letter or a number — that a program manipulates. The values we have seen so far are 4 (the result when we added 2 + 2), and "Hello, World!".

Values are grouped into different data types or classes.

Note

As mentioned last chapter in the What is a computer? section, at the level of the hardware of the machine, all values are stored as a sequence of bits, usually represented by the digits 0 and 1. All computer data types, whether they be numbers, text, images, sounds, or anything else, ultimately reduce to an interpretation of these bit patterns by the computer.

Thankfully, high-level languages like Python give us flexible, high-level data types which abstract away the tedious details of all these bits and better fit our human brains.

4 is an integer, and "Hello, World!" is a string, so-called because it contains a string of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks.

If you are not sure what class a value falls into, Python has a function called type which can tell you.

>>> type("Hello, World!") <class 'str'> >>> type(17) <class 'int'>

Not surprisingly, strings belong to the class str and integers belong to the class int. Less obviously, numbers with a point between their whole number and fractional parts belong to a class called float, because these numbers are represented in a format called floating-point. At this stage, you can treat the words class and type interchangeably. We’ll come back to a deeper understanding of what a class is in later chapters.

>>> type(3.2) <class 'float'>

What about values like "17" and "3.2"? They look like numbers, but they are in quotation marks like strings.

>>> type("17") <class 'str'> >>> type("3.2") <class 'str'>

They are strings!

Don’t use commas in ints

When you type a large integer, you might be tempted to use commas between groups of three digits, as in 42,000. This is not a legal integer in Python, but it does mean something else, which is legal:

>>> 42000 42000 >>> 42,000 (42, 0)

Well, that’s not what we expected at all! Because of the comma, Python treats this as a pair of values in a tuple. So, remember not to put commas or spaces in your integers. Also revisit what we said in the previous chapter: formal languages are strict, the notation is concise, and even the smallest change might mean something quite different from what you intended.

Tag » What Are Expressions In Python