A list of python basics and python Basics

Source: Internet
Author: User

A list of python basics and python Basics

  • List Function

The list function converts other types of sequences to a list, as shown in figure

>>> List ("hello world ")
['H', 'E', 'l', 'l', 'O', '', 'w', 'O', 'R', 'l ', 'D']

  • List operations

Element assignment can change the list, as shown in figure

>>> 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 delete elements from the list, as shown in figure

>>> Sen
['H', 'E', 'l', 'l', 'O', '', 'w', 'O', 'R', 'l ', 'D']
>>> Del sen [0]
>>> Sen
['E', 'l', 'l', 'O', '','', 'w', 'O', 'R', 'l', 'D']

You can assign values to multiple elements at a time, as shown in figure

>>> Sen
['E', 'l', 'l', 'O', '','', 'w', 'O', 'R', 'l', 'D']
>>> Sen [: 3] = list ("hhhh ")
>>> Sen
['H', 'h', 'O', '', 'w', 'O', 'R', 'l ', 'D']

You can insert a sequence into the multipart assignment, as shown in figure

>>> 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 directly modify the original list.

Count -- count the number of times an element appears in the list

Extend -- append multiple values of another sequence at the end of the list to directly modify the original list.

>>> Sen = list ("world ")
>>> Sen. extend (list ('er '))
>>> Sen
['W', 'O', 'R', 'l', 'D', 'E', 'R']

Index -- locate the index location of the first matching item of a value from the list

>>> Sen
['W', 'O', 'R', 'l', 'D', 'E', 'R']
>>> Sen. index ('R ')
2

Insert -- insert an object to the list

Pop -- remove an element from the list (the last one by default) and return the value of this 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 -- remove the first match of a value in the list

>>> Sen
['O', 'R', 'l', 'D', 'E']
>>> Sen. remove ('l ')
>>> Sen
['O', 'R', 'D', 'E']

Reverse -- store the elements in the list

Sort -- sort the list in the original position and return a null value

Sorted -- get a copy of the sorted list. This function is used for any sequence.

>>> Sen_sorted = sorted (sen)
>>> Sen_sorted
['D', 'E', 'O', 'R']
>>> Sen
['O', 'R', 'D', 'E']

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.