In this chapter, we will describe the data structures handled by Python programs. Among them, I would like to touch on the list this time.
Python is said to be an object-oriented language. Actually, the character string, which was dealt with in Chapter 2, is also an object, but I think that it acted (method) on the thing (object) called the character string.
The list handled this time is also one of the objects. You will also be able to work with such objects.
However, objects are difficult to handle theoretically, so I'll tell you more about them when I talk about object-oriented programming.
Python allows you to combine relevant data into one. This is called ** list ***.
Enter the following code from the ** Python Console **.
>>>L = [10, 20, 30, 40, 50]
As mentioned above, ** list is enclosed in [] and separated by "," **. A list is assigned to the variable L. You can see the contents of the list with the print function as shown below.
>>>print(L)
[10, 20, 30, 40, 50]
I talked about "variables" first, but I said that values are tagged with variable names. Again, the list is still tagged.
Each value is called a ** list element **. For example, to retrieve one element, write: This time as well, the ** element numbers in the list start from 0th **, as in the case of strings. Also, the element number is the same as for a character string, and you can also assign a ** minus number **.
Number (1) | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
Number (2) | -5 | -4 | -3 | -2 | -1 |
Numbers in the list | 10 | 20 | 30 | 40 | 50 |
>>>L[2]
30
>>>L[-2]
40
The image looks like this:
Of course, if you specify a number that does not exist in the element, an error will occur as shown below.
>>>L[5]
Traceback (most recent call last):
File "<input>", line 1, in <module>
IndexError: list index out of range
Not only numbers but also strings can be assigned to the list. However, if it is a character string, it must be enclosed in "'" (single quotation marks) or "" "(double quotation marks).
Enter the following code from the ** Python Console **.
>>>strL = ['Japan', 'China', 'Korea']
>>>print(strL)
['Japan', 'China', 'Korea']
>>>strL[1]
'China'
A list of character strings is created in a variable called strL, and the elements are specified and output. This is the same as the list of numbers above.
I explained slicing in Chapter 02-05. You can also apply slices in a list.
Enter the following code from the ** Python Console **. Assign the list to the variable name ls and output the contents of the variable to check.
>>>ls = ['Japan', 'Canada', 'Australia', 'England', 'German', 'Italy', 'France']
>>>print(ls)
['Japan', 'Canada', 'Australia', 'England', 'German', 'Italy', 'France']
The contents of the variable ls are summarized below with element numbers.
Number (1) | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
Number (2) | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
String in the list | 'Japan' | 'Canada' | 'Australia' | 'England' | 'German' | 'Italy' | 'France' |
Based on the above, I will explain using slices.
>>>ls[2:]
['Australia', 'England', 'German', 'Italy', 'France']
>>>ls[:5]
['Japan', 'Canada', 'Australia', 'England', 'German']
>>>ls[2:5]
['Australia', 'England', 'German']
>>>ls[:-3]
['Japan', 'Canada', 'Australia', 'England']
>>>ls[1:-3]
['Canada', 'Australia', 'England']
First, I would like to consider the following results.
>>>ls[2:]
>>>ls[:5]
>>>ls[2:5]
As explained in the string slice in Chapter 02-05, it may be easier to understand if you think mathematically. For ** ls [2:] **, it is as follows. (Think of x as a number)
2≦x
In other words, all values with element numbers 2 or higher in the list will be output. ** ls [: 3] ** and ** ls [2: 5] ** are also as follows. The treatment is almost the same as when it is a character string.
x<5\\
2≦x<5
Next, I would like to look at the following slices.
>>>ls[:-3]
>>>ls[1:-3]
I think this is easy to understand if you combine it with the table above. The element "-3" is'German'in the table above, but this time it does not include the element number of -3, that is
x<-3
Therefore, in the above table, "-7" "-6" "-5" "-4" (that is, ** ['Japan','Canada','Australia','England'] **) Will be applicable.
** ls [1: -3] ** may also be easier to understand by comparing it with the table.
This time I mentioned the list in the data structure. You will definitely need the list when you analyze data in Python in the future, so be sure to keep it down. Next time, I would like to talk about working with lists.
Recommended Posts