table of Contents
First, the list (list)
If now there is a demand, we need to store a person's hobby, if we use the data type is stored before we learned, there is no doubt, I can only use what strings are stored, and a person's hobby might be more so we use a space as a separator between a hobby and hobbies.
hobbies = "read run girl"
print(hobbies)
read run girl
But we have been emphasizing, deposit is not an end, to take is the goal . If you now give us a demand, we need a hobby out of this man, seemingly with our current knowledge can not start. This time you have to change our strategy, we can for ways to store a person's hobby - list.
1.1 What is the type of list
The list is a data type for storing a plurality of data, just as container.
It is orderly and does not go heavy , comma separated multiple elements within the brackets
1.2 Definitions Method
lt = ['xucheng','handsome','chenyuxing','beautiful',1,1,1,1]
print(lt) # 输出 ['xucheng', 'handsome', 'chenyuxing', 'beautiful', 1, 1, 1, 1]
1.3 Use
Deposit is not an end, is taking aim, we introduce the method list index value, bearing in mind the index number from zero.
hobby_list = ['read', 'run', 'girl']
# 索引序号 0 1 2
# 取出第二个爱好
print(hobby_list[1])
Output: run
hobby_list = ['read', 'run', ['girl_name', 18, 'shanghai']]
# 取出girl的年龄
print(hobby_list[2][1])
Output: 18