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


Groups > comp.lang.python > #62684

Re: Variables in a loop, Newby question

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; 'indices': 0.07; 'lesser': 0.07; 'variables': 0.07; '[0,': 0.09; 'append': 0.09; 'line:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'python': 0.11; 'language.': 0.14; '49,': 0.16; 'be:': 0.16; 'dict': 0.16; 'examples:': 0.16; 'indexerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'simplified': 0.16; 'index': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; '>>>': 0.22; 'python?': 0.22; 'header:User-Agent:1': 0.23; 'lets': 0.24; 'handling': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; "i'm": 0.30; 'code': 0.31; '"",': 0.31; '(on': 0.31; 'file': 0.32; 'this.': 0.32; 'probably': 0.32; '(most': 0.33; 'something': 0.35; 'really': 0.36; 'var': 0.36; 'possible': 0.36; 'should': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'little': 0.38; 'recent': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'length': 0.61; 'range': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'results': 0.69; '10:': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Variables in a loop, Newby question
Date Tue, 24 Dec 2013 17:29:45 +0100
Organization None
References <9ad01eef-baf0-4018-833e-0b4dce4b9b85@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p508496fe.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4599.1387902584.18130.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387902584 news.xs4all.nl 2929 [2001:888:2000:d::a6]:53318
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:62684

Show key headers only | View raw


vanommen.robert@gmail.com wrote:

> Hello, for the first time I'm trying te create a little Python program.
> (on a raspberri Pi)
> 
> I don't understand the handling of variables in a loop with Python.
> 
> 
> Lets say i want something like this.
> 
> x = 1
> while x <> 10
> var x = x
> x = x + 1
> 
> The results must be:
> 
> var1 = 1
> var2 = 2
> 
> enz. until var9 = 9
> 
> How do i program this in python?

You are trying to generate a variable name programatically. While this is 
possible in Python

>>> x = 1
>>> while x != 10:
...     exec("var{} = x".format(x))
...     x = x + 1
... 
>>> var1
1
>>> var7
7

this is a really bad idea that you probably picked up from old code in a 
lesser language. Don't do it that way!

In Python you should use a dict or a list, for example:

>>> var = [] # an empty list
>>> x = 0
>>> while x < 10:
...     var.append(x) # append an item to the list
...     x += 1
... 
>>> var
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This can be simplified to a single line:

>>> var = list(range(10))

A few usage examples:

>>> var[0]
0
>>> var[9]
9
>>> var[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

As you can see list indices are zero-based. A list of length 10 has items 0 
to 9.

>>> var[7] += 42
>>> var
[0, 1, 2, 3, 4, 5, 6, 49, 8, 9]
>>> sum(var)
87

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


Thread

Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 08:07 -0800
  Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 11:20 -0500
  Re: Variables in a loop, Newby question "Tobias M." <tm@tobix.eu> - 2013-12-24 17:24 +0100
  Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-24 17:29 +0100
  Re: Variables in a loop, Newby question bob gailer <bgailer@gmail.com> - 2013-12-24 12:26 -0500
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 09:54 -0800
    Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 13:10 -0500
    Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-24 13:42 -0500
    Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-25 13:35 -0500
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 10:27 -0800
    Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 02:54 +0000
      Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-25 16:42 +1100
        Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 15:27 +0000
          Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-26 12:01 +1100
          Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 12:35 +1100
            Re: Variables in a loop, Newby question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-26 16:41 +1100
              Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-26 03:14 -0500
              Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 19:24 +1100
    Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-25 15:52 +0100
    Re: Variables in a loop, Newby question Michael Torrie <torriem@gmail.com> - 2013-12-25 23:34 -0700
  Re: Variables in a loop, Newby question Larry Hudson <orgnut@yahoo.com> - 2013-12-25 00:13 -0800
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-27 00:53 -0800
    Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-27 11:39 -0500

csiph-web