Beginners Learn python (one) list basic operations

Source: Internet
Author: User

Want to write something to the process of learning Python to record, so there is a rookie learn python ...


List lists are often used in writing programs, and are data structures used to process a set of ordered items.

For example, range (1,5) is actually a list of integers

So

For x in range (1,5)
can be written
For x in [1,2,3,4]

Here are some common basic operations for lists

Create a list of empty lists first

list = []


Add a value to this list with append

List.append (1)                     #向list里面添加1

You can add multiple values to the middle of a list by extend

List.extend ([2,3,4,5,6])            #向list中一次性添加2, 3,4,5,6


You can remove a known value from the list by removing

List.remove (6)                #将list中的6删除

If I want to access a value in the list, I need to know his location, and the values in the list have an attribute that can be understood as the position number, starting with 0

For example, in the list I want to know what the 2nd value is, just need

LIST[1]

Known location, you can also modify the value to modify the first value in the list to ' Frist '

List[0] = ' frist '

can be removed to remove the frist that was just inserted

Del List[0]

Can insert data, insert ' 0 ' in the first position

List.insert (0, ' 0 ')

If you only know what the value is, you need index ()

List.index (3) List.index (4)



Beginners Learn python (one) list basic 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.