List of Python basics

Source: Internet
Author: User

This article focuses on the nature of the list in Python and some of the built-in methods of the list.

I. Definition and nature of the list 1. The definition of a list:

The list is a built-in in Python, and the keyword for a class in Python is the list. The definition of the listing is as follows:

li=[12.' Hello ', 344,[12,23, ' HSHS ']

The list is marked by the [] flag. The elements in the list are separated by ",". The elements of a list can be numbers, strings, tuples, dictionaries.

2. Nature of the list (1) list can find elements by index
li=[1,2,3,4]s=li[3]print[s]

The index of the list starts at 0, and the index of the last element is the length of the list-1.

(2) The list can be traversed through for and while loops
1 li=[1,2,45,45]2 s=len (li)3 i=04 for in li:5     print(item)6 while i<s:  7     print(li[i])8     i=i+1

The list is ordered so that it can be traversed by a while loop (the dictionary is unordered and therefore cannot be traversed with a while loop). Of course, the For loop traversal is also supported.

(3) The elements in the list can be basic data, i.e. data, strings, lists, dictionaries, tuples. (4) The list can be sliced
1 li=[12,34,56,67,89]2 li1=li[2:4]3print(LI1)

The list can be sliced to remove elements from the list, and the slice's index range is left-closed and right-open. In addition to querying, you can modify list elements by using list slices

(5) The elements of the list allow modification
1 li=[23,45,67,90]2 li[2]=1003print(LI)4 li[ 0:3]=[11,21,23]5print(LI)

The elements of the list can be modified either by index modification or by slice modification

(6) The elements in the list allow deletion

The elements in the list can be deleted by Del Li[i] or by the method provided by the Listl class. The method is discussed later, only the Delete method of Del is mentioned here.

1 li=[45,67,90,'defde', True,'huebfur' 2 del Li[0] 3 Print (LI) 4 del li[0:3]5print(LI)

Del can either be deleted for the index or deleted for the slice.

(7) Conversion between a list and a string

Lists and strings can be converted to each other. The string-to-list is relatively straightforward and can be converted directly:

s='hello's1=List (s)print(S1)

When you convert a string to a list, each character in the string becomes an element in the list.

The list-to-string cannot go directly, because if the direct transfer takes the "[]" marker of the list as part of the string. List-to-string, we can only write a function to convert:

1 li=[123,'sre', 567,true,'defe']2 s='3 for in Li:4     s=s+str (ILTM)  5print(s)

If the elements in the list have only strings, you can convert the list to a string by using the Join function:

1 li=['hello','dhuewu','CBUREUVR  ']2 s='. Join (LI)3print(s)
(8) Supplement: Once created, the string can no longer be modified

The string is a constant in Python (as in other languages), so once the string is created, it can no longer be modified, if you want to modify the string, it is equivalent to a copy of the string, so the modified string requires another variable to receive, the modification operation on the original string is invalid, The modification of the list is done for the original list, so there is no need for another variable to be received.

1 s='helloworld'# Original string 2 s1=s.upper ()#  Change a string to uppercase 3print(S1)4print(s)

Code Execution Effect:

Here is a new element at the end of the list:

1 li=[1,2,3,4,5,6]2 li1=li.append (9)3print(LI) 4 Print (LI1)

This is the result of his execution:

As can be seen, all actions on the list are made directly against the original list. The value received by the new variable is determined by the return value of the method.

3. Method of the list class (1) Append (value):

The append function adds an element at the end of the list

(2) Clear ():

Clear List

(3) Copy ():

Copy list (shallow copy)

(4) Count (value):

Calculates the number of times that value appears in the list.

(5) Extend (iterable):

The function passes in the parameter an iterative object (which can be used for looping an iterative object), and then adds the elements inside the iterated object to the end of the list. Note that this method differs from the Append method, and if the APPEND function passes through an iterative object, the iterator is added to the list as an integral element, and extend is the addition of each element of the iteration object to the list:

1 li=[1,2,3]2 li1=li.copy ()3 li.append ('hello'  )4 li1.extend ('hello')5Print  (LI)6print(LI1)

Here is the result of the execution:

(6) Index (Value,strat=none,end=none):

This method determines the index of the first occurrence of the lookup element in the list (left to right), which has two default parameters that specify the scope of the lookup. If the value does not exist in the list, an error will be

(7) Insert (Index,value):

Inserts a new value at the specified location in the list

(8) Pop (Index=none):

Deletes an element from the list. The default is to delete the last element of the list. The method has a default parameter that can be used to specify the location of the deletion.

(9) Remove (value):

Deletes the value (left-to-right) of the first occurrence in the list.

(reverse) ():

Flips the list. That is, the first element becomes the penultimate element, the second element becomes the penultimate element ...

(one) sort (key=none,reverse=false):

Sort the list elements by default, in ascending order, and if descending, set the reverse to True,key value the following department will discuss again and skip over.

List of Python basics

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.