In [1]:
n = 5

In [2]:
[i*i for i in range(n)]


Out[2]:
[0, 1, 4, 9, 16]

In [3]:
a = []
for i in range(n):
    a.append(i*i)
a


Out[3]:
[0, 1, 4, 9, 16]

In [4]:
tuple(i*i for i in range(n))


Out[4]:
(0, 1, 4, 9, 16)

In [5]:
{i:i*i for i in range(n)}


Out[5]:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

In [6]:
{i:(i*i)%3 for i in range(n)}


Out[6]:
{0: 0, 1: 1, 2: 1, 3: 0, 4: 1}

In [7]:
{(i*i)%3:i for i in range(n**n)}


Out[7]:
{0: 3123, 1: 3124}

In [8]:
{(i*i)%3 for i in range(n**n)}


Out[8]:
{0, 1}

In [9]:
for j in (i*i for i in range(n)):
    print(j)


0
1
4
9
16

In [10]:
g = (i*i for i in range(n))
g


Out[10]:
<generator object <genexpr> at 0xb296cacc>

In [11]:
next(g)


Out[11]:
0

In [12]:
next(g)


Out[12]:
1

In [13]:
next(g)


Out[13]:
4

In [14]:
next(g)


Out[14]:
9

In [15]:
next(g)


Out[15]:
16

In [16]:
next(g)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-16-5f315c5de15b> in <module>()
----> 1 next(g)

StopIteration: 

In [17]:
def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a+b

In [18]:
f = fib()
f


Out[18]:
<generator object fib at 0xb210b234>

In [19]:
next(f)


Out[19]:
0

In [20]:
next(f)


Out[20]:
1

In [21]:
next(f)


Out[21]:
1

In [22]:
next(f)


Out[22]:
2

In [23]:
next(f)


Out[23]:
3

In [24]:
next(f)


Out[24]:
5

In [25]:
next(f)


Out[25]:
8

In [26]:
from itertools import islice

In [27]:
for i in islice(fib(), n):
    print(i)


0
1
1
2
3

In [28]:
list(islice(fib(), n))


Out[28]:
[0, 1, 1, 2, 3]