Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77878
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Lists |
| Date | 2014-09-15 09:36 +0200 |
| Organization | None |
| References | <12kc1a5j82lak7tmnatvhk84fh2ne08s24@4ax.com> <lv5vgi$ehv$1@dont-email.me> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14018.1410766622.18130.python-list@python.org> (permalink) |
Christian Gollwitzer wrote: > Am 15.09.14 04:40, schrieb Seymore4Head: >> nums=range(1,11) >> print (nums) > >> I don't understand why the command nums=range(1,11) doesn't work. >> I would think that print(nums) should be 1,2,3 ect. >> Instead it prints range(1,11) > > It does work, but in a different way than you might think. range() does > not return a list of numbers, but rather a generator - that is an object > which produces the values one after another. But you can transform it > into a list: > > print(list(nums)) > > should give you what you want. > > Christian I'd call range() an iterable. A generator is a specific kind of iterator. The difference between iterable and iterator is that the latter cannot be restarted: >>> def f(): ... yield 1 ... yield 2 ... >>> g = f() >>> list(g) [1, 2] >>> list(g) [] # empty --> g is an iterator >>> r = range(1, 3) >>> list(r) [1, 2] >>> list(r) [1, 2] # same as before --> r is an iterable
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-14 22:40 -0400
Re: Lists Christian Gollwitzer <auriocus@gmx.de> - 2014-09-15 08:04 +0200
Re: Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-15 17:09 +1000
Re: Lists Peter Otten <__peter__@web.de> - 2014-09-15 09:36 +0200
Re: Lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-15 09:53 -0600
Re: Lists Peter Otten <__peter__@web.de> - 2014-09-15 18:16 +0200
Re: Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-15 16:59 +1000
Re: Lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-15 11:27 -0400
Re:Lists Dave Angel <davea@davea.name> - 2014-09-15 09:05 -0400
Re: Lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-15 11:28 -0400
csiph-web