Python 5 data structure

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/known/archive/2010/09/03/1817499.html

Python has three built-in data structures, lists, tuples, and dictionaries

1. Lists (list)

The list is denoted by a bunch of square brackets "", and you don't want the data to be separated by commas, and once you've created a list, you can add, delete, or search for it.

So the list can be changed.

2. Tuples

Tuples and lists are similar, but tuples cannot be changed! Tuples are represented by (), and each data is separated by commas.

Tuples are often used to make statements or user-defined functions safe to take a set of worthwhile moments,

3. Dictionary (dict)

Key combination, the key must be unique, use immutable objects (such as strings), and the values can be mutable, immutable objects.

The dictionary is denoted by {}, separated by commas between each key-value pair, separated by a colon between the values.

4. Sequence-"list, tuple, string

The two main features of a sequence are indexes and slices (eg.substring)

The index allows us to fetch a specific item from the sequence.

Slicing allows us to get a slice of the sequence, which is part of the sequence

Shoplist[-1]: Represents the last element of a sequence

Shoplist[-2]: The second-to-last element of a sequence

Negative, the position is calculated from the end of the sequence.

The start position is contained in the sequence slice, and the end position is excluded from the slice

The colon is a must

5. Objects and references

shoplist =[‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘]mylist =shoplist   # mylist is just another name pointing to the same object!

Del Shoplist[0] # This deletion would delete both shoplist and mylist

Make a copy of Shoplist

MyList = shoplist[:]

Del Mylist[0] # This del would not affect original shoplist

6. String functions

Python 5 data structure

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.