1

I got an error for a simple print statement, what could be the possible error, have changed to float and tried but still error persist.

if __name__ == '__main__':
    print (i*i for i in range(5))

error:

<generator object <genexpr> at 0x0000000002731828>

Thanks in advance...

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ramas
  • 53
  • 1
  • 1
  • 6

2 Answers2

10

In Python 3, print() is a function, not a statement.

A generator expression is like a list comprehension, except it creates an object that produces results when you iterate over it, not when you create it. For example,

[i*i for i in range(5)]

produces a list, [0, 1, 4, 9, 16], while

(i*i for i in range(5))

produces a generator object that will produce those numbers when you iterate over it.

If you give a function only one argument and it is a generator expression, you can omit the parentheses around the generator expression, so you do not have to do myfunc((i + 1 for i in something)).

So you are creating a generator object, and passing it to the print() function, which prints its representation. It’s doing exactly what you asked for, just not what you meant to ask for.

You can initialize a list from a generator expression:

print(list(i*i for i in range(5)))

but it is easier to use the list comprehension:

print([i*i for i in range(5)])

A simple example of how you might use the generator object is:

for value in (i * i for i in range(5)):
    print value

although in that simple example it would obviously be easier to write:

for i in range(5):
    print i * i
musicinmybrain
  • 621
  • 4
  • 8
  • Python 2 would produce the same output; it doesn't matter here if `print` is a statement or function, really. – Martijn Pieters May 09 '14 at 14:07
  • True, the semantics of the parentheses in `print(list(i*i for i in range(5)))` differs in Python 2 and 3 (whether they belong to the print function or to the generator expression), but the result is the same. – musicinmybrain May 09 '14 at 23:35
2

There is no error. I think you are simply trying to print a list. Use [] to get a list instead of a generator:

if __name__ == '__main__':
    print([i*i for i in range(5)])

Output:

[0, 1, 4, 9, 16]

To print on separate lines, you would do:

if __name__ == '__main__':
    print('\n'.join([str(i*i) for i in range(5)]))

This uses the 'delimiter'.join(list) approach to join all the elements of the list with the specified delimiter (in this case a newline: \n)

Output:

0
1
4
9
16

Or as @MartijnPieters suggested (for python3 only), you can also do:

print(*(i*i for i in range(5)), sep='\n')
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • @MartijnPieters, Ah right. Thank you so much. Hard to keep track of the little but important errors :) – sshashank124 May 09 '14 at 14:01
  • Another tip: you can use `print(*(i*i for i in range(5)), sep='\n')` to get the same output too. – Martijn Pieters May 09 '14 at 14:03
  • Your version with join will **not** work, however, not without adding `str()` calls: `print('\n'.join([str(i*i) for i in range(5)]))` – Martijn Pieters May 09 '14 at 14:04
  • @MartijnPieters, Ah yes the python3 printing style. Thank you. I have updated answer based on both comments. – sshashank124 May 09 '14 at 14:05
  • Thanks @MartijnPieters recently got a new system from XP to 64 bit windows so installed Python 3.3, that's the reason for all confusion, now I have to learn latest version Python syntaxes... thanks for your time... – ramas May 09 '14 at 14:17