- Hands-On Data Science and Python Machine Learning
- Frank Kane
- 209字
- 2021-07-15 17:15:00
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]
- 軟件安全技術
- Boost C++ Application Development Cookbook(Second Edition)
- Learn to Create WordPress Themes by Building 5 Projects
- Learning Docker
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- TestNG Beginner's Guide
- Building a Recommendation Engine with Scala
- HTML5從入門到精通 (第2版)
- Learning Concurrent Programming in Scala
- Natural Language Processing with Python Quick Start Guide
- 視窗軟件設計和開發自動化:可視化D++語言
- 超好玩的Scratch 3.5少兒編程
- 數字媒體技術概論
- Drupal Search Engine Optimization
- JavaEE架構與程序設計