官术网_书友最值得收藏!

Pre colon

If, for example, you want to take the first three elements of a list, everything before element number 3, we can say :3 to get the first three elements, 1, 2, and 3, and if you think about what's going on there, as far as indices go, like in most languages, we start counting from 0. So element 0 is 1, element 1 is 2, and element 2 is 3. Since we're saying we want everything before element 3, that's what we're getting.

So, you know, never forget that in most languages, you start counting at 0 and not 1.

Now this can confuse matters, but in this case, it does make intuitive sense. You can think of that colon as meaning I want everything, I want the first three elements, and I could change that to four just again to make the point that we're actually doing something real here:

x[:4]

The output of the above code example is as follows:

[1, 2, 3, 4]
主站蜘蛛池模板: 广水市| 彭山县| 敖汉旗| 开江县| 余干县| 徐汇区| 张家口市| 阿拉善盟| 湟源县| 永川市| 郎溪县| 沁水县| 贵德县| 京山县| 承德市| 邻水| 腾冲县| 肇东市| 兰州市| 桐乡市| 崇信县| 南乐县| 渭南市| 平阳县| 凤山市| 绍兴县| 石渠县| 瑞丽市| 宁夏| 渭南市| 四子王旗| 通渭县| 新宾| 乌拉特前旗| 江安县| 湾仔区| 沙河市| 张家口市| 潞城市| 霍城县| 隆安县|