"Code Learning" PYTHON list loop traversal and list common operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.