Data Structure of Python syntax Learning

Source: Internet
Author: User

Data Structure

Python has three built-in data structures: List, tuples, and dictionary.

1: List:

ListIs to process the data structure of a group of ordered projects, that is, you can store a series of projects in a list, in Python, you are separated by commas between each project

The items in the list should be included in square brackets so that python knows that you are specifying a list. For example, you can see that the list can contain strings or numbers. It can contain multiple types.

Related Function: Len (list) length list. append ('Rice ') after listing, add a string list. Sort () sorting list [0] to retrieve the first string.

The list is a variable data type:

For example:

Output:

This method affects the List itself, but the sort method does not return a modified list.

For example:

Output:

Note:You can addObjects of any typeIncluding number or other lists

 

2: tuples:The tuples are defined by commas (,) in parentheses. Tuples are usually used to securely use a group of values for statements or user-defined functions,That is, the value of the used tuples will not change..

For example:

Output:


Note:Tuples are the same as strings, that is, you cannot modify them.. For example, zoo [0] = "Others" also reports an error. The defined tuples cannot be changed.

3: Dictionary

Concept:Key(Name) andValueThe keys must be unique. Note that you can only use immutable objects (such as strings) as Dictionary keys.

Operation:

Add, delete, and modify, such:

Output:

Modify the value of DT:

Output:

Use dictionaryitemsTo read each key/value pair in the dictionary.

Output:

UseinOperator to check whether a key/value pair exists, or usedictClasshas_keyMethod.

Output:

Summary: items in the list and dictionary are variable. The values of tuples and strings are immutable.

 

 

 

 

 

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.