Python Forum
forloop to compute sum by alternating from addition to subtraction
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
forloop to compute sum by alternating from addition to subtraction
#3
sign = 1
total = 0
for x in range(1, 1002, 2):
    total += sign/x
    sign = -sign
print(total)
Not much to explain.
Reply


Messages In This Thread
RE: forloop to compute sum by alternating from addition to subtraction - by deanhystad - Apr-02-2022, 08:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,993 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Odd numpy error with subtraction DreamingInsanity 5 4,050 Jun-01-2020, 02:49 PM
Last Post: DreamingInsanity
  Basic subtraction error rix 4 4,810 Oct-11-2019, 06:43 AM
Last Post: buran
  addition for elements in lists of list ridgerunnersjw 3 4,100 Sep-15-2019, 07:11 AM
Last Post: perfringo
  multiplication by successive addition Zebrol 1 4,578 Sep-14-2019, 05:37 PM
Last Post: ichabod801
  Addition of 2 classes Naito 1 2,566 Feb-15-2019, 10:51 AM
Last Post: DeaD_EyE
  Python 2.7 Addition to dict is too slow VolanD 6 5,551 May-04-2018, 09:24 AM
Last Post: Gribouillis
  Trying to create a python exe for an addition problem with an odd variable Python_Newb 0 2,624 Nov-30-2017, 12:18 PM
Last Post: Python_Newb
  How to make a subtraction within a range of numbers? Alberto 3 12,017 May-08-2017, 09:13 PM
Last Post: ichabod801
  pdf sms alternating conversation style sdcaliber 4 6,258 Apr-20-2017, 05:20 AM
Last Post: sdcaliber

Forum Jump:

User Panel Messages

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