One, for loop
For a more efficient output list of each data, you can use loops to complete
Code:
A = ['xiaowang','xiaozhang','xiaohua ' for in A: Print tempname
Execution Result:
Xiaowangxiaozhangxiaohua
Second, the list of related operations
1. Add element ("increment" append)
#define variable A, which has 3 elements by defaultA = ['Xiaowang','Xiaozhang','Xiaohua']Print "before-----added, the data for list a-----" forTempnameinchA:PrintTempname#hints, and add elementstemp = Raw_input ('Please enter the name of the student you want to add:') a.append (temp)Print "After the-----is added, the data for list a-----" forTempnameinchA:PrintTempname
2, modify the element ("change")
When modifying an element, use the subscript to determine which element is being modified before you can modify it.
Code:
#define variable A, which has 3 elements by defaultA = ['Xiaowang','Xiaozhang','Xiaohua']Print "before-----Modification, the data for list a-----" forTempnameinchA:PrintTempname#modifying elementsA[1] ='Xiaolu'Print "after-----modification, the data for list a-----" forTempnameinchA:PrintTempname
Execution results
Before-----modification, the data for List a----- xiaowang xiaozhang Xiaohua -----Modified, the data for List a----- Xiaowang Xiaolu Xiaohua
3. Find Element ("Check" in, not in)
You can add elements to a list by append
#define variable A, which has 3 elements by defaultA = ['Xiaowang','Xiaozhang','Xiaohua']Print "before-----added, the data for list a-----" forTempnameinchA:PrintTempname#hints, and add elementstemp = Raw_input ('Please enter the name of the student you want to add:') a.append (temp)Print "After the-----is added, the data for list a-----" forTempnameinchA:PrintTempname
4. Delete Element ("delete" Del, pop, remove)
In real life, if a classmate shifts, then the name of the student should be removed after the walk, and it is often used to remove this feature in development.
Common ways to delete list elements are:
- Del: Delete according to subscript
- Pop: Delete last element
- Remove: Delete based on the value of the element
Code:
Moviename = ['Pirates of the Caribbean','Hacker Empire','first drop of blood','Lord of the rings','The Hobbit.','Speed and passion']Print 'before------Delete------' forTempnameinchMoviename:PrintTempnamedelMoviename[2] Print 'after------delete------' forTempnameinchMoviename:PrintTempNam
"Code Learning" PYTHON list loop traversal and list common operations