![]() |
How to make a subtraction within a range of numbers? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to make a subtraction within a range of numbers? (/thread-3249.html) |
How to make a subtraction within a range of numbers? - Alberto - May-08-2017 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 RE: How to make a subtraction within a range of numbers? - ichabod801 - May-08-2017 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. RE: How to make a subtraction within a range of numbers? - Alberto - May-08-2017 (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 RE: How to make a subtraction within a range of numbers? - ichabod801 - May-08-2017 result = 10 for number in range(4): result -= number print(result) |