Summary of some basic operation knowledge in the Python list, and basic python operations

Source: Internet
Author: User
Tags element groups python list

Summary of some basic operation knowledge in the Python list, and basic python operations

The most basic data structure of Python is a sequence (list/tuples ). Each element in a sequence is assigned a number-its location 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 is list and element groups, which we will see in this tutorial.

There are some things that can be done using all sequence types. These operations include indexing, slicing, adding, multiplication, and checking members. In addition, Python has built-in functions to search for the length of a sequence and its largest and smallest elements.
Python list:

List is the most common data type. In Python, you can write a comma-separated value square brackets (Project) list. Items in a list are not necessarily of the same data type.

To create a list, you only need to separate the values of squere brackets with commas. For example:

list1 = ['physics', 'chemistry', 1997, 2000];list2 = [1, 2, 3, 4, 5 ];list3 = ["a", "b", "c", "d"];

For example, for string indexes, the list indexes start from 0 and list slice and connection.
Value in the access list:

For the value in the access list, use square brackets to index or index along the slice to obtain the value corresponding to the available index. The following is a simple example:

#!/usr/bin/pythonlist1 = ['physics', 'chemistry', 1997, 2000];list2 = [1, 2, 3, 4, 5, 6, 7 ];print "list1[0]: ", list1[0]print "list2[1:5]: ", list2[1:5]

When the above code is executed, the following results are generated:

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

Update list:

You can update one or more elements in the list on the left of the slice assignment operator, and add elements to the list using the append () method. The following is a simple example:

#!/usr/bin/pythonlist = ['physics', 'chemistry', 1997, 2000];print "Value available at index 2 : "print list[2];list[2] = 2001;print "New value available at index 2 : "print list[2];

Note: The append () method will be discussed in subsequent sections.

When the above code is executed, the following results are generated:

Value available at index 2 :1997New value available at index 2 :2001

Delete the elements in the list:

To delete the list elements, you can use the del statement. If you know which elements are to be deleted, or if you do not know which elements are to be deleted, use the remove () method. The following is a simple example:

#!/usr/bin/pythonlist1 = ['physics', 'chemistry', 1997, 2000];print list1;del list1[2];print "After deleting value at index 2 : "print list1;

When the above code is executed, it will produce the following results:

['physics', 'chemistry', 1997, 2000]After deleting value at index 2 :['physics', 'chemistry', 2000]

Note: The remove () method will be discussed in subsequent sections.
Basic list operations:

The + and * operators are listed as strings. The concatenation and repetition strings are also listed here. The difference is that the result is a new list instead of a string.

In fact, the list responds to all the general operation orders we use in strings.

Index, slice, and matrix:

Because list sequences, indexes and slices work in a similar way as they operate on strings.

Assume the following input:

L = ['spam', 'Spam', 'SPAM!']

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.