Summary of common data types in Python (i)

Source: Internet
Author: User

Python provides a variety of data types to hold data item collections, mainly including sequences (list and tuple tuple), mappings (such as dictionary dict), collections (set), which are described in one of these ways:

A sequence

1. List of lists

A list is an ordered collection of elements that are variable in relation to tuples and strings, and can be added and removed at any time.

(1) Create List

Test on the command line as follows:

>>> L1 = [1,2,3]>>> L1 [1, 2, 3]>>> L2 = [' abc ']>>> L2 [' abc ']>>> L3 = ["A", " B "," C "]>>> L3 [' A ', ' B ', ' C ']

Note that the string must be quoted.

It is very useful to create a list of strings through the list, such as:

>>> L = List ("Python") >>> L [' P ', ' y ', ' t ', ' h ', ' o ', ' n ']

(2) Visit list

Depending on the index, note that you cannot cross the line, which is particularly similar to arrays:

>>> l[0] ' P ' >>> l[-1] ' n '

(3) Adding new elements

Append the new element to the end of the list using the Append () method, and insert () to add a new element to a specific location.

(4) Deleting an element

The delete element can take the pop () method, execute L.pop () to delete the last element of the list, if it is a specific position can take the pop (2), 2 represents the position.

(5) Replacement

The substitution is simple, and the direct index is fine.

(6) Printing

>>> L = [' A ', ' B ', ' C ']>>> for I in L:print (i) a B C

Reprint to: (Strange _ Yang Source: http://www.cnblogs.com/ybjourney/p/4767726.html)

Summary of common data types in Python (i)

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.