News:

11 March 2016 - Forum Rules

Main Menu

Question about Python

Started by Squall_FF8, March 31, 2018, 04:41:30 AM

Previous topic - Next topic

Squall_FF8

That should be a simple question: Does 'for' loop include its boundaries?

For example:
Quotefor 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)?
Welcome to the FF5 Den: https://discord.gg/AUqDF85

FAST6191

https://wiki.python.org/moin/ForLoop
QuoteThe ''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/

Quoterange() (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

Thank you FAST6191 for the fast answer!!!

It seems it doesn't include the right boundary so this is where my mistake was  :laugh:
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Disch

Quote from: Squall_FF8 on March 31, 2018, 05:43:53 AM
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

Quote from: Disch on March 31, 2018, 12:14:39 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 ...?
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Disch

Quote from: Squall_FF8 on March 31, 2018, 12:50:59 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.   :'(