Naming Values

As the underscore only stores the last result, and now it is the total price for the trip, if the gasoline price changes to US$ 2.45 we will have to retype the total gallons for the trip to know the new trip total price:

>>> 22.119999999999997 * 2.45
54.193999999999996

Is it really necessary to retype values whenever we want to use a result that is not the last?

And if there was an underscore for each value?

Well, there is and its name is... whatever name you like. We can name the miles per gallon mileage as mpg:

>>> mpg = 320 / 11.2
>>>

Now just type mpg:

>>> mpg
28.571428571428573

The mpg name will be defined as long as you don't leave the interactive interpreter. Note that the name MPG in upper case is not defined:

>>> MPG
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'MPG' is not defined

Lets name the trip total distance as distance:

>>> distance = 632
>>> distance
632

The gas price will be gallon_price:

>>> gallon_price = 2.45
>>> gallon_price
2.4500000000000002

The total price for the trip:

>>> distance / mpg * gallon_price
54.193999999999996

Now if the gas price varies you just change the gallon_price value:

>>> gallon_price = 2.41
>>> distance / mpg * gallon_price
53.309199999999997

computer programming degree learning course tutorial