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

Experimenting with lists

x = [1, 2, 3, 4, 5, 6]
print (len(x))

You can say, call a list x, for example, and assign it to the numbers 1 through 6, and these square brackets indicate that we are using a Python list, and those are immutable objects that I can actually add things to and rearrange as much as I want to. There's a built-in function for determining the length of the list called len, and if I type in len(x), that will give me back the number 6 because there are 6 numbers in my list.

Just to make sure, and again to drive home the point that this is actually running real code here, let's add another number in there, such as 4545. If you run this, you'll get 7 because now there are 7 numbers in that list:

x = [1, 2, 3, 4, 5, 6, 4545]
print (len(x))

The output of the previous code example is as follows:

7

Go back to the original example there. Now you can also slice lists. If you want to take a subset of a list, there's a very simple syntax for doing so:

x[3:]

The output of the above code example is as follows:

[1, 2, 3]
主站蜘蛛池模板: 长治市| 睢宁县| 大丰市| 定结县| 都昌县| 弥勒县| 霍林郭勒市| 扎囊县| 贵南县| 丹凤县| 乃东县| 荃湾区| 阜平县| 英山县| 涟源市| 东兰县| 个旧市| 枣阳市| 佛学| 古田县| 南召县| 方正县| 台南市| 镇平县| 万安县| 南京市| 桦甸市| 新营市| 富平县| 阳东县| 鲁山县| 卓尼县| 普格县| 额济纳旗| 四平市| 乐业县| 竹北市| 敖汉旗| 松滋市| 车致| 内乡县|