Chapter VII python3 tuple tuple

Tuple tuple is a commonly used immutable sequence; said tuples and lists tuple list is very similar, except that it has been created tuples can not be changed.

Tuple elements really can not modify it? Tuples and lists what kind of difference in the end they have it? Let's go check it out:

First, define the tuple, which is in parentheses () to create the (,) are separated by commas Item element, example code:

 

 

 The operating results we find that the elements of the tuple item may be different types of objects, and this is the same as in list; a list on the list defined by the square brackets []; between all the elements of both use a comma (,) separated.

When only one tuple how to define the element, example code:

 

 

 From the above results, we conclude that: when the tuple within a tuple only element, the element must be added later in a comma (,); otherwise, the parentheses Python interpreter will be calculated as the operator, as a result a is an integer of 5; to eliminate this ambiguity, Python is only required when defining a tuple element, after the element must be a comma (,); this list and the list is not the same.

1 , tuple tuple indexing and slicing

Tuple of the tuple indexing and slicing method in list index and slicing method is substantially the same; follow [superscript head: tail subscript) "left off right open" principle, also called "pack left does not include the right" Principles :

 

 

 We modify the next one element:

 

 

 Uh? ! How they are also being given out? Tip tuple object does not support the element assignment.

Oh, this is what we often say "tuple tuple can not be changed once defined it."

2 , a tuple tuple nested

Example code:

 

 

 Us try tuple 'Java' changed to 'Go', example code:

 

 

 Uh? Do not say that a tuple can not be changed once defined after you, how they can change it? This is regarded people confused! Attentive friends will find that the changed element is a list type, because the list is a variable type;

Right, it is like this. If an element in the tuple is an immutable type of object, such as: constants, strings, etc., then the error will modify elements when these immutable tuple; if an element is a tuple mutable objects, such as: list list, etc., can be modified at this time, it will not error.

On the surface, the tuple element has indeed changed, but the change is not in fact an element tuple, but the list of elements.

We again an example, the analysis continued:

 

 

 The first two del () are being given, indicating the tuple once created the elements can not be deleted; in fact strings also have this property, once created, there is a single string can not be deleted.

Behind three times del () delete the child element is a variable element in the list, when the sub-element list variable elements are all removed (empty list []), if we continue to delete the wrong TypeError will be reported.

The above examples use the id () has been "monitoring" the last tuple element of a memory address change, found that no matter how removed, it has remained the same memory address.

What happens if we assign a variable to the last element? Example code:

 

 

 The above reported IndexError subscript bounds exception because tuple_org [-1] this time has an empty list [], and there is no element [0] represents a bit of the first element, therefore being given;

The whole process of the above example, no matter how the assignment will be to the last element of error: tuple_org [-1], you went to Thailand it is? Denatured?

Closer to home, in conjunction with the above examples and id () output function, we next analyzed:

Tuple tuple it is an immutable sequence, called tuples "immutable" refers to each element in the tuple memory point will never change it. All immutable sequence after the first definition of "memory point" can not be changed again, it contains references to other objects, and objects have these references may be variable or may be modified;

Although tuples element values can not be modified, but we can use = splicing of tuples,

Example code:

 

 

 After then + = append operation, memory address A group element is changed, because it came to the new elements reallocate the memory addresses.

3 , Python built-in functions

len () seeking the number of elements in the sequence, the sorted () sorting sequence, example code:

 

 

 Note: When sorted () built-in function to sort the tuples returned is not a new tuple object, but a sorted list list object, and tuple itself and there is no change;

Tuples into a list, example code:

 

 

 Although the element value tuples can not be deleted, but we can use the del statement to delete an entire tuple, example code:

 

 

 By the way, tuples not have their own way, but the list have their own method of operation.

4 , sum up

Tuple itself at the memory, is actually stored in the data memory address tuple collection of tuples, once established, the memory address of the collection can not be modified, and deleted. Once the address within the set changes, you must reassign the tuple space to save the new memory addresses.

There is also a version upgrade tuple: tuple named namedtuple, it is the function object collections module.

Named tuple object-oriented follow-up will be put in to tell you about, because it requires a combination of class objects to use in order to understand something to say. Interested friends can go to find out.

These examples and analysis of reading the above, I understand that immutable sequence of tuples of it is not deeper.

Guess you like

Origin www.cnblogs.com/tianyu2018/p/11605579.html