Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'parameters': 0.04; 'element': 0.07; 'modify': 0.07; 'sys': 0.07; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'newly': 0.09; 'spelled': 0.09; 'jan': 0.12; 'question.': 0.14; '%s\\n"': 0.16; 'filename)': 0.16; 'referencing': 0.16; 'index': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'slightly': 0.19; '>>>': 0.22; 'import': 0.22; 'to:name:python- list@python.org': 0.22; 'print': 0.22; 'sean': 0.24; 'script': 0.25; 'shown': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'character': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'code': 0.31; 'getting': 0.31; 'os,': 0.31; 'languages': 0.32; 'fri,': 0.33; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'results.': 0.60; 'providing': 0.61; 'different': 0.65; 'results': 0.69; 'containing': 0.69; 'wish': 0.70 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=paBJIhpEb1sZPIkECNgH9MYPD/GDMBcbEjPE1s2HsXc=; b=MnVwddFA1e9coknIvPsq6RjDJLiUh7wBYCCIeI1ZuhPQXJ6gVKaILAZRFPrb7QMmeq LABg1zko+7/7XU9lqrOyNfsCELN1n/59Sa999C1XgcHP25cw8Vlm3yHvVrCgdXPqUp6b FVWZznsyf2n2tE6MpgjIP3YcKj6YcPbeqraF8AZam0Q/P++vSoQFqH14NGgD9GUDeasW c3Ip+A9PyYoE5s2lcJMpLcxP7hj1f6WY5F5Cfq/MPskZuLHzrhCq64evO5NiHJQgiETe TTqeGDVVeENmWEL7FWeONf6TCGL2QMpVn5m55z0yOVKBXP2UDcls71DmTuehBIWSUxqh i2CQ== MIME-Version: 1.0 X-Received: by 10.180.97.39 with SMTP id dx7mr4538750wib.10.1388812198705; Fri, 03 Jan 2014 21:09:58 -0800 (PST) In-Reply-To: <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com> References: <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com> Date: Sat, 4 Jan 2014 00:09:58 -0500 Subject: Re: Strange behaviour with a for loop. From: Larry Martell To: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388812200 news.xs4all.nl 2858 [2001:888:2000:d::a6]:52246 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63108 On Fri, Jan 3, 2014 at 11:03 PM, Sean Murphy 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']