Reasons for demand:
Common data structures in Python can be collectively referred to as containers. Sequence (such as list and tuple), mapping (such as dictionary) and set are the three main containers. I will divide it into several knowledge points and give a little definition, but most of them focus on practical operation training. I will explain the structure of knowledge points in the grammatical format of functions and use cases of functions.
Requirement objectives:
Today we will discuss the concept of a list and two ways to access a list (read the X element of a list and read a range of elements in a list).
Specific practical operation:
List concepts: Lists are one of the most commonly used data structures in Python and other languages. Python uses brackets [] to represent lists
2. Access list
1) Read the x-th element of the list
Syntax: List name [index value]
Note: the index value is divided into forward index and reverse index, 0 is the first forward index of the list, -1 is the last case of the reverse index of the list:
a = [1.'a'[1.2] and {'key':'value'}]
print(a[0Print (a[-]) print(a[-4] # reverse index to find the first element in the listCopy the code
2) Read a range of elements from the list
Syntax: List name [initial index: destination index]
Function: The index value position requires a number, in accordance with the packet before not after the principle
Case study:
a = [1.'a'[1.2] and {'key':'value'}]
print(a[1:3] # forward index'a'[1.2]
print(a[-3: -1]'a'[1.2]
print(a[0:1]) # select first element (a[:1Print (a[-1:]) # take the last element (and a[-)1The difference between a[-] and1:] converts the last element to a list, and a[-1] will only extract the last element.Copy the code