Python Learning Note 3: Lists and tuples

Source: Internet
Author: User

Python Learning Note 3: Lists and tuples

A list of lists

Summary: The list is a mutable variable, and you can modify the value of the element by subscript

1. Definition list --brackets []

name=[' Lily ', ' Lucy ', ' Meimei '

  

2, subscript value --plus or minus number can be

  

3. Add a value to the list

1)Append () method, add a value at the end of the list

2)Insert () method, add a value at the specified position in the list

3)Extend () method to append a list to the end of a list

4. Remove values from the list

1) Thepop () method, which deletes the last element by default, and returns its value; the subscript of the parameter list element, you can delete the element at the specified position

2) Remove() method, Delete the specified value, the argument is the value in the list

3) Clear () method, clear list

4) del keyword deletes the specified element

        

      

5. Modify the values in the list

Format: listname[n]=xxx

  

6, Slice, take sub-list

format: listname[n:m], remove the value between the mark N to M, also Gu Tou disregard the tail

  

   

7. Sorting

 1) Sort () method, ascending

2) Reverse () method, descending

  

8, the length of the list

Len () method to calculate the length of the list

  

9, the traversal of the list

The For loop iterates through the list, iterating through every element in the list

  

10. Other methods

1) index () returns the subscript of the element

  

2) count () Number of occurrences of the statistic element

  

Second, tuple tuple

Summary: tuples are immutable variables

1, definition, parentheses ()

name = (' Hello ', ' world ', ' python ')

2. Methods

Tuples have only two methods: count () Number of occurrences of the statistic element

Index () returns the subscript of an element

  

3. Note: Define tuples, if there is only one element, must end with a comma

  

Python Learning Note 3: Lists and tuples

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.