Python Forum
Question about my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about my code
#3
(Jun-07-2020, 11:42 PM)Than999 Wrote: What I don't understand in this solution is why the range is starting from 1 instead of 0?
It make no difference when summing it up,can look at range() to see what's going on.
>>> list(range(6))
[0, 1, 2, 3, 4, 5]
>>> list(range(0, 6))
[0, 1, 2, 3, 4, 5]
>>> list(range(1, 6))
[1, 2, 3, 4, 5] 
So Python(as most programming langues) start to count from 0.
When summing these up can use build sum() as you have written as a exercise,
then it will not be 21 because start a 0.
>>> sum(range(6))
15
>>> sum(range(0 ,6))
15
>>> sum(range(1, 6))
15
>>> 
>>> # So most add 1 to get 21,so starting a 0 or 1 make difference when use sum() or your code
>>> sum(range(7))
21
>>> sum(range(0 ,7))
21
>>> sum(range(1 ,7))
21
Reply


Messages In This Thread
Question about my code - by Than999 - Jun-07-2020, 11:42 PM
RE: Question about my code - by pyzyx3qwerty - Jun-08-2020, 02:06 AM
RE: Question about my code - by snippsat - Jun-08-2020, 03:07 AM
RE: Question about my code - by buran - Jun-08-2020, 03:52 AM
RE: Question about my code - by Emekadavid - Jun-08-2020, 06:28 PM
RE: Question about my code - by jefsummers - Jun-08-2020, 09:09 PM

Forum Jump:

User Panel Messages

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