News: 11 March 2016 - Forum Rules, Mobile Version
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia

Author Topic: Question about Python  (Read 287 times)

Squall_FF8

  • Full Member
  • ***
  • Posts: 141
    • View Profile
Question about Python
« on: March 31, 2018, 04:41:30 am »
That should be a simple question: Does 'for' loop include its boundaries?

For example:
Quote
for y in range(top_y, bottom_y):
Does Y takes all values from top_y to bottom_y (including both boundaries) or some of the boundaries are excluded like right one (Y never gets value of bottom_y)?

FAST6191

  • Hero Member
  • *****
  • Posts: 2267
    • View Profile
Re: Question about Python
« Reply #1 on: March 31, 2018, 04:53:24 am »
https://wiki.python.org/moin/ForLoop
Quote
The ''range'' is seen so often in for statements that you might think range is part of the for syntax. It is not: it is a Python built-in function which returns a sequence, which meets the requirement of providing a sequence for the for statement to iterate over.

https://www.pythoncentral.io/pythons-range-function-explained/

Quote
range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list is mylist[0]. Therefore the last integer generated by range() is up to, but not including, stop. For example range(0, 5) generates integers from 0 up to, but not including, 5.

Squall_FF8

  • Full Member
  • ***
  • Posts: 141
    • View Profile
Re: Question about Python
« Reply #2 on: March 31, 2018, 05:43:53 am »
Thank you FAST6191 for the fast answer!!!

It seems it doesn't include the right boundary so this is where my mistake was  :laugh:

Disch

  • Hero Member
  • *****
  • Posts: 2582
  • NES Junkie
    • View Profile
Re: Question about Python
« Reply #3 on: March 31, 2018, 12:14:39 pm »
It seems it doesn't include the right boundary so this is where my mistake was  :laugh:

To clarify:  'for' iterates over every element in the sequence you give it.
'range' generates a sequence which excludes the upper-bound

Ex:  range(0,5) generates the sequence [0,1,2,3,4] ... which is the sequence the for loop iterates over.

Squall_FF8

  • Full Member
  • ***
  • Posts: 141
    • View Profile
Re: Question about Python
« Reply #4 on: March 31, 2018, 12:50:59 pm »
To clarify:  'for' iterates over every element in the sequence you give it.
'range' generates a sequence which excludes the upper-bound
I see what you mean: the range is the reason for excluding the right boundary, not 'for'

P.S. Hi Disch, long time no see :) I'm glad that you are still active! Its been like 2 years since we worked together. Are you still doing NES projects or ...?

Disch

  • Hero Member
  • *****
  • Posts: 2582
  • NES Junkie
    • View Profile
Re: Question about Python
« Reply #5 on: March 31, 2018, 02:44:45 pm »
P.S. Hi Disch, long time no see :) I'm glad that you are still active! Its been like 2 years since we worked together. Are you still doing NES projects or ...?

Heyo.  No I'm not really doing any hobby projects anymore.  Too busy with real life responsibilities.   :'(