Python Forum

Full Version: How to make a subtraction within a range of numbers?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Python Users,

I am pretty new to python, please help me with the following issue. I need to subtract every 10th squared number within a range of (1000,0). For example, 1000^2 - 990^2..... I started with the following loop, but do not now how to continue:

for z in range(1000,0,-10):
    Answer= z[z]**z[z] - z[z-1]**z[z-1]  
print("Answer=" Answer)
Best regards,
Alberto
I'm not sure what you need as the result. Do you need a list of the subtractions of the pairs of squares, or do you need to start at 1000^2 and subtract all of the other squares from that to get one final number? I'm guessing you want the latter. In that case, you would initialize Answer to 1000 ** 1000. Then loop down from 990, subtracting and storing the new result in Answer. Note that z is not a sequence, it is an integer. So z[z] will give you an error. You just want z ** z.
(May-08-2017, 08:40 PM)ichabod801 Wrote: [ -> ]I'm not sure what you need as the result. Do you need a list of the subtractions of the pairs of squares, or do you need to start at 1000^2 and subtract all of the other squares from that to get one final number? I'm guessing you want the latter. In that case, you would initialize Answer to 1000 ** 1000. Then loop down from 990, subtracting and storing the new result in Answer. Note that z is not a sequence, it is an integer. So z[z] will give you an error. You just want z ** z.
Thank you for your reply! Yes, I need the later - just the final number. Can you please clarify how can I store the result and go further (to another integer)?

Best regards,
Alberto
result = 10
for number in range(4):
    result -= number
print(result)