Python Forum
How to make a subtraction within a range of numbers?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a subtraction within a range of numbers?
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
result = 10
for number in range(4):
    result -= number
print(result)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  forloop to compute sum by alternating from addition to subtraction JulianZ 3 1,826 Apr-02-2022, 09:36 AM
Last Post: DeaD_EyE
  nsimplify, make zero the really small numbers in the matrix quest 5 1,660 Jan-19-2022, 11:41 PM
Last Post: quest
  matplotlib x axis range goes over the set range Pedroski55 5 3,229 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,009 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,448 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Odd numpy error with subtraction DreamingInsanity 5 2,749 Jun-01-2020, 02:49 PM
Last Post: DreamingInsanity
  Return prime numbers from range krzyfigh 2 1,934 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,098 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Basic subtraction error rix 4 3,422 Oct-11-2019, 06:43 AM
Last Post: buran
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,755 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020