What is the difference between single and double colon in this situation?
data[0:,4]
vs data[0::,4]
women_only_stats = data[0::,4] == "female"
men_only_stats = data[0::,4] != "female"
I tried to replace data[0::,4]
with data[0:,4]
and I see no difference. Is there any difference in this or another case?
data
is 2-dimensional array with rows like ['1' '0' '3' 'Braund, Mr. Owen Harris' 'male' '22' '1' '0' 'A/5 21171' '7.25' '' 'S']
In your case data is
So it is equal to
In this sitation there is only single item in list so data[0::4] or data[0:4] doesn't impact any thing.
If you try this it will clear your question/answer
It works like
So it behaves as usual if your step size is less then data length.
Both syntaxes result in the same indexes.
Basically,
1::,6
is a tuple of a slice (1::
) and a number (6
). The slice is of the formstart:stop[:stride]
. Leaving the stride blank (1::
) or not stating it (1:
) is equivalent.There is no difference. You're indexing by an identical
slice
object.It's like this:
s[start:end:step]
. Slice s from start to end with step step.No, there is no difference.
See the Python documentation for slice:
From the docs:
a[start:stop:step]
In this case, you are including an empty
step
parameter.And to understand what the
step
parameter actually does:So by leaving it to be implicitly
None
(i.e., by eithera[2:]
ora[2::]
), you are not going to change the output of your code in any way.Hope this helps.