RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs

lab07-variables
variables

Review


Last time, we discussed:

We saw an unwieldy answer to the last one: substring("abcdefhijk", len("abcdefghijk")*2/3-1, len("abcdefghijk")*2/3 ) This expression is hard to read because, in part, the reader has to notice the similarity between the three arguments (e.g. that they each are using the exact same string), and even verify that the similarity is intentional and not ever mis-typed.

We'd like to give a name to sub-parts of this computation.

letters = "abcdefghijk"
substring( letters, len(letters)*2/3 - 1, len(letters)*2/3 )
    
In fact, we can go further and also give a name to 2/3 of the way through letters:
letters = "abcdefghijk"
partwayThrough = len(letters) * 2/3
substring( letters, partwayThrough-1, partwayThrough )
    

We say that letters and partwayThrough are variables. Variables can be used to give names to sub-parts of a larger computation.

= is pronounced “gets”

We read letters = "abcdefghijk" as “lettters gets "abcdefghijk"”. Note that this is not the same as equality1. In particular, = indicates an action: figure out the value of the right-hand-side, and store it in a box with the name on the left-hand-side.

Variables are a named locations which hold a value

Think of a variable as a labeled box: it can hold one value. We'll draw pictures of what's going on inside the comptuer; those pictures are simple now, but will become more involved in the future.

Stepping through an expression-evaluation

When evaluating expressions:

letters = "abcdefghijk"
substring( letters, len(letters)*2/3 - 1, len(letters)*2/3 )
    
After the first line, python has a box named “letters”. It proceeds step-by-step:
  substring( letters, len(letters)*2/3 - 1, len(letters)*2/3 )
=   substring( letters, len(letters)*2/3 - 1, len(letters)*2/3 )
=   substring( "abcdefghijk", len(letters)*2/3 - 1, len(letters)*2/3 )
=   substring( "abcdefghijk", len("abcdefghijk")*2/3 - 1, len(letters)*2/3 )
=   substring( "abcdefghijk", 11*2/3 - 1, len(letters)*2/3 )
=   substring( "abcdefghijk", 22/3 - 1, len(letters)*2/3 )
=   substring( "abcdefghijk", 7 - 1, len(letters)*2/3 )
=   substring( "abcdefghijk", 6, len(letters)*2/3 )
=   substring( "abcdefghijk", 6, len("abcdefhijk")*2/3 )
=   substring( "abcdefghijk", 6, 11*2/3 )
=   substring( "abcdefghijk", 6, 22/3 )
=   substring( "abcdefghijk", 6, 7 )
=   "g"
    
Whew! Notice in particular that python doesn't quite remember the value of letters all the time; instead, whenever it sees that variable, it goes and looks it up in its memory. Nore does python automatically remember the result of len(letters)*2/3; it re-computes it each time it sees that (sub)expression.

Suppose we want to compute the letter which is 'a' shifted back by 32. One solution would be chr( ord('a') - 32 ). But others could be:

TODO:

Note that if we first want to do one thing, and use that result in the next step, and so-on, we can either compose functions, or we can make up name for intermediate results and store those results in those variables.


1 We'll see soon, how to ask whether two values are equal or not — that's “==” in python :-( .      

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.Feb.06 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme