Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63108

Re: Strange behaviour with a for loop.

References <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com>
Date 2014-01-04 00:09 -0500
Subject Re: Strange behaviour with a for loop.
From Larry Martell <larry.martell@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4888.1388812200.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jan 3, 2014 at 11:03 PM, Sean Murphy <mhysnq1964@icloud.com> wrote:
> Hello all.
>
> This is a newly question. But I wish to understand why the below code is providing different results.
>
> import os, sys
>
>
> if len(sys.argv) > 2:
>   filenames = sys.argv[1:]
> else
>   print ("no parameters provided\n")
>   sys.edit()
>
> for filename in filenames:
>   print ("filename is: %s\n" %filename)
>
> The above code will return results like:
>
> filename is test.txt
>
> If I modify the above script slightly as shown below, I get a completely different result.
>
> if len(sys.argv) > 2:
>   filenames = sys.argv[1]
> else
>   print ("no parameters provided\n")
>   sys.exit()
>
> for filename in filenames:
>   print ("filename is:  %s\n" % filename)
>
> The result is the filename is spelled out a character at a time. The bit I am missing is something to do with splicing or referencing in Python.
>
> Why am I getting different results? In other languages I would have got the whole content of the element when using the index of the array (list).
>
>
> Sean
> filename is: t
> filename

argv[1] gives just the second item in the list
argv[1:] gives a list containing the items in the list, from the
second to the end

>>> x = ['foo', 'bar', 'baz']
>>> print x[1]
bar
>>> print x[1:]
['bar', 'baz']

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Strange behaviour with a for loop. Larry Martell <larry.martell@gmail.com> - 2014-01-04 00:09 -0500

csiph-web