0

So here's my code:

user_input = input('What number would you like to convert?: ')
binary = []
num = int(user_input)
while num != 0:
    remainder = num % 2
    num = (num - remainder)  / 2
    binary.append(int(remainder))
print(user_input, 'in binary is: ', end='')
print(*reversed(binary), sep='')

Why do i have to have the '*' before printing out the array? i looked everywhere and couldn't find what it does and what its used for.

Thanks in advance, Rob

1 Answers1

0

This is called the "splat" operator. For more information on it, see the Python documentation on it.

What it's basically doing is this:

print(reversed(binary)[0], reversed(binary)[1], ..., sep='')

Essentially, it uses the array elements as arguments instead of passing the array as a single argument itself.

>>> lst = [1, 2, 3]
>>> print(lst)  # equivalent to `print([1, 2, 3])'
[1, 2, 3]
>>> print(*lst) # equivalent to `print(1, 2, 3)'
1 2 3
>>> print(reversed(lst))
<list_reverseiterator object at 0x7f4863ae4a20>
>>> print(*reversed(lst))
3 2 1
tckmn
  • 57,719
  • 27
  • 114
  • 156