Question: Recently I have started learning python programming language and I got stuck while printing 123..n sequence using this:

```
n=10
print(i for i in range(1,n+1))

```

I got this output:

```
<generator object <genexpr> at 0x7f5990f20db0>                                   

```

Expecting Output:  

 1234....n  

 (Note: Only continuous output)

Answer: 
You are attempting to `print` a generator expression. This is not possible. The parentheses, or lack thereof, indicate a [generator expression](https://docs.python.org/3/reference/expressions.html#generator-expressions) in Python.

Instead, you can use a `list`, indicated by square brackets:

```
n = 10
print([i for i in range(1, n+1)])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

```

If you wish to print the contents of a generator expression, you can iterate explicitly:

```
n = 10
for i in (i for i in range(1, n+1)):
    print(i)

```

Or via unpacking the expression and using the `sep` argument:

```
n = 10
print(*(i for i in range(1, n+1)), sep='')
12345678910

```

In this specific example, since you are exhausting the iterator, you can pass a `range` object directly:

```
n = 10
print(*range(1, n+1), sep='')
12345678910

```

The `*` operator is used for **sequence unpacking**. It can take any iterable and pass unpacked components to a feeder function, in this case `print`. It works for `print` as this specific function can take an arbitrary number of arguments, e.g. try `print(1, 2, 3, 4, 5, sep='')`. It will not, in contrast, work with `list`, which requires only one argument.
As many have mentioned this is creating a `generator`. Generators are different from normal functions. Generator returns an `iterator` which you can iterate one item per iteration.

The method that uses `yield` statement returns a generator.
Difference between return statement and yield is, that `return` statement terminates the program completely. Whereas `yield` statement saves the state of the variable for the next iterations. Suppose you want to do something with the yielded value in those scenarios we use generators.

Following are the examples for return and yield statement.

`Return`

```
def retutn_meth(n):
    for i in range(1, n+1):
        return i

a = retutn_meth(10)
for b in range(1,10):
    print(a)

```

`Output`

```
1
1
1
1
1
1
1
1
1

Process finished with exit code 0

```

`yield`

```
def gen_meth(n):
    for i in range(1, n+1):
        yield i

a = gen_meth(10)
for b in range(1,10):
    print(next(a))
    print('blah blah blah')

```

`output`

```
1
blah blah blah
2
blah blah blah
3
blah blah blah
4
blah blah blah
5
blah blah blah
6
blah blah blah
7
blah blah blah
8
blah blah blah
9
blah blah blah

Process finished with exit code 0

```

As you can see in the second example the state of the variable has been saved in each iteration and at `print('blah blah') you can put any other functionality you want to do in between the iterations.

Please read here for further clarification: [Python Generators](https://www.programiz.com/python-programming/generator)