for i in range(15):
print i #will print out 0..14
for i in range(1, 15):
print i # will print out 1..14
for i in range (a, b, s):
print i # will print a..b-1 counting by s. interestingly if while counting by the step 's' you exceed b, it will stop at the last 'reachable' number, example
for i in range(1, 10, 3):
print i
> 1
> 4
> 7
List Splicing:
a = "hello" # there are 5 characters, so the characters are accessible on indexes 0..4
a[1] = 'e'
a[1:2] = 'e' # because the number after the colon is not reached.
a[x:y] = all characters starting from the character AT index 'x' and ending at the character which is before 'y'
a[x:] = all characters starting from x and to the end of the string
In the future, if you ever wonder what the behavior of python is like, you can try it out in the python shell. just type python in the terminal and you can enter any lines you want (though this is mostly convenient for one-liners rather than scripts).
Answer from Jeremy Fisher on Stack Overflowfor i in range(15):
print i #will print out 0..14
for i in range(1, 15):
print i # will print out 1..14
for i in range (a, b, s):
print i # will print a..b-1 counting by s. interestingly if while counting by the step 's' you exceed b, it will stop at the last 'reachable' number, example
for i in range(1, 10, 3):
print i
> 1
> 4
> 7
List Splicing:
a = "hello" # there are 5 characters, so the characters are accessible on indexes 0..4
a[1] = 'e'
a[1:2] = 'e' # because the number after the colon is not reached.
a[x:y] = all characters starting from the character AT index 'x' and ending at the character which is before 'y'
a[x:] = all characters starting from x and to the end of the string
In the future, if you ever wonder what the behavior of python is like, you can try it out in the python shell. just type python in the terminal and you can enter any lines you want (though this is mostly convenient for one-liners rather than scripts).
The best way to clarify such doubts is to play with the REPL:
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x = list(range(10))
>>> x[:3]
[0, 1, 2]
>>> x[:1]
[0]
>>> x[1:10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Is range(len(list)) ever Pythonic?
'List index out of range' when it is not.
Irksome list slicing syntax - Why does python interpret indices differently in 'direct' indexing vs. 'range' indexing?
Embarrassingly, i don't understand how list indexing works
Videos
Are there circumstances where for i in range(len(my_list)): is to be preferred over for i, _ in enumerate(my_list):? If so, what are they, generally speaking?
Edited to fix syntax.
I'm new to python so please be gentle.
I have a list of 9 float numbers called scorelist. All of these numbers are separate.
[23.0, 39.0, 47.0, 51.5, 53.0, 58.5, 78.0, 80.5, 9.0]
Part of the program needs to determine the median number.
I wrote
mid = scorelist[4]
because that's the middle number and is therefore the median. However, despite this not being out of range, I get the 'list index out of range' error for that line.
I literally am not doing anything to change or alter the list in the entire code. The only thing I ever do to change the list is when I assign a slice of it to another list, but this doesn't actually change the original list, it just copies the numbers (printing before and after gives the original list).
Printing the list before and after declaring the list is fine. Printing 'mid' also works and gives the right number. It even appears to correctly use itself in an elif block.
But because all of this is inside of a for loop, my compiler crashes as soon as the loop ends and doesn't continue to the code that's outside the loop.
Why is this happening? How can I fix it?
Google is extremely unhelpful and just says "the number must be out of range" but it's literally not. It's working like it should be, but I still get the error for that line and the code stops executing.
I'll post the full code if it's needed. I don't know what would be wrong.
Here is what I don't quite get:
Given a list
list = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
We access the mth or nth element thusly:
m = 1
n = 5
print(list[m] + ', ' + list[n])
> B, F
Everything makes sense so far.
Now I want to access a subset of the elements of 'list' that includes all elements from the mth to the nth element (inclusively).
What I expect the notation to look like
slice = list[m:n]
does not produce the result
print(slice)
> ['B' 'C' 'D' 'E' 'F']
but rather, produces
print(slice)
> ['B' 'C' 'D' 'E']
Why does n suddenly become exclusive when written as a 'range'? Why does it make sense to write
slice = list[m:(n+1)]
to produce a subset of a list that spans the mth to the nth element?
As a bit of background, I have past experience in VHDL/Verilog, where a range written as m:n inclusively specifies bits m to n of a signal or register. The 'python way' of making the n exclusive baffles me. I'd like to understand why python syntax works this way.