The following is to share a Python create a list and add elements to the implementation of the list, has a good reference value, I hope to help you. Come and see it together.
Today's learning content is related to the list in Python.
I. Create a list
1. Create a normal list
>>> Tabulation1 = [' The Holy Man ', ' canopy ', ' roll the curtain ']>>> tabulation1[' st ', ' canopy ', ' roller blinds ']
>>> Tabulation2 = [72,36,18]>>> tabulation2[72, 36, 18]
2. Create a mixed list
>>> Mix tabulation = [' The Holy Man ', 72, ' canopy ', 36]syntaxerror:invalid syntax>>> mixtabulation = [' The sage ', 72, ' canopy ', 36 ]>>> mixtabulation[' Holy Sage ', 72, ' canopy ', 36]
3. Create an empty list
>>> empty = []>>> empty[]
There are three ways to introduce you, and next, what do you do if you want to add elements to the list?
Two. Adding elements to the list
1.append
>>> tabulation1.append (' Purple Chardonnay ') >>> tabulation1[' Holy sage ', ' canopy ', ' roller blinds ', ' Violet Chardonnay '
2.extend
>>> tabulation1.extend ([' Purple Xia ', ' Qing Xia ']) >>> tabulation1[' The holy man ', ' canopy ', ' roller blinds ', ' Purple Chardonnay ', ' Purple Chardonnay ', ' green Chardonnay '
There is a way to expand the list with extend, and it is important to note that this method uses a list to expand the list instead of adding elements directly, so add "[]" to "()".
3.insert
>>> Tabulation1.insert (1, ' Purple Chardonnay ') >>> tabulation1[' Holy sage ', ' Purple Chardonnay ', ' canopy ', ' roller blinds ', ' Purple Chardonnay ', ' Purple Chardonnay ', ' green Chardonnay '