Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing from 1 to 99 using a print and for loop function

I tried the following in the IDLE of Python 3.6

print(value for value in range(1,100))

A message is produced in the IDLE which says

<generator object <genexpr> at 0x101b73a40>

I'm confused what this means. Have I done anything wrong?

like image 410
user3416724 Avatar asked Jul 11 '17 19:07

user3416724


People also ask

Which of the following loop is used to print number from 1 to 100?

C Program to Print 1 to 100 Numbers using While Loop.


1 Answers

(value for value in range(1,100)) produces generator object, if you want to print list, just wrap it in []

print([value for value in range(1,100)])

or you can simply

print(list(range(1,100)))

You can read what generators are HERE

A function which returns a generator iterator. It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.

Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.

like image 161
vishes_shell Avatar answered Oct 05 '22 22:10

vishes_shell