Learning environment:
Ubuntu 18.04 system + Geany editor Python 3
Pyhton3 installation:
$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python3.5
Geary editor installation:
$ sudo apt-get install geany
Catalog:
- First, list
- 1. create
- 2. visit
- 3. Processing operation
- 1) Modify element
- 2) Add element
- 3) Delete element
- 4) Sort list
- 5) Print list
- 6) Calculated length
- 4. tuple
First, list
1. create
(1) Character list
List name = ['element 1', 'element 2' 'element n-1', 'element n']
(editor display error, single quotation mark of English input method expected)
litters = ['a','b','c']
(2) Number list
Almost any number set you need can be created with range()
<1> Use the function range()+list()
list() converts the result of range() directly to a list
numbers = list(range(1,6)) #Be careful:range(1,6)Access only numbers12345,No,6 print(numbers)
numbers = list(range(1,11,2)) #functionrange()from1Start counting, keep adding2,Until reaching or exceeding11 print(numbers)
<2> Use the function range()
squares = [] #Create an empty list first for value in range(1,11): #sendrange()ergodic1-10 squares.append(value**2) #Square the current value and add it to the end of the empty list print(squares)
The simplified version is as follows:
squares = [value**2 for value in range(1,11)] print(squares)
2. visit
(1) Forward index access
Note: the element index in the list starts from 0!
letters = ['a','b','c'] print(letters[0]) #First element of output list print(letters[1]) #Second element of output list message="The first letter is "+letters[0]+'.' print(message)
(2) Backward index access
Note: index elements in the list start with - 1!
letters = ['a','b','c'] print(letters[-1]) #Last element of output list print(letters[-2]) #The penultimate element of the output list message="The last letter is "+letters[-1]+'.' print(message)
3. Processing operation
1) Modify element
List name [element index] = 'change content']
(editor display error, single quotation mark of English input method expected)
letters = ['a','b','c'] print(letters) letters[0]='M' #Change the first element in the list to M print(letters)
2) Add element
append() can add elements to the end of the list
insert() can add elements anywhere in the list
letters = ['a','b','c'] print(letters) letters.append('T') #Add T to the end of the list print(letters) letters.insert(1,'K') #Add K to index as1Location print(letters)
3) Delete element
(1) del statement delete a location element
letters = ['a','b','c'] print(letters) del letters[1] #Delete index as1Elements print(letters)
(2) pop() to delete (pop up) a location element
letters = ['a','b','c'] print(letters) print(letters.pop()) #Delete the last element of the list and output it print(letters) print(letters.pop(1)) #Delete list index as1And output the element print(letters)
(3) remove() deletes a specified element
Note: only the first specified element can be deleted
letters = ['a','b','c','a','k'] print(letters) letters.remove('a') #Delete list element a print(letters)
4) Sort list
(1) sort() permanent sort
letters = ['a','c','m','b','k'] print(letters) letters.sort() #Sort alphabetically print(letters) letters.sort(reverse=True) #Sort alphabetically in reverse order print(letters)
(2) sorted() temporary sort
letters = ['a','c','m','b','k'] print(letters) print(sorted(letters)) #Output temporarily sorted list print(letters)
5) Print list
(1) The entire list includes symbols and elements printed completely
letters = ['a','c','m','b','k'] print(letters) #Print list #Reverse list order and print letters.reverse() print(letters)
(2) Print out list elements only
letters = ['a','c','m','b','k'] for letter in letters: print(letter)
6) Calculation length len()
letters = ['a','c','m','b','k'] print(len(letters))
7) Statistical maximum (list of numbers)
numbers = list(range(1,11)) print(max(numbers)) print(min(numbers))
8) slice
Some elements of the processing list are called slices
List name [A: b]
Only the elements of list index from a to b-1 are processed. When a is not entered, it starts from the first element by default; when b is not entered, it ends at the last one by default
letters = ['a','m','d','f','k','g'] print(letters[:]) #Output all elements print(letters[:2]) #Before output2Element print(letters[-3:]) #Output final3Element print(letters[1:4]) #Output index1To index3Elements
9) Copy list
(1) Section method
More flexible. Changing leeters after copying commands will not affect copy
letters = ['a','m','d','f'] copy = letters[:] #Create a slice containing all lists to form a new list copy letters.append('K') print(letters) print(copy)
(2) Direct replication
If you change the letters after copying the command, the copy will also change, that is, the copy is always equal to the letters
letters = ['a','m','d','f'] copy = letters letters.append('K') print(letters) print(copy)
4. tuple
1) definition
Python calls immutable values immutable, and immutable lists tuples
2) comparison
The list uses [] square brackets. After definition, you can modify the list elements
Tuples use () parentheses. Tuple elements cannot be modified after definition (but can be redefined)
letters = ('a','m','d') print(letters) for letter in letters: print(letter) letters = ('L','k','R') #Redefinition print(letters) for letter in letters: print(letter)