Python is going to be hard

Rustom Mody rustompmody at gmail.com
Thu Sep 4 03:48:47 CEST 2014


On Wednesday, September 3, 2014 11:41:27 PM UTC+5:30, Seymore4Head wrote:
> import math
> import random
> import sys
> b=[]
> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
> for x in steve:
>     print (steve[x])

> Traceback (most recent call last):
>     print (steve[x])
> IndexError: list index out of range

$ python
Python 2.7.8 (default, Aug 23 2014, 21:00:50) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> steve
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> # You can see steve (strange name choice) without any printing

>>> [x for x in steve]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> # A bit long-winded but good to get used to; And NO PRINT

>>> [(x,y) for (x,y) in zip(steve, range(100))]
[(1, 0), (1, 1), (2, 2), (3, 3), (5, 4), (8, 5), (13, 6), (21, 7), (34, 8), (55, 9), (89, 10)]
>>> # NO PRINT; but whats that 100 there?

>>> [(x,y) for (x,y) in zip(steve, range(len(steve)))]
[(1, 0), (1, 1), (2, 2), (3, 3), (5, 4), (8, 5), (13, 6), (21, 7), (34, 8), (55, 9), (89, 10)]
>>> # NO PRINT; but sufficiently common that it needs a shortform


>>> [(x,y) for (x,y) in enumerate(steve)]
[(0, 1), (1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (7, 21), (8, 34), (9, 55), (10, 89)]
>>> # NO PRINT but why not just the simple

>>> enumerate(steve)
<enumerate object at 0x7f0434de2780>
>>> # Hmm whats that??

>>> list(enumerate(steve))
[(0, 1), (1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (7, 21), (8, 34), (9, 55), (10, 89)]
>>> NO PRINT



More information about the Python-list mailing list