Nine, python basis: data container

Nine, python basis: data container

Simple introduction of three data container python

We speak in front of data types: numeric and string types are the most basic type of data, and usually with time, we can not each time only one type, and sometimes need to use containers to put them together, so we facilitate the data transfer and operation.

1, tuple tuple:

Example 5 The following is created tuples, this wrap with parentheses, which elements separated by commas, that is, the tuple.
Tuples can be nothing in it, it is an empty tuple.
Nothing can be put: integer data, floating point data, strings, and even data containers: lists, dictionaries, tuple, you can think what can go into it.

t = ()
t = (1,2)
t = (1, '2', 1.2, ['hello'])
t = (1, '2', (1, 3), { 'name' : 'xiaoming'})
t = (1,)

Note: when there is only one element of a tuple, you need to want to add a comma after the element

Characteristics of tuples are immutable: tuples once created, it can not be changed, it is no way CRUD

The so-called immutable, does not mean it can not do anything. But to create out after this thing, it is how is how kind, you can not make changes in its basis, but you can still put it inside something taken out or put it with other data together, that it is to perform certain operations. But this is not to change it, but to get him to create something new.

Related operations tuples, built-in functions, specifically to see another article.

2, a list of list:

Separated by a comma in parentheses wrap, which the element is a list, and it is similar with the tuple, you can put various types of data.

l = []
l = [1, 2, '3', {1:2, 3:4}, ('hello','小明'), [1, 3, 4]]

It tuples with different places is that it can be changed, relevant modification method, then change it or it.

A list of related operations, built-in functions, specifically to see another article.

3, the dictionary dict:

Surrounded by a brace, internal separated by commas, it is a dictionary, and it is the same with the above, but also can put various types of data and data container

dict = {}
dict = {1:2, 'name': 'xiaoming', 'age': 12}

It is with a tuple, a list of different places that it is the key to the structure, one pair, separated by commas, each pair in front of the colon, is the key to key, is the value after the colon. We usually use it to store data one relationship, for example, to one person, we usually use

{'name': 'xiaoming', 'age': 12}

This structure corresponds to the personal information store, can be removed when the key corresponding to the extracted value.

Dictionary of related operations, built-in functions, specifically to see another article.

Published 47 original articles · won praise 74 · views 7893

Guess you like

Origin blog.csdn.net/Jacky_kplin/article/details/104784826