Before you can make games, you need to learn a few basic programming concepts. You’ll start in this chapter by learning how to use Python’s interactive shell and perform basic arithmetic.
Start IDLE by following the steps in “Starting IDLE” on page xxvi. First you’ll use Python to solve some simple math problems. The interactive shell can work just like a calculator. Type 2 + 2 into the interactive shell at the >>> prompt and press ENTER. (On some keyboards, this key is RETURN.) Figure 1-1 shows how this math problem looks in the interactive shell—notice that it responds with the number 4.
Figure 1-1: Entering 2 + 2 into the interactive shell
This math problem is a simple programming instruction. The plus sign (+) tells the computer to add the numbers 2 and 2. The computer does this and responds with the number 4 on the next line. Table 1-1 lists the other math symbols available in Python.
Operator |
Operation |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
The minus sign (-) subtracts numbers, the asterisk (*) multiplies numbers, and the slash (/) divides numbers. When used in this way, +, -, *, and / are called operators. Operators tell Python what to do with the numbers surrounding them.
Integers (or ints for short) are whole numbers such as 4, 99, and 0. Floating-point numbers (or floats for short) are fractions or numbers with decimal points like 3.5, 42.1, and 5.0. In Python, 5 is an integer, but 5.0 is a float. These numbers are called values. (Later we will learn about other kinds of values besides numbers.) In the math problem you entered in the shell, 2 and 2 are integer values.
The math problem 2 + 2 is an example of an expression. As Figure 1-2 shows, expressions are made up of values (the numbers) connected by operators (the math signs) that produce a new value the code can use. Computers can solve millions of expressions in seconds.
Figure 1-2: An expression is made up of values and operators.
Try entering some of these expressions into the interactive shell, pressing ENTER after each one:
>>> 2+2+2+2+2
10
>>> 8*6
48
>>> 10-5+6
11
>>> 2 + 2
4
These expressions all look like regular math equations, but notice all the spaces in the 2 + 2 example. In Python, you can add any number of spaces between values and operators. However, you must always start instructions at the beginning of the line (with no spaces) when entering them into the interactive shell.
When a computer solves the expression 10 + 5 and returns the value 15, it has evaluated the expression. Evaluating an expression reduces the expression to a single value, just like solving a math problem reduces the problem to a single number: the answer. For example, the expressions 10 + 5 and 10 + 3 + 2 both evaluate to 15.
When Python evaluates an expression, it follows an order of operations just like you do when you do math. There are just a few rules:
• Parts of the expression inside parentheses are evaluated first.
• Multiplication and division are done before addition and subtraction.
• The evaluation is performed left to right.
The expression 1 + 2 * 3 + 4 evaluates to 11, not 13, because 2 * 3 is evaluated first. If the expression were (1 + 2) * (3 + 4) it would evaluate to 21, because the (1 + 2) and (3 + 4) inside parentheses are evaluated before multiplication.
Expressions can be of any size, but they will always evaluate to a single value. Even single values are expressions. For example, the expression 15 evaluates to the value 15. The expression 8 * 3 / 2 + 2 + 7 - 9 will evaluate to the value 12.0 through the following steps:
Even though the computer is performing all of these steps, you don’t see them in the interactive shell. The interactive shell shows you just the result:
>>> 8 * 3 / 2 + 2 + 7 - 9
12.0
Notice that expressions with the / division operator always evaluate to a float; for example, 24 / 2 evaluates to 12.0. Math operations with even one float value also evaluate to float values, so 12.0 + 2 evaluates to 14.0.
If you enter 5 + into the interactive shell, you’ll get the following error message:
>>> 5 +
SyntaxError: invalid syntax
This error happened because 5 + isn’t an expression. Expressions have values connected by operators, and the + operator expects a value before and after it. An error message appears when an expected value is missing.
SyntaxError means Python doesn’t understand the instruction because you typed it incorrectly. Computer programming isn’t just about giving the computer instructions to follow but also knowing how to give it those instructions correctly.
Don’t worry about making mistakes, though. Errors won’t damage your computer. Just retype the instruction correctly into the interactive shell at the next >>> prompt.
When an expression evaluates to a value, you can use that value later by storing it in a variable. Think of a variable as a box that can hold a value.
An assignment statement will store a value inside a variable. Type a name for the variable, followed by the equal sign (=), which is called the assignment operator, and then the value to store in the variable. For example, enter the following into the interactive shell:
>>> spam = 15
>>>
The spam variable’s box now stores the value 15, as shown in Figure 1-3.
Figure 1-3: Variables are like boxes that can hold values.
When you press ENTER, you won’t see anything in response. In Python, you know the instruction was successful if no error message appears. The >>> prompt will appear so you can enter the next instruction.
Unlike expressions, statements are instructions that do not evaluate to any value. This is why there’s no value displayed on the next line in the interactive shell after spam = 15. If you’re confused about which instructions are expressions and which are statements, remember that expressions evaluate to a single value. Any other kind of instruction is a statement.
Variables store values, not expressions. For example, consider the expressions in the statements spam = 10 + 5 and spam = 10 + 7 - 2. They both evaluate to 15. The end result is the same: both assignment statements store the value 15 in the variable spam.
A good variable name describes the data it contains. Imagine that you moved to a new house and labeled all of your moving boxes Stuff. You’d never find anything! The variable names spam, eggs, and bacon are example names used for variables in this book.
The first time a variable is used in an assignment statement, Python will create that variable. To check what value is in a variable, enter the variable name into the interactive shell:
>>> spam = 15
>>> spam
15
The expression spam evaluates to the value inside the spam variable: 15.
You can also use variables in expressions. Try entering the following in the interactive shell:
>>> spam = 15
>>> spam + 5
20
You set the value of the variable spam to 15, so typing spam + 5 is like typing the expression 15 + 5. Here are the steps of spam + 5 being evaluated:
You cannot use a variable before an assignment statement creates it. If you try to do so, Python will give you a NameError because no such variable by that name exists yet. Mistyping the variable name also causes this error:
>>> spam = 15
>>> spma
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
spma
NameError: name 'spma' is not defined
The error appeared because there’s a spam variable but no spma variable.
You can change the value stored in a variable by entering another assignment statement. For example, enter the following into the interactive shell:
>>> spam = 15
>>> spam + 5
20
>>> spam = 3
>>> spam + 5
8
When you first enter spam + 5, the expression evaluates to 20 because you stored 15 inside spam. However, when you enter spam = 3, the value 15 in the variable’s box is replaced, or overwritten, with the value 3 since the variable can hold only one value at a time. Because the value of spam is now 3, when you enter spam + 5, the expression evaluates to 8. Overwriting is like taking a value out of the variable’s box to put a new value in, as shown in Figure 1-4.
Figure 1-4: The value 15 in spam is overwritten by the value 3.
You can even use the value in the spam variable to assign a new value to spam:
>>> spam = 15
>>> spam = spam + 5
20
The assignment statement spam = spam + 5 says, “The new value of the spam variable will be the current value of spam plus five.” To keep increasing the value in spam by 5 several times, enter the following into the interactive shell:
>>> spam = 15
>>> spam = spam + 5
>>> spam = spam + 5
>>> spam = spam + 5
>>> spam
30
In this example, you assign spam a value of 15 in the first statement. In the next statement, you add 5 to the value of spam and assign spam the new value spam + 5, which evaluates to 20. When you do this three times, spam evaluates to 30.
So far we’ve looked at just one variable, but you can create as many variables as you need in your programs. For example, let’s assign different values to two variables named eggs and bacon, like so:
>>> bacon = 10
>>> eggs = 15
Now the bacon variable has 10 inside it, and the eggs variable has 15 inside it. Each variable is its own box with its own value, as shown in Figure 1-5.
Figure 1-5: The bacon and eggs variables each store values.
Enter spam = bacon + eggs into the interactive shell, then check the new value of spam:
>>> bacon = 10
>>> eggs = 15
>>> spam = bacon + eggs
>>> spam
25
The value in spam is now 25. When you add bacon and eggs, you are adding their values, which are 10 and 15, respectively. Variables contain values, not expressions, so the spam variable was assigned the value 25, not the expression bacon + eggs. After the spam = bacon + eggs statement assigns the value 25 to spam, changing bacon or eggs will not affect spam.
In this chapter, you learned the basics of writing Python instructions. Because computers don’t have common sense and only understand specific instructions, Python needs you to tell it exactly what to do.
Expressions are values (such as 2 or 5) combined with operators (such as + or -). Python can evaluate expressions—that is, reduce the expression to a single value. You can store values inside of variables so that your program can remember those values and use them later.
There are a few other types of operators and values in Python. In the next chapter, you’ll go over some more basic concepts and write your first program. You’ll learn about working with text in expressions. Python isn’t limited to just numbers; it’s more than a calculator!