Python list basic syntax

Source: Internet
Author: User

1.Lists can hold any data type, elements can be duplicated, and lists can be nested. Can be added to the check operation.
2. Create a list:
L= "" or l=list () creates an empty list;
3, query or value:
Ex:l= "A", "a", "B", "C", "
The index or subscript value, starting from left to right subscript from 0, starting from right to left from 1. L "0" =1,l "-1" = "C".
4. Find the index value of the element:
Find the index value for B: L.index ("b") = 4, if there are duplicate elements, the first index value is returned by default.
5. Number of statistical elements:
L.count (1) = 3.
6, Slice: Only by index number from small to large cut, from left to right cut.
L "2:4" = "3," a "": Remove the element labeled 2 to 3, Gu Tou regardless of the tail.
L ": 3" = "0", take out the elements marked with 2
L "4:" = "" B "," C "," N "take out the element labeled 4 to the last.
L " -5:-1" = "A", "B", "C", 1 "remove the element from the subscript 5 to-1.
L "-5:" = "" A "," B "," C "," Mark "take out the subscript from 5 to the last element.
L ":" = "a", "a", "B", "C", "All" takes out all the elements.
L "2:7:2" = "3," B ", 1" press Step 2, jump one, then remove the elements from 2 to 7.
7. Modify the elements:
L.append ("PW"): Append "PW" to the last side of the list.
L.insert (2, "Rick"): Insert "Rick" before the second subscript.
8. Replace elements:
L "2" = "Pengwei", the element labeled 2 in the L list (3) is replaced with "Pengwei".
L "2:5" = "8,6,5" replaces the values in the list labeled 2 to 4 with "8,6,5".
9. Delete the element:
L.pop (): Default bar list The last element is removed and deleted.
L.remove ("B"): Bar the list of element B is deleted, but if there are duplicates, the first one is deleted by default.
Del L "2": the element in the bar list labeled 2 is deleted.
Del L "2:5": Bar list Subscript from 2 to 4 is removed.
Del L: Remove the list L.
10. Circular list::
For I in L:
Print (i) prints all the elements in the list sequentially.
Range (10): Automatically generates a list of 0 to 10.
For I in range (10): Loop output 0 to 9. (Gu Tou regardless of tail)
The difference between for and while is that while can be defined as a dead loop: while true;for loops have boundaries.
11. Sort the list:
L.sort (): Sorts elements of the same type from small to large in the ASCLL table order, and does not support the arrangement of different types of elements.
L.reverse (): Sort the list in reverse order.
12, the list of splicing:
Two lists are added directly: l+m
L.extend (m): Adds the M list to L.
13, the list of clear:
L.clear: Clears the list.
14. Replication of the list:
L=p: Unlike variables, l=p means to assign P values to L, and when p changes, l also changes.
If l do not change with P: with L=p.copy (), then p changes, L is unchanged, p and L are independent.

Python list basic syntax

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.