A list of the basic Python tutorial notes

Source: Internet
Author: User

    • List function

The list function converts a sequence of other types to a list, such as

>>> list ("Hello World")
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']

    • List operations

Element assignments can change the list, such as

>>> Sen
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']
>>> sen[0] = ' H '
>>> Sen
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']

Use Del to remove elements from the list, such as

>>> Sen
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']
>>> del Sen[0]
>>> Sen
[' E ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ']

Shard assignments can assign values to multiple elements at once, such as

>>> Sen
[' E ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ']
>>> Sen[:3] = List ("HHhH")
>>> Sen
[' H ', ' h ', ' h ', ' h ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']

A shard assignment can also insert a sequence, such as

>>> sen = list ("World")
>>> sen[0:0] = list ("Hello")
>>> Sen
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']

    • List method

Append--Append a new object to the end of the list and modify the original list directly

Count--count the number of occurrences of an element in the list

Extend--Append multiple values of another sequence at the end of the list, modifying the original list directly

>>> sen = list ("World")
>>> sen.extend (' er ')
>>> Sen
[' W ', ' O ', ' r ', ' L ', ' d ', ' e ', ' R ']

Index--Find out from the list where the first match of a value is indexed

>>> Sen
[' W ', ' O ', ' r ', ' L ', ' d ', ' e ', ' R ']
>>> Sen.index (' R ')
2

Insert--Inserts an object into the list

Pop-Removes an element from the list (the last one by default) and returns the value of the element

>>> Sen
[' W ', ' O ', ' r ', ' L ', ' d ', ' e ', ' R ']
>>> Sen.pop ()
' R '
>>> Sen
[' W ', ' O ', ' r ', ' L ', ' d ', ' e ']
>>> Sen.pop (0)
' W '

Nameerror:name ' Seb ' is not defined
>>> Sen
[' O ', ' r ', ' L ', ' d ', ' e ']

Remove-Removes the first occurrence of a value in the list

>>> Sen
[' O ', ' r ', ' L ', ' d ', ' e ']
>>> sen.remove (' l ')
>>> Sen
[' O ', ' R ', ' d ', ' e ']

Reverse--returning the elements from the list to the home store

Sort-Sorts the list at the original location, returning a null value

Sorted--Gets a sorted copy of the list, which is used for any sequence

>>> sen_sorted = sorted (sen)
>>> sen_sorted
[' d ', ' e ', ' o ', ' R ']
>>> Sen
[' O ', ' R ', ' d ', ' e ']

A list of the basic Python tutorial notes

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.