# [Tutor] Query regarding output

George Fischhof george at fischhof.hu
Sat Jul 1 10:57:52 EDT 2017

2017-06-29 15:55 GMT+02:00 shubham goyal <skgoyal721 at gmail.com>:

> Thanks all
> Great place to learn Python.
>
> On Jun 29, 2017 7:24 PM, "shubham goyal" <skgoyal721 at gmail.com> wrote:
>
> Thankyou all.
>
> Great place to learn.
>
> On Jun 29, 2017 5:55 PM, "Mats Wichmann" <mats at wichmann.us> wrote:
>
> > On 06/29/2017 03:02 AM, Alan Gauld via Tutor wrote:
> > > On 29/06/17 03:14, shubham goyal wrote:
> > >
> > >> This Question is asked in some exam. i am not able to figure it out.
> > >>
> > >> a = [0, 1, 2, 3]
> > >> for a[-1] in a:
> > >>     print(a[-1])
> > >>
> > >> its giving output 0 1 2 2
> > >>
> > >> it should be 3 3 3 3 as a[-1] belongs to 3.
> > >> can anyone help me figuring it out.
> > >
> > > This is quite subtle and it took me a few minutes to figure
> > > it out myself.
> > >
> > > It might be clearer if we print all of 'a' instead
> > > of a[-1]:
> > >
> > >>>> for a[-1] in a:
> > > ...    print(a)
> > > ...
> > > [0, 1, 2, 0]
> > > [0, 1, 2, 1]
> > > [0, 1, 2, 2]
> > > [0, 1, 2, 2]
> > >
> > > What is happening is that a[-1] is being assigned the value
> > > of each item in a in turn. The final iteration assigns a[-1]
> > > to itself, thus we repeat the 2.
> >
> >
> > Ugh.  I guess on an exam where they're trying to see if you can tease it
> > out, as you may one day have to debug such a sequence... but don't write
> > code like that.  One of the reasons people like "functional programming"
> > is it avoids these kind of side effects.
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

Hi All,

just a comment ;-)
Actually I do not know why it is good to ask similar things on a Python
exam... Yes, I know that if one can answer these questions they show that
they understand it.  (Just a side question: Really understand? ;-) )

But a Pythonista never will write a code like this.

I think this behaviour is just to "show" to the student that this is very
strong school... :-(

I think it would be better to teach how to write Pythonic code...

BR,
George