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

Author Topic: Question about Python  (Read 2848 times)

Squall_FF8

  • Full Member
  • ***
  • Posts: 206
    • 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)?
Welcome to the FF5 Den: https://discord.gg/AUqDF85

FAST6191

  • Hero Member
  • *****
  • Posts: 2588
    • 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: 206
    • 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:
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Disch

  • Hero Member
  • *****
  • Posts: 2723
  • 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: 206
    • 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 ...?
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Disch

  • Hero Member
  • *****
  • Posts: 2723
  • 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.   :'(

RockyPython

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Question about Python
« Reply #6 on: June 23, 2018, 01:45:21 pm »
for loop iterates over every element in the sequence, it may be anything list, string or range of numbers.
here you passed range because of it doesn't include the LAST element in it. it has a very simple answer because index always starts with ZERO in python. if you count total numbers between range (0,5) you will get [0,1,2,3,4] i.e. total count is 5

Read more on python range here https://pynative.com/python-range-function/
Read more on python for loop herehttps://pynative.com/python-for-loop-explained-with-examples/

vani sharma

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Question about Python
« Reply #7 on: August 29, 2019, 05:36:54 am »
Python for loop looks to be a bit shabby but in reality, it is not. It doesn't restrict on including boundary values. Instead, it iterates everything you provide.
In your case, it is the Python range which is a culprit. It is so because of the range() function which generates values starting from top_y to bottom_y-1. Also, if you pass a single parameter to it like range(n), then it will begin generating numbers from 0 to n-1. Some very useful examples with explanations are given below: