2.5 Names

Download notebook Interact

You have already seen expressions.

You saw in variables that we often want to give names to the results of expressions.

Now we get a little more formal about what that looks like in Python.

When Python gives a name to a value, that is an assignment statement.

A statement is a piece of code that performs an action.

Here is an assignment statement:

x = 4

Let’s break the statement down in more detail.

The statement has three parts:

  1. A left hand side (LHS), to the left of the equals sign =. In the expression above, the LHS is x;
  2. The equals sign =;
  3. The right hand side (RHS), to the right of the equals sign. This is an expression, that gives a value. In our case, the RHS is 4.

The assignment statement gives a name (on the LHS) to a value (on the RHS).

Whenever you see a name, then =, then an expression, that is an assignment statement.

It is often useful to describe what Python statements are doing, in words.

For the expression above, we can say “x gets the value 4”.

We call x a variable.

Once a variable gets a value, the name evaluates to its value.

For example, here we ask Python to evaluate an expression:

3 * x + 2

3 * x + 2 is an expression. By entering the expression in a cell on its own, we ask Python to evaluate the expression. First it evaluates the variable x to get 4. Then it evaluates the rest to get 3 * 4 + 2 = 14.

Remember, an assignment gives a name (on the LHS) to the value (on the RHS).

Here are two assignment statements, giving values to the names a and b. Then we use these variaables in an expression. The Notebook shows the result.

a = 10
b = 20
a + b
30

In the expression a + b above, Python evaluates the variable a to get 10, it evaluates the variable b to get 20, and then adds them, to give the final result of the expression.

A variable can be used in the expression to the right of =.

quarter = 1/4
half = 2 * quarter
half
0.5

We can change the value for variables. Here we change the value of variable quarter from 0.25 to 4.

quarter = 4

Now we have changed the value of quarter What do you think will happen to the value of half above? Will it change, or will it stay the same?

Remember that an expression evaluates its variables before returning the result. In the cell above, the expression half = 2 * quarter evaluated quarter, to get 0.25, and then evalulated 2 * 0.25, to give 0.5. When we changed quarter in the next cell, it did not affect the value that we have already given to half:

half
0.5

Rules for variable names

Variable names must start with a letter, but can contain both letters and numbers. A name cannot contain a space; instead, it is common to use an underscore character _ to replace each space. Names are only as useful as you make them; it’s up to the programmer to choose names that are easy to interpret. Typically, more meaningful names can be invented than a and b. For example, let’s say you were calculating the 20% Value Added Tax for a restaurant bill, as well as 15% tip, on top of that. The following names clarify the meaning of the various quantities involved.

meal_price = 25
vat_rate = 0.2
vat = meal_price * vat_rate
meal_price_with_vat = meal_price + vat
meal_price_with_vat
30.0
tip_rate = 0.15
tip = meal_price_with_vat * tip_rate
meal_price_total = meal_price_with_vat + tip
meal_price_total
34.5

See the Names exercises to practice the material in this section.

This page has content from the Names notebook from the UC Berkeley course. See the Berkeley course section of the license