Reputation: 1503
I'm just getting started with python. Can somebody interpret line 2 of the following code snippet? I don't understand the `num`
bit. I tried to replace the backtick character with a single tick '
, but then it broke. Just a detailed explanation of that line would be great.
loop_count = 1000000
irn = ''.join([`num` for num in range(loop_count)])
number = int(irn[1]) * int(irn[10]) * int(irn[100]) * int(irn[1000]) * int(irn[10000]) * int(irn[100000]) * int(irn[1000000])
print number
Upvotes: 9
Views: 7658
Reputation: 208425
Backticks are a deprecated alias for the repr()
builtin function, so the second line is equivalent to the following:
irn = ''.join([repr(num) for num in range(loop_count)])
This uses a list comprehension to create a list of strings representing numbers, and then uses ''.join()
to combine that list of strings into a single string, so this is equivalent to the following:
irn = ''
for num in range(loop_count):
irn += repr(num)
Note that I used repr()
here to be consistent with the backticks, but you will usually see str(num)
to get the string representation of an int (they happen to be equivalent).
Upvotes: 12
Reputation: 500247
for num in range(loop_count)
iterates over all numbers from zero up to and excluding 1,000,000num
in backticks converts each number to string using the repr()
function.''.join(...)
merges all those strings into one without any separators between them.irn = ...
stores the result in irn
.Upvotes: 2