Basic learning of Python (ii)

Source: Internet
Author: User
Tags python list

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.

Data items for a list do not need to have the same type

Create a list by enclosing separate data items separated by commas in square brackets. As shown below:

List1 = [1, 2, 3, 4= ["a""b""C"  "D"];

As with the index of a string, the list index starts at 0. The list can be intercepted, combined.

Accessing values in a list

Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:

# !/usr/bin/env python # -*-coding:utf-8-*- # author:jerry Shilist1 = [1,2,3,4= [a,b,c,d]; Print (":", list1[0]) Print (":", List2[1:])
Update list

You can modify or update the data items in the list as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiList1 = [1, 2, 3, 4, 5]list2= ["a","b","C","D"]Print(List1[0]) list1[0]= 123list2[2] ='s'Print(LIST1,LIST2)
To delete a list element

You can use the DEL statement to delete the elements of a list, as in the following example:

# !/usr/bin/env python # -*-coding:utf-8-*- # author:jerry Shilist1 = [1, 2, 3, 4, 5 ]del list1[1]print (list1)
List of functions append () method description

The append () method is used to add a new object at the end of the list.

Grammar
List.append (obj)
Instance

The following example shows how the append () function is used:

# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jerry Shi  = [1, 2, 3, 4, 5 ]list1.append (555)print(list1)
The Count () method describes

The count () method is used to count the number of occurrences of an element in the list.

Grammar
List.count (obj)
Instance
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jerry Shi  = [1, 2, 3, 4, 5,135,5,2,4,2,2]print(List1.count (5))print( List1.count (2))
Extend () method description

The Extend () function is used to append multiple values from another sequence at the end of the list (the original list is expanded with a new list).

Grammar
List.extend (seq)
Instance
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jerry Shi  = [1, 2, 3, 4, 5= List (range (3))   # Create a listing of 0-2 list1.extend (list2)   #  Extended list print(list1)
Index () method description

The index () function is used to find the index position of the first occurrence of a value from the list.

Grammar
List.index (obj)
Instance
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiList1= ['1','2','3','4','5',]Print(List1.index ('3'))Print(List1.index ('5'))
Insert () method description

The Insert () function inserts the specified object into the specified position of the list

Grammar
List.insert (index, obj)
Instance
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiList1= ['1','2','3','4','5',]list1.insert (4,'haha')Print(List1)
Pop () method description

The pop () function removes an element from the list (the last element by default) and returns the value of the element.

Grammar
List.pop (Obj=list[-1])
Instance
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiList1= ['1','2','3','4','5',]list1.pop ()Print(List1) List1.pop (1)Print(List1)

#显示结果:

[' 1 ', ' 2 ', ' 3 ', ' 4 ']
[' 1 ', ' 3 ', ' 4 ']

Basic learning of Python (ii)

Related Article

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.