I'm relatively new to python, and I'm trying to optimize some code for a HackerRank problem. I found it odd that using range (i.e. generating a list?) is faster than just using a while loop with a single variable to iterate.
I'm wondering if it's faster to cache the result of the range function if I iterate over the same sequence later in the code. For example:
Is this faster:
ten = 10
zeroToTen = range(ten)
sum = 0
for x in zeroToTen:
    sum += x
product = 1
for y in zeroToTen:
    product *= y
Or should I just recall range each time:
ten = 10
sum = 0
for x in range(10):
    sum += x
product = 1
for y in range(10):
    product *= y