home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
lab05-string-indices
string indices
(and, substring)
In yesterday's lab, we fit functions together as if they were lego blocks
(in particular, we happened to connect: pickAFile(), makePicture, show).
I have also been harping on types — the type given to a function
(as part of the question), and its return-type (as the answer).
The reason types are important is because they're what are revealing the “shape” of the function(block),
which reveals how they can be fit together.
The largest software projects are nothing but huge towers of interconnecting function-calls!
Recall how we evaluated makePicture( pickAFile() ) — this worked because
makePicture wants to be given a string,
and pickAFile returns a string.
We had to evaluate the innermost parentheses first:
we had to call pickAFile before we knew what string we were going to give to makePicture.
So nesting the function-calls (“composing the functions”) forced a sequence on what happened.
It may not look like we told the computer to first call pickAFile and then call makePicture,
but in essence we really did.
Self-quiz:
pickAFile( makePicture() ) makes no sense!
Can you give two different reasons why not?
The Editor Pane
The top-half of the JES window is just a text-editor (like Word, except it's slightly savvier about indenting python code).
-
Try typing in the following in the top-half.
Be sure to get it right character-for-character!
# We define a new function:
#
def substring(str,a,b):
return str[a:b]
|
-
What do you thing the # means, on the first two lines?
They are comment-lines — from the # (“hash”) to the end of the line.
The next couple of lines define a new function that didn't previously exist.
We'll see more about this starting next week, but as a sneak-peek:
-
Can you guess: What is the name of the function we're defining?
-
Can you guess: How many of pieces of information (“arguments”) will we need to give to this function, when we call it?
-
From the menu, File > Save this file.
- Save the file on your H: drive.
- You'll want to make a new folder for all your classwork; I suggest “itec109”.
- The file name should end in “.py” to indicate that this is a python program.
I suggest “hw01.py”.
-
After you've saved, press Load File.
This sends all the code from the top, editor pane down to the bottom, interaction pane.
If you have non-correct python syntax, this is where it will be caught (as the interaction pane checks it out).
Hopefully there was no error, but if there is, pay attention to the error message and which line is highlighted.
Common errors:
- Not having spaces before “return”.
- Not having a colon at the end of the “def” line.
- In the function-body, using parentheses rather than brackets:
str(a:b)
instead of
str[a:b].
If all went well, and the load was successful, you should be able to evaluate substring("hello",1,4)
and get the result "ell".
String indices
substring is a function that takes a big string, a start-index, and a stop-index;
it returns the (sub)portion of the string from the start-index up to, but not including, the stop-index.
Since we saw substring("hello",1,4) = "ell", we can deduce that 'e' is at index 1,
and 'o' (the first character not included) is at index 4.
Indices start counting from 0; we saw the index 1 started from the character 'e'.
(Rather than say “the 2nd character”, I'll try to say “the character at index 1”, to avoid confusion.)
We say “The indices are 0-based”.
This seems weird, but actually makes some sense:
a charcter's index is number of chars you need to skip, to reach it.
-
What will substring("hello",2,4) return? Try it out.
-
What will substring("hello",3,5) return? Try it out.
-
What do we type, to extract the first three characters?
Staring hw
For the remainder of the class, you can work on hw01—calling functions: types; composing functions.
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs