python2.7 Getting Started---lists (list)

Source: Internet
Author: User
Tags python list

The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on. Python has 6 built-in types of sequences, but the most common are lists and tuples. Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements. A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket. The data items of the list do not need to have the same type. Create a list by enclosing separate data items separated by commas in square brackets. Consider the example:

List1=[‘Physics‘,‘Chemistry‘,1997,2000]List2=[12345< Span class= "Hl-code" > ] List3 = [ "a "" b" "c" "d" "

As with the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on. Next, we'll use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters in the list:

#!/usr/bin/python List1=[‘Physics‘,‘Chemistry‘,1997,2000]List2=[1,2,3,4,5,67 ] print "list1[0": " Span class= "Hl-identifier" >list1[0 print "list2[1:5]: " list2[1:5 /span>

The result of the above example output is:

List1[0]:  physicslist2[1:5]:[2,3,4 ,5]                 

We can also modify or update the data items of the list, or you can use the Append () method to add the list items as follows:

#!/usr/bin/python#-*-Coding:utf-8-*- List = [] ## empty list List.append ( ' Google ") ## use Append () to add elements Span class= "Hl-code" > List. Append ( ' Luyaran " Print list

The result of the above example output is:

[' Google ',' Luyaran ']   

And then, let's use the DEL statement to delete the elements of the list, as in the following example:

#!/usr/bin/python List1=[‘Physics‘,‘chemistry ' 1997< Span class= "Hl-code", 2000] print list1 Del list1[2] print "after deleting value at index 2: " Print list1

The result of the above example output is:

[ ' physics '  ,   ' chemistry ' , 1997< Span class= "pun" >, 2000] after deleting value at index 2 :[ ' physics ' , span class= "str" > ' chemistry ' , 2000  

We'll look at the script operator for the list next. First, however, the list-to + and * operators are similar to strings. The + sign is used for the combined list, and the * number is used for repeating lists. Well, look at the table:

Python Expressions Results Description
Len ([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
[' hi! '] * 4 [' hi! ', ' hi! ', ' hi! ', ' hi! '] Repeat
3 in [1, 2, 3] True Whether the element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 Iteration

Take a look at the Python list interception:

>>>L=[‘Google‘,‘Runoob‘,‘Taobao‘]>>>L[2]‘taobao ' >>> l[-2] ' runoob Span class= "Hl-code", "taobao >>>

This is a description of the above code:

Python Expressions Results Description
L[2] ' Taobao ' Read the third element in the list
L[-2] ' Runoob ' Reads the second-to-last element in a list
L[1:] [' Runoob ', ' Taobao '] To intercept a list starting with the second element

For list python, the following functions are included:

Serial Number function
1 CMP (List1, List2)
Compare two elements of a list
2 Len (list)
Number of list elements
3 Max (list)
Returns the maximum value of a list element
4 MIN (list)
Returns the minimum value of a list element
5 List (seq)
Convert a tuple to a list

For list Python, the following methods are included:

ordinal method
1 list.append (ob j)
Add a new object at the end of the list
2 list.count (obj)
count the number of occurrences of an element in the list
3 list.extend (seq)
Append multiple values from another sequence at the end of the list (expand the original list with a new list)
4 list.index (obj
Find the index position of the first occurrence of a value from the list
5 list.insert (index, obj)
Insert an object into the list
6 list.pop (obj=list[-1])
removes an element from the list (the last element by default) and returns the value of the element
7 lis T.remove (obj)
Remove the first occurrence of a value in the list
8 list.reverse ()
Reverse list elements
9 list.sort ([func])
Sorts the original list

OK, here, the content is almost over, if you feel good, please more praise support oh ...

Original link: 79911003

python2.7 Getting Started---lists (list)

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: 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.