2 Variables Operators Expressions
2 Variables Operators Expressions
Before we start writing computer games, we should learn some basic programming
concepts first. These concepts are values, operators, expressions, and variables. We won't
start programming in this chapter, but knowing these concepts and the names of things will
make learning to program much easier. This is because most programming is built on only
a few simple concepts combined together to make advanced programs.
Let's start by learning how to use Python's interactive shell.
As you can see, we can use the Python shell just like a calculator. This isn't a program by
itself because we are just learning the basics right now. The + sign tells the computer to add
the numbers 2 and 2. To subtract numbers use the - sign, and to multiply numbers use an
asterisk (*), like so:
Table 2-1: The various math
operators in Python.
2+2
addition
2-2
subtraction
2*2
multiplication
2/2
division
When used in this way, +, -, *, and / are called operators because they tell the
computer to perform the specified operation on the numbers surrounding them.
Expressions
Try typing some of these math problems into the shell, pressing Enter key after each one.
2+2+2+2+2
8*6
10-5+6
2 +
Figure 2-2 is what the interactive shell in IDLE will look like after you type in the
instructions above.
Figure 2-2: What the IDLE window looks like after entering instructions.
This is like how a cat is a type of pet, but not all pets are cats. Someone could have a pet
dog or a pet lizard. An expression is made up of values (such as integers like 8 and 6)
connected by an operator (such as the * multiplication sign). A single value by itself is also
considered an expression.
In the next chapter, we will learn about working with text in expressions. Python isn't
limited to just numbers. It's more than just a fancy calculator!
Evaluating Expressions
When a computer solves the expression 10 + 5 and gets the value 15, we say 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.
The expressions 10 + 5 and 10 + 3 + 2 have the same value, because they both
evaluate to 15. Even single values are considered expressions: The expression 15 evaluates
to the value 15.
However, if you just type 5 + into the interactive shell, you will get an error message.
>>> 5 +
SyntaxError: invalid syntax
This error happened because 5 + is not an expression. Expressions have values
connected by operators, but the + operator always expects to connect two things in Python.
We have only given it one. This is why the error message appeared. A syntax error means
that the computer does not understand the instruction you gave it because you typed it
incorrectly. Python will always display an error message if you enter an instruction that it
cannot understand.
This may not seem important, but a lot of computer programming is not just telling the
computer what to do, but also knowing exactly how to tell the computer to do it.
The first time you store a value inside a variable by using an assignment statement,
Python will create that variable. Each time after that, an assignment statement will only
replace the value stored in the variable.
Now let's see if we've created our variable properly. If we type spam into the shell by
itself, we should see what value is stored inside the variable spam.
>>> spam = 15
>>> spam
15
>>>
Now, spam evaluates to the value inside the variable, 15.
And here's an interesting twist. If we now enter spam + 5 into the shell, we get the
integer 20, like so.
>>> spam = 15
>>> spam + 5
20
>>>
That may seem odd but it makes sense when we remember that we set the value of spam
to 15. Because we've set the value of the variable spam to 15, writing spam + 5 is like
writing the expression 15 + 5.
If you try to use a variable before it has been created, Python will give you an error
because no such variable would exist yet. This also happens if you mistype the name of the
variable.
We can change the value stored in a variable by entering another assignment statement.
For example, try the following:
>>>
>>>
20
>>>
>>>
8
>>>
spam = 15
spam + 5
spam = 3
spam + 5
The first time we enter spam + 5, the expression evaluates to 20, because we stored
13
the value 15 inside the variable spam. But when we enter spam = 3, the value 15 is
replaced, or overwritten, with the value 3. Now, when we enter spam + 5, the expression
evaluates to 8 because the value of spam is now 3.
To find out what the current value is inside a variable, just enter the variable name into
the shell.
Now here's something interesting. Because a variable is only a name for a value, we can
write expressions with variables like this:
>>> spam = 15
>>> spam + spam
30
>>> spam - spam
0
>>>
When the variable spam has the integer value 15 stored in it, entering spam + spam
is the same as entering 15 + 15, which evaluates to 30. And spam - spam is the same
as 15 - 15, which evaluates to 0. The expressions above use the variable spam twice.
You can use variables as many times as you want in expressions. Remember that Python
will evaluate a variable name to the value that is stored inside that variable, each time the
variable is used.
We can even use the value in the spam variable to assign spam a new value:
>>> spam = 15
>>> spam = spam + 5
20
>>>
The assignment statement spam = spam + 5 is like saying, "the new value of the
spam variable will be the current value of spam plus five." Remember that the variable on
the left side of the = sign will be assigned the value that the expression on the right side
evaluates to. We can also keep increasing the value in spam by 5 several times:
>>>
>>>
>>>
>>>
>>>
30
14
spam
spam
spam
spam
spam
=
=
=
=
15
spam + 5
spam + 5
spam + 5
>>>
Overwriting Variables
Changing the value stored inside a variable is easy. Just perform another assignment
statement with the same variable. Look what happens when you enter the following code
into the interactive shell:
>>> spam = 42
>>> print(spam)
42
>>> spam = 'Hello'
>>> print(spam)
Hello
Initially, the spam variable had the integer 42 placed inside of it. This is why the first
print(spam) prints out 42. But when we execute spam = 'Hello', the 42 value is
tossed out of the variable and forgotten as the new 'Hello' string value is placed inside
the spam variable.
Replacing the value in a variable with a new value is called overwriting the value. It is
important to know that the old value is permanently forgotten. If you want to remember this
value so you can use it later in your program, store it in a different variable before
overwriting the value:
>>> spam = 42
>>> print(spam)
42
>>> oldSpam = spam
>>> spam = 'Hello'
>>> print(spam)
Hello
>>> print(oldSpam)
42
In the above example, before overwriting the value in spam, we store that value in a
variable named oldSpam.
For example, let's assign different values to two variables named eggs and fizz, like
so:
>>> fizz = 10
>>> eggs = 15
Now the fizz variable has 10 inside it, and eggs has 15 inside it.
Figure 2-5: The "fizz" and "eggs" variables have values stored in them.
Without changing the value in our spam variable, let's try assigning a new value to the
spam variable. Enter spam = fizz + eggs into the shell then enter spam into the
shell to see the new value of spam. Can you guess what it will be?
>>>
>>>
>>>
>>>
25
>>>
fizz = 10
eggs = 15
spam = fizz + eggs
spam
The value in spam is now 25 because when we add fizz and eggs we are adding the
values stored inside fizz and eggs.
Summary
In this chapter you learned the basics about writing Python instructions. Python needs
you to tell it exactly what to do in a strict way, because computers don't have common
sense and only understand very simple instructions. You have learned that Python can
16
evaluate expressions (that is, reduce the expression to a single value), and that
expressions are values (such as 2 or 5) combined with operators (such as + or -). You have
also learned that you can store values inside of variables in order to use them later on.
In the next chapter, we will go over some more basic concepts, and then you will be
ready to program!
17