The bin() function


#1

Hello.
https://www.codecademy.com/courses/python-intermediate-en-KE1UJ/0/4?curriculum_id=4f89dab3d788890003000096#
I figured how to use a loop for this but how do I use a range? From what Ive tried:


print bin(range(2,6))

error: 'list' object cannot be interpreted as an index
and

print bin(x for x in range(2,6))

error:'generator' object cannot be interpreted as an index
halp?


#2

range returns a list, for x in <list> returns a generator. You can use function bin only to get binary representation of a single number.

I figured how to use a loop for this

So you probably already have a correct answer :slight_smile:

for x in range(2,6):
    print x, bin(x)

#3

Yes thats my loop code. Ok so I can only put one int number inside the bin() brackets. So is there a way to use only range and not for AND range?


#4

Sure, but is there any good reason to do that?

For example, you can use map:

print '\n'.join(map(bin, range(2, 6)))

Edit

I just checked the hint text. I understand your concern, but I think that someone just had made a typo there. Your solution is completely correct.