[Tutor] What is the square brackets about?
boB Stepp
robertvstepp at gmail.com
Sat Jan 16 14:35:12 EST 2016
On Sat, Jan 16, 2016 at 1:14 PM, boB Stepp <robertvstepp at gmail.com> wrote:
>
> While learning I find it very helpful to either use IDLE or invoke the
> Python interpreter in the shell and try these things out. Once I get
> it to work, then I play around with the syntax and deliberately try to
> break things and see what sorts of errors are generated, figure out
> the limits of what the syntax will allow, etc., until I feel I am
> starting to understand what the original code does.
Continuing to play around with the code:
>>> loc = [0, 2, 8]
>>> get_(loc, thing)
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
get_(loc, thing)
File "<pyshell#51>", line 3, in get_
return get_(loc[1:], thing[loc[0]])
File "<pyshell#51>", line 3, in get_
return get_(loc[1:], thing[loc[0]])
File "<pyshell#51>", line 3, in get_
return get_(loc[1:], thing[loc[0]])
IndexError: string index out of range
and,
>>> loc = [0, 1]
>>> get_(loc, thing)
'a'
And so on. Until you (and I) can understand why the function produces
these outputs with the given values of loc and thing, then we cannot
claim we understand what is going on. So I encourage you to
thoroughly explore your sample code!
--
boB
More information about the Tutor
mailing list