2. Variables, expressions, statements, for loops

2.1. Values and data types

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 2 (the result when we added 1 + 1), and "Hello, World!".

These values belong to different data types: 2 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.

The print statement also works for integers.

>>> print(4)
4

If you are not sure what type a value has, the interpreter can tell you.

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

Not surprisingly, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating-point.

>>> 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’re strings.

Strings in Python can be enclosed in either single quotes (‘) or double quotes (”):

>>> type('This is a string.')
<class 'str'>
>>> type("And so is this.")
<class 'str'>

Double quoted strings can contain single quotes inside them, as in "Bruce's beard", and single quoted strings can have double quotes inside them, as in 'The knights who say "Ni!"'.

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

>>> print(1,000,000)
(1, 0, 0)

Well, that’s not what we expected at all! Python interprets 1,000,000 as a three separate items to be printed. So remember not to put commas in your integers.

2.2. Variables

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.

The assignment statement creates new variables and gives them values:

>>> message = "What's up, Doc?"
>>> n = 17
>>> pi = 3.14159

This example makes three assignments. The first assigns the string "What's up, Doc?" to a new variable named message. The second gives the integer 17 to n, and the third gives the floating-point number 3.14159 to pi.

The assignment operator, =, should not be confused with an equals sign (even though it uses the same character). Assignment operators link a name, on the left hand side of the operator, with a value, on the right hand side. This is why you will get an error if you enter:

>>> 17 = n

A common way to represent variables on paper is to write the name with an arrow pointing to the variable’s value. This kind of figure is called a state diagram because it shows what state each of the variables is in (think of it as the variable’s state of mind). This diagram shows the result of the assignment statements:

State diagram

The print statement also works with variables.

>>> print(message)
What's up, Doc?
>>> print(n)
17
>>> print(pi)
3.14159

In each case the result is the value of the variable. Variables also have types; again, we can ask the interpreter what they are.

>>> type(message)
<class 'str'>
>>> type(n)
<class 'int'>
>>> type(pi)
<class 'float'>

The type of a variable is the type of the value it refers to.

2.3. Variable names and keywords

Programmers generally choose names for their variables that are meaningful — they document what the variable is used for.

Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. Although it is legal to use uppercase letters, by convention we don’t. If you do, remember that case matters. Bruce and bruce are different variables.

The underscore character ( _) can appear in a name. It is often used in names with multiple words, such as my_name or price_of_tea_in_china.

If you give a variable an illegal name, you get a syntax error:

>>> 76trombones = "big parade"
SyntaxError: invalid syntax
>>> more$ = 1000000
SyntaxError: invalid syntax
>>> class = "Computer Science 101"
SyntaxError: invalid syntax

76trombones is illegal because it does not begin with a letter. more$ is illegal because it contains an illegal character, the dollar sign. But what’s wrong with class?

It turns out that class is one of the Python keywords. Keywords define the language’s rules and structure, and they cannot be used as variable names.

Python has thirty-one keywords:

and as assert break class continue
def del elif else except exec
finally for from global if import
in is lambda not or pass
print raise return try while with
yield          

You might want to keep this list handy. If the interpreter complains about one of your variable names and you don’t know why, see if it is on this list.

2.4. Statements

A statement is an instruction that the Python interpreter can execute. We have seen two kinds of statements: print and assignment.

When you type a statement on the command line, Python executes it and displays the result, if there is one. The result of a print statement is a value. Assignment statements don’t produce a result.

A program usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.

For example, the program

print(1)
x = 2
print(x)

produces the output:

1
2

Again, the assignment statement produces no output.

2.5. Evaluating expressions

An expression is a combination of values, variables, and operators. If you type an expression on the command line, the interpreter evaluates it and displays the result:

>>> 1 + 1
2

The evaluation of an expression produces a value, which is why expressions can appear on the right hand side of assignment statements. A value all by itself is a simple expression, and so is a variable.

>>> 17
17
>>> x
2

Confusingly, evaluating an expression is not quite the same thing as printing a value.

>>> message = "What's up, Doc?"
>>> message
"What's up, Doc?"
>>> print(message)
What's up, Doc?

When the Python shell displays the value of an expression, it uses the same format you would use to enter a value. In the case of strings, that means that it includes the quotation marks. But the print statement prints the value of the expression, which in this case is the contents of the string.

In a program, an expression all by itself is a legal statement, but it doesn’t do anything. The program

17
3.2
"Hello, World!"
1 + 1

produces no output at all. How would you change the program to display the values of these four expressions?

2.6. Operators and operands

Operators are special symbols that represent computations like addition and multiplication. The values the operator uses are called operands.

The following are all legal Python expressions whose meaning is more or less clear:

20+32   hour-1   hour*60+minute   minute/60   5**2   (5+9)*(15-7)

The symbols +, -, and /, and the use of parenthesis for grouping, mean in Python what they mean in mathematics. The asterisk (*) is the symbol for multiplication, and ** is the symbol for exponentiation.

When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.

Addition, subtraction, multiplication, division and exponentiation all do what you expect. Sometimes, however, it is useful when dividing two integers together to get an integer result. Compare these two lines:

>>> cookies = 15
>>> cookies/4
3.75
>>> cookies//4
3

The value of cookies is 15, and 15 divided by 4 is 3.75. The / operator gives this result. However, there are times when we want an integer result and Python will provide an integer result if you use the // operator. The // operator always rounds down even if the result is very close to the next higher integer and when the number is negative:

>>> 99//100
0
>>> 15//4
3
>>> -15//4
-4

In each case the result is rounded down. Another alternative is to use floating-point division which you get using the / operator. We’ll see in the Conditionals chapter how to convert integer values and variables to floating-point values.

2.7. Order of operations

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. Python follows the same precedence rules for its mathematical operators that mathematics does. The acronym PEMDAS is a useful way to remember the order of operations:

  1. Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even though it doesn’t change the result.
  2. Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27.
  1. Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than 4, and 2//3-1 is -1, not 1 (remember that in integer division, 2//3=0).
  2. Operators with the same precedence are evaluated from left to right. So in the expression minute*100//60, the multiplication happens first. If minute=59, this would yield 5900//60, which in turn yields 98. If the operations had been evaluated from right to left, the result would have been 59*1, which is 59, which is wrong.

2.8. Operations on strings

In general, you cannot perform mathematical operations on strings, even if the strings look like numbers. The following are illegal (assuming that message has type string):

message-1   "Hello"/123   message*"Hello"   "15"+2

Interestingly, the + operator does work with strings, although it does not do exactly what you might expect. For strings, the + operator represents concatenation, which means joining the two operands by linking them end-to-end. For example:

fruit = "banana"
baked_good = " nut bread"
print(fruit + baked_good)

The output of this program is banana nut bread. The space before the word nut is part of the string, and is necessary to produce the space between the concatenated strings.

The * operator also works on strings; it performs repetition. For example, 'Fun'*3 is 'FunFunFun'. One of the operands has to be a string; the other has to be an integer.

On one hand, this interpretation of + and * makes sense by analogy with addition and multiplication. Just as 4*3 is equivalent to 4+4+4, we expect "Fun"*3 to be the same as "Fun"+"Fun"+"Fun", and it is. On the other hand, there is a significant way in which string concatenation and repetition are different from integer addition and multiplication. Can you think of a property that addition and multiplication have that string concatenation and repetition do not?

2.9. Input

The built-in function input() can be used for getting keyboard input. This function takes one argument (the string used as a prompt to the user) and returns whatever the user enters as a string:

name = input("Please enter your name: ")
print(name)
age = input("Please enter your age: ")
print(age)

A sample run of this program would look something like this:

$ python tryinput.py
Please enter your name: Jeff
Jeff
Please enter your age: 50
50

NOTE: in the above example, the variable age is assigned the string “50”. It’s easy to see this using the interactive python shell and the type() function. If you need an integer or a float, you could convert the given user input:

>>> age = input("How old are you? ")
How old are you? 50
>>> type(age)
<class 'str'>
>>> age = int(age)
>>> type(age)
<class 'int'>
>>>

We will see more examples of this in the Type conversion section of the Conditionals chapter.

2.10. Composition

So far, we have looked at the elements of a program — variables, expressions, and statements — in isolation, without talking about how to combine them.

One of the most useful features of programming languages is their ability to take small building blocks and compose them. For example, we know how to add numbers and we know how to print; it turns out we can do both at the same time:

>>>  print(17 + 3)
20

In reality, the addition has to happen before the printing, so the actions aren’t actually happening at the same time. The point is that any expression involving numbers, strings, and variables can be used inside a print statement. You’ve already seen an example of this:

print("Number of minutes since midnight: ")
print(hour*60+minute)

You can also put arbitrary expressions on the right-hand side of an assignment statement:

percentage = (minute * 100) / 60

This ability may not seem impressive now, but you will see other examples where composition makes it possible to express complex computations neatly and concisely.

Warning: There are limits on where you can use certain expressions. For example, the left-hand side of an assignment statement has to be a variable name, not an expression. So, the following is illegal: minute+1 = hour.

2.11. Comments

As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.

For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they are marked with the # symbol:

# compute the percentage of the hour that has elapsed
percentage = (minute * 100) // 60

In this case, the comment appears on a line by itself. You can also put comments at the end of a line:

percentage = (minute * 100) // 60     # caution: integer division

Everything from the # to the end of the line is ignored — it has no effect on the program. The message is intended for the programmer or for future programmers who might use this code. In this case, it reminds the reader about the ever-surprising behavior of integer division.

2.12. Introduction to lists

So far, we have discussed three of Python’s data types: integers (int), floating-point numbers (float), and strings (str). Python also includes a type called a list which is, appropriately, a list of other values. Lists are created by writing a comma-separated sequence of elements in square brackets. Each of these elements is simply the Python code for another value, like so:

[1, 3, 5, 7]

The above is a list of the first four odd natural numbers. Elements of a list may be any legal Python expression. For instance, all of the following are lists:

[1+2, 3+4, 5+6]      # equivalent to the list [3,7,11]
[1.0, 3.5, 2.0]      # floats may be members of lists
["Ann", "Bjorn"]    # so can strings
[[1, 3, 5], [2, 4, 6]]  # this is a list of lists!

We discuss lists in greater detail later. For now, lists are most interesting because we can “loop over them.” That leads us to our next question...

2.13. Introduction to for loops

The code examples presented so far consist of a series of statements which are executed in order. For instance, consider the following source:

print("How are you today?")
mood = input()
print("Me too!")

When executed, this program will print a prompt (How are you today?), wait for the user to answer the question, and then print a follow-up message. These statements occur in the order they occur in the source code and each of them will only run once. In a sense, the code runs in a straight line from the top of the file to the bottom.

By contrast, a loop is a piece of code which may execute many times (or perhaps even not at all). One such loop is the for loop, an example of which appears in the following code:

for name in ["Alice", "Bjorn", "Cayman", "Duanphen", "Esfir", "Farah"]:
    print("I know " + name + ".")
    print("%s is a friend of mine." % (name))
print("Those are the people I know.")

In the above code, for and in are keywords in the Python language. Between them, name describes the name of a variable. After in, a list must appear, followed by a colon (:). Finally, note that two print statements beneath the for loop are indented, forming what is known as the body of the loop.

When the for loop is executed, Python will run the entire body of the loop once for each element in the for loop’s list. Each time the body runs, the variable (here, name) will be set to that element. So the steps of the above program are:

  1. Set name to "Alice".
  2. Execute both of the statements in the body of the loop.
  3. Set name to "Bjorn".
  4. Execute both of the statements in the body of the loop again.
  5. Continue the above process until we have set name to "Farah" and run the body of the loop.
  6. Having completed the for loop, now execute the final print statement.

The output of the program is therefore:

I know Alice.
Alice is a friend of mine.
I know Bjorn.
Bjorn is a friend of mine.
I know Cayman.
Cayman is a friend of mine.
I know Duanphen.
Duanphen is a friend of mine.
I know Esfir.
Esfir is a friend of mine.
I know Farah.
Farah is a friend of mine.
Those are the people I know.

Of course, we can achieve the above output using straight-line code as well, but it is far more tedious!

2.14. The range function

In the above example, the for loop keeps the code small but doesn’t let us do anything we couldn’t do without it. for loops become more interesting if we give them lists created using the range function.

The range function is built into Python and returns an object of type range that we can iterate over. For example, if we want to loop over the numbers 0,1,2,...,99, we do not want to have to write out each number in a list beginning [0,1,2,3,4,5,...]. Instead, range gives us the ability to create sequences of integers that start and stop at any numbers we choose. For now we will give range two arguments: a start value which will be included in the output, and a stop value which will not be included in the output. An example of using range is shown below:

>>> range(0,10)         # start at 0 (inclusive), end at 10 (exclusive)
range(0, 10)
>>> type(range(0,10))
<class 'range'>

In its current form, this output is not very helpful for understanding our code. We can convert the output of range to a list using type conversion, as shown below. This allows us to see the sequence of numbers as a list.

>>> list(range(0,10))           # convert to a list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2,8))            # includes 2, excludes 8
[2, 3, 4, 5, 6, 7]

Now that we understand how to use range, we can loop over very long sequences:

for number in range(0,1000):
    print(number)

These two lines execute the print statement a total of 1000 times!

The range function can take any int values, even ones from user input. If we want to give the user control of how many times we run our loop’s body, for instance, we might write a program like this:

total_str = input("How many numbers should I print? ")
total = int(total_str)
for number in range(0,total):
    print(number)

Now, the number of times the print statement runs is up to the user. We could further modify this code to let the user choose the start and stop values of the loop.

start = int(input("Which number should I print first? "))
stop = int(input("Which number should make the loop stop? "))
for number in range(start,stop):
    print(number)

Now the loop will start at the first number the user chooses, and stop right before the second number the user chooses. It is important to note that the difference between stop and start is the number of times the loop is executed. If the user chose 47 for the start and 63 for the end, the loop would be executed 63-47=16 times.

If range is used with only one argument, i.e. range(5), it will by default have a start value of 0. In addition, if range is used with three arguments, i.e. range(0,20,2) the third argument is interpreted as the step size, meaning it will start at 0, then step 2 so the next number is 2, then 4,6,8, etc.

>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(0,20,2))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

2.15. Accumulators

One helpful technique to use with loops is to create a variable called an accumulator. An accumulator variable is like any other variable, but it is used to gather up (accumulate) information as we run the loop.

Consider a program that adds up all of the numbers from 0 to some number given by the user. That is, if the user provides 8, it will produce 36 (which is the result of computing 0+1+2+3+4+5+6+7+8). How would we write such a program? We can’t loop to create more + operations; our loop can only run a fixed list of statements over and over again.

This problem is best solved by an accumulator which will track the total we have added so far in the program. At the beginning of the program, we set the accumulator to 0 (because we haven’t done any addition yet). Then, the loop body adds another number to the accumulator each time it runs.

last_number_str = input("Sum up to what number? ")
last_number = int(last_number_str)
sum = 0  # this is our accumulator
for number in list(range(0, last_number+1)):  # +1 to include last_number itself
    sum = sum + number  # increases the sum variable by number
print("The sum of those numbers is: %d" % (sum))

We set the sum accumulator to 0 above because 0 is the unit of addition: it is the value which has no effect when it is added to another number. To compute the factorial of a number (rather than the summation), our loop would need to multiply the accumulator and so its initial value would be 1 (the unit of multiplication).

2.16. Glossary

assignment operator
= is Python’s assignment operator, which should not be confused with the mathematical comparison operator using the same symbol.
assignment statement

A statement that assigns a value to a name (variable). To the left of the assignment operator, =, is a name. To the right of the assignment operator is an expression which is evaluated by the Python interpreter and then assigned to the name. The difference between the left and right hand sides of the assignment statement is often confusing to new programmers. In the following assignment:

n = n + 1

n plays a very different role on each side of the =. On the right it is a value and makes up part of the expression which will be evaluated by the Python interpreter before assigning it to the name on the left.

body
The part of a loop statement which may run many times (or not at all).
comment
Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.
composition
The ability to combine simple expressions and statements into compound statements and expressions in order to represent complex computations concisely.
concatenate
To join two strings end-to-end.
data type
A set of values. The type of a value determines how it can be used in expressions. So far, the types you have seen are integers (type int), floating-point numbers (type float), and strings (type str).
element
A value which appears in a list.
evaluate
To simplify an expression by performing the operations in order to yield a single value.
expression
A combination of variables, operators, and values that represents a single result value.
float
A Python data type which stores floating-point numbers. Floating-point numbers are stored internally in two parts: a base and an exponent. When printed in the standard format, they look like decimal numbers. Beware of rounding errors when you use floats, and remember that they are only approximate values.
int
A Python data type that holds positive and negative whole numbers.
integer division
An operation that divides one integer by another and yields an integer. Integer division yields only the whole number of times that the numerator is divisible by the denominator and discards any remainder.
list
A type of value which contains other values, called elements, in some order.
loop
Code which may execute many times (or not at all).
keyword
A reserved word that is used by the compiler to parse program; you cannot use keywords like if, def, and while as variable names.
operand
One of the values on which an operator operates.
operator
A special symbol that represents a simple computation like addition, multiplication, or string concatenation.
rules of precedence
The set of rules governing the order in which expressions involving multiple operators and operands are evaluated.
state diagram
A graphical representation of a set of variables and the values to which they refer.
statement
An instruction that the Python interpreter can execute. Examples of statements include the assignment statement and the print statement.
str
A Python data type that holds a string of characters.
value
A number or string (or other things to be named later) that can be stored in a variable or computed in an expression.
variable
A name that refers to a value.
variable name
A name given to a variable. Variable names in Python consist of a sequence of letters (a..z, A..Z, and _) and digits (0..9) that begins with a letter. In best programming practice, variable names should be chosen so that they describe their use in the program, making the program self documenting.

2.17. Exercises

  1. Record what happens when you print an assignment statement:

    >>> print(n = 9)
    

    How about this?

    >>> print(7 + 5)
    

    Or this?

    >>> print("this"+"that")
    

    Can you think of a general rule for what can follow the print statement? What does the print statement return?

  2. Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

  3. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6.

  4. Place a comment before a line of code that previously worked, and record what happens when you rerun the program.

  5. Start the Python interpreter and enter bruce + 4 at the prompt. This will give you an error:

    NameError: name 'bruce' is not defined
    

    Assign a value to bruce so that bruce + 4 evaluates to 10.

  6. Write a program (Python program) named madlib.py, which asks the user to enter a series of nouns, verbs, adjectives, adverbs, plural nouns, past tense verbs, etc., and then generates a paragraph which is syntactically correct but semantically ridiculous (see http://madlibs.org for examples).

  7. One example from this chapter prints all of the numbers from 0 to a number that the user provides. Write a program that prints all of the odd numbers from 0 to a number that the user provides.

  8. What is the output of the following for loops?

    for i in range(0):
        print("hello")
    
    for i in []:
        print("goodbye")
    
  9. Some of the above loops use lists directly (e.g. [1, 2, 3]) or use the range function. Do the following work?

    x = range(5)
    for i in x:
        print("hello")
    
    x = 5
    for i in x:
        print("hello")
    
  10. Technically, for loops work with any sequence in python. A string is a sequence. What is the output of the following loop?

    mystr = "Swarthmore"
    for i in mystr:
      print(i)
      print("--------------")