[Edit, test and edit] zero-based learning python_06_list (understand and use lists)

Understanding the list
consists of a series arrangement of elements in a certain order.
You can create a list that contains all the letters of the alphabet, numbers 0-9, or the names of all family members; you can also add anything to the list without any relationship between the elements. Given that lists usually contain multiple elements, it is a good idea to give the list a plural name (such as letters, digits, or names).
In Python, square brackets ([]) are used to denote lists, and commas are used to separate the elements. The following is an example of a simple list, this list contains several teachers:
<font size="3" face="Microsoft Yahei">teachers = ['利剑','Qingyun','Blade','White Crane']
print(teachers)</font>
Copy the code Since
[Edit, test and edit] zero-based learning python_06_list (understand and use lists)
this is not the output you want users to see, let's learn how to access list elements.
Access list elements A
list is an ordered set, so to access any element of the list, you only need to tell Python the position or index of the element. To access a list element, you can specify the name of the list, then the index of the element, and put it in square brackets.
For example, the following code extracts a teacher from the list of teachers:
<font size="3" face="Microsoft Yahei">teachers = ['利剑','Qingyun','Blade','White Crane']
print (teachers[0])</font>

[Edit, test and edit] zero-based learning python_06_list (understand and use lists)

In Python, the index of the first list element is 0, not 1. This is true in most programming languages, and this is related to the underlying implementation of list operations. If the result is unexpected, please see if you made a simple mistake. The index of the second list element is 1. According to this simple counting method, to access any element of the list, you can subtract 1 from its position and use the result as an index. For example, to access the third list element, use index 2.
<font size = "3" face = " Microsoft yahei"> teachers = [ 'sword', 'Wun', 'blade', 'crane']
Print (Teachers [2]) </ font>
copy the code
[Edit, test and edit] zero-based learning python_06_list (understand and use lists)
Python is Access to the last list element provides a special syntax. By specifying the index as -1, Python can return the last list element:

<font size="3" face="Microsoft Yahei">teachers = ['Li Jian','Qingyun','Blade','White Crane']
print(teachers[-1])</font>
Copy code
[Edit, test and edit] zero-based learning python_06_list (understand and use lists)
here This syntax is useful because you often need to access the last element without knowing the length of the list. This convention also applies to other negative index, e.g., the index returns -2 penultimate
two list elements, the index returns -3 third last list element, and so on.

Use each element
in the list to use each value in the list like other variables. For example, you can use splicing to create messages based on the values ​​in the list.
Let's try to select the favorite teacher from the list and use this value to create a message:

<font size="3" face="Microsoft Yahei">teachers = ['利剑','Qingyun','Blade','White Crane']
message = "My favorite teacher is "+ teachers[0] +" ."
print(message)</font>
Copy code
[Edit, test and edit] zero-based learning python_06_list (understand and use lists)

Guess you like

Origin blog.51cto.com/14972695/2551949