![]() |
![]() |
|
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
Last time, we discussed:
Challenge: What is the index of the character that is 2/3 of the way through? Can you even usesubstring from yesterday, to pull out that one character?
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 ) |
letters = "abcdefghijk" partwayThrough = len(letters) * 2/3 substring( letters, partwayThrough-1, partwayThrough ) |
We say that
We read
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.
When evaluating expressions:
letters = "abcdefghijk" substring( letters, len(letters)*2/3 - 1, len(letters)*2/3 ) |
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" |
Suppose we want to compute
the letter which is
startCode = ord('a') shiftedCode = startCode - 32 chr(shiftedCode) |
startLetter = 'a' startCode = ord(startLetter) shiftedCode = startCode - 32 shiftedLetter = chr(shiftedCode) shiftedLetter |
TODO:
Discuss w/ your partner: We have seen doing this with 0 variables, with 2 variables and with 4 or 5. Which do you prefer, and why?
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 “
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Feb.06 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |