Chapter 3 python3, python3
A built-in data type in Python is list: list.
- List is an ordered set.
- You can add or delete elements at any time.
- It can appear as comma-separated values in square brackets.
- List data items do not need to have the same type
To create a list, you only need to enclose different data items separated by commas (,) in square brackets. As follows:
list1 = ['Google', 1997]list2 = [1, 2, 3, 4, 5]list3 = ["a", "b", "c", "d"]
Like the string index, the List Index starts from 0. The list can be intercepted and combined.
1. Values in the access list
Use subscript indexes to access values in the list. You can also use square brackets to intercept characters, as shown below:
list1 = ['Google', 1997]list2 = [1, 2, 3, 4, 5]print("list1[0]: ", list1[0])print("list2[1:5]: ", list2[1:5])
The above code outputs:
list1[0]: Googlelist2[1:5]: [2, 3, 4, 5]
2. Update the list
You can modify or update the data items in the list, as shown below:
List1 = ['Google ', 1997] print ("the 2nd elements of list1 are:", list1 [1]) list1 [1] = 'baidu' print ("the updated 2nd elements are:", list1 [1])
The above code outputs:
The first element of list1 is: 2nd. The first element after update is: baidu.
You can also use the append () method to add list items:
List1 = ['Google ', 1997] print ("original list1:", list1) list1.append ("baidu") print ("list1 :", list1)
The above code outputs:
Original list1: ['Google ', 1997]
After an element is added, the list1: ['Google ', 1997, 'baidu']
3. Delete list elements
You can use the del statement to delete the list elements, as shown in the following example:
List1 = ['Google ', 1997] print ("original list1:", list1) del list1 [1] print ("list1:", list1) after the second element is deleted)
The above code outputs:
Original list1: ['Google ', 1997] After deleting the second element, list1: ['Google']
4. Operators
The operators of list pairs + and * are similar to strings. + Is used to combine the list, and * is used to repeat the list.
As follows:
| Python expressions |
Result |
Description |
| Len ([1, 2, 3]) |
3 |
Length |
| [1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
Combination |
| ['Hi! '] * 4 |
['Hi! ', 'Hi! ', 'Hi! ', 'Hi! '] |
Repeated |
| 3 in [1, 2, 3] |
True |
Whether the element exists in the list |
| For x in [1, 2, 3]: print (x, end = "") |
1 2 3 |
Iteration |
5. Intercept and splice
List truncation and string operation types. We define the following list:
Language = ['C', 'c ++ ', 'python'] print ('read the third element language [2]:', language [2]) print ('read the second-to-last element language [-2]: ', language [-2] from the right) print ('language [1:]: ', language [1:])
The above code outputs:
Read the third element language [2]: python reads the second to last element language from the right [-2]: c ++ outputs the language [1:]: ['C ++ ', 'python'] of all elements starting from the second element.
6. nested list
Use the nested list to create other lists in the list, for example:
language = ['c', 'c++', 'python']run = ['game', 'billiards']roy =[language, run]print(roy)
The above code outputs:
[['c', 'c++', 'python'], ['game', 'billiards']]
Thought: How to output python in the nested list roy above