On Apr 17, 2013 9:58 AM, "hari prasadh" <hariprasad...@gmail.com> wrote: > > Hi All, > Small doubt in behaviour of for loop in python: > > >>> a=[(1,2)] > >>> for i in a: > ... print i > ... > (1, 2) In this case the list a consists of a single tuple and so the way the loop is organized it prints each element of the list-- in this case one tuple
> >>> for i,j in a: > ... print i,j > ... > 1 2 > Explain how for loop behaves here? Here instead of using a tuple you are unpacking a tuple. This code assumes that the elements of the list are all 2 tuples and assigns the first element to i and the second to j. > > >>> for i,j in a: > ... for k,l in a: > ... print i,j,k,l > ... > 1 2 1 2 Same explanation as above. > Explain how for loop behaves here? > >>> a=[(1,2),(3,4)] > >>> for i in a: > ... print i > ... > (1, 2) > (3, 4) Refer to the explanation to the first loop The rest can be understood the same way > >>> for i,j in a: > ... print i,j > ... > 1 2 > 3 4 > Explain how for loop behaves here? > >>> for i,j in a: > ... for k,l in a: > ... print i,j,k,l > ... > 1 2 1 2 > 1 2 3 4 > 3 4 1 2 > 3 4 3 4 > > Explain how for loop behaves here? > Please clarify. > > -- > Thanks & Regards, > HariPrasad BTW, you are better off asking in the python tutor list. _______________________________________________ ILUGC Mailing List: http://www.ae.iitm.ac.in/mailman/listinfo/ilugc