tony1812
Posts: 225
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

basic python question

Tue Nov 07, 2017 2:03 pm

Hello, I have a python array
array={1,2,3,4]

Now I like to retrieve the values in the array using for xxx in range (y)
for index in range(4):
print(array[index])
result: I get all 4 array values-1,2,3,4
but if I use
for index in range(3):
print(array[index])
result: I only get 3 array values-1,2,3
Does in range not start on index 0? Thanks.

bensimmo
Posts: 1918
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: basic python question

Tue Nov 07, 2017 2:13 pm

It ends at but does not include the last number, as such

So 4 = 0,1,2,3 (four in total)


It's a bugger and an English Vs Computer thinking.
Python didn't move the Computer part into the background ;-)

pcmanbob
Posts: 2218
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: basic python question

Tue Nov 07, 2017 3:56 pm

Tony.

I thing your confusion is with what's being returned and how the count is done.

run this simple program

Code: Select all

items = [1,2,3,4]

for index in range(len(items)):
    print(index, items[index])
    
and you get this as the result

Code: Select all

(0, 1)
(1, 2)
(2, 3)
(3, 4)
So the number on the left is the actual counting loop which starts at zero but it returns the first number in your array which is of course one and so on
till we get to the last number which is 3 in the loop but it returns 4 as it's the last number in the array.

So if we change the array to this

Code: Select all

items = [11,23,63,84]
We get this as the result which might make things a bit clearer.

Code: Select all

(0, 11)
(1, 23)
(2, 63)
(3, 84)
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

jahboater
Posts: 1938
Joined: Wed Feb 04, 2015 6:38 pm

Re: basic python question

Tue Nov 07, 2017 4:03 pm

bensimmo wrote:
Tue Nov 07, 2017 2:13 pm
It ends at but does not include the last number, as such

So 4 = 0,1,2,3 (four in total)


It's a bugger and an English Vs Computer thinking.
Python didn't move the Computer part into the background ;-)
The language B (precursor to C) did that (arrays started at one instead of zero) by adding an extra item at the start that was never used.

The other possibility is continually subtracting one on every single array reference.

Both are horrible!

robbes
Posts: 103
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: basic python question

Thu Nov 09, 2017 7:55 pm

Only slightly off-topic:
The enumerate method provides an option for you to specify the starting value of the index in an enumerated list.
For example, to have the first value of the index as 1 rather than the default 0:

Code: Select all

mylist = [2, 4, 6, 8]
for index, item in enumerate(mylist, 1):
    print(index, item)

bb677a
Posts: 10
Joined: Mon Jan 02, 2017 9:38 pm

Re: basic python question

Tue Nov 21, 2017 7:01 pm

Your main issue is not using the range function correctly. You are only asking for 3 numbers, see below results. Replace that '3'in your code with a '4' and you should get what you want.

Code: Select all

>>> for index in range(3):
	print index

	
0
1
2

Return to “Python”

Who is online

Users browsing this forum: No registered users and 28 guests