The type/object distinction and possible synthesis of OOP and imperative programming languages
Chris Angelico
rosuav at gmail.com
Fri Apr 19 03:05:41 EDT 2013
On Fri, Apr 19, 2013 at 1:35 PM, rusi <rustompmody at gmail.com> wrote:
> If I have a loop:
>
> while i < len(a) and a[i] != x:
> i++
>
> I need to understand that at the end of the loop:
> i >= len(a) or a[i] == x
> and not
> i >= len(a) and a[i] == x
> nor
> i == len(a) or a[i] == x # What if I forgot to initialize i?
Or your program has crashed out with an exception.
>>> i,a,x=-10,"test","q"
>>> while i < len(a) and a[i] != x:
i+=1
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
while i < len(a) and a[i] != x:
IndexError: string index out of range
Or if that had been in C, it could have bombed with a segfault rather
than a nice tidy exception. Definitely initialize i.
But yeah, the basis of algebra is helpful, even critical, to
understanding most expression evaluators.
ChrisA
More information about the Python-list
mailing list