Python Learning Route 16--tuples

Source: Internet
Author: User
Tags shallow copy

Meta-grouptuples and lists are very similar. Tuples are different from lists. The tuple uses parentheses and the list is square brackets. And tuples are immutable types.
1 Creating tuples and assigning values

2 Accessing the value of a tupleAn element that accesses a tuple requires a slice operation of a tuple, and a tuple's slice operation is the same as a list.

3 Updating tuplesbecause tuples are immutable types, programmers cannot update the elements of tuples. To update a tuple, you must construct a new tuple with the old tuple fragment.
>>> Atupe = atupe[0], atupe[1]>>> atupe (1, ' both ')

4 deleting tuplesdel atuple
operation of Tuplestuples and lists, with duplicate *, connect +, member in, slice operations. same built-in functions as str,max,min,cmpbut tuples do not have a list of sort, add, replace, etc. operations, because tuples are immutable.
copy Python objects, shallow copies, and deep copiesan object assignment is actually a simple object reference. In other words, when you create an object and then assign the object to another object, Python does not copy the object, but instead copies the reference to the object. Let's take a look at a shallow copy
>>> hubby = person[:]>>> wifey = List (person) >>> [ID (x) for x in person, hubby, wifey][14003252 4226712, 140032524226784, 140032524227144]>>> hubby[0] = ' Joe ' >>> wifey[0] = ' Jane ' >>> hubby , Wifey ([' Joe ', [' Saving ', 100.0]], [' Jane ', [' Saving ', 100.0]]) >>> hubby[1][1] = 50.00>>> hubby, wifey ([' Joe ', [' saving ', 50.0]], [' Jane ', [' saving ', 50.0]]) >>> [ID (x) for x in hubby][140032524156760, 1400325241445 12]>>> [ID (x) for x in wifey][140032524223856, 140032524144512]>>>
When hubby's account balance becomes 50,wifey's account balance becomes 50. But why are the names of the two accounts different?The string is immutable, and when the account name is re-assigned, the two accounts refer to two different string objects. But the list is a mutable type, and two accounts refer to the same list object. We can judge by observing their ID values. As can be seen from the above results, two accounts refer to different objects, but the small list within two accounts is the same list object referenced. This explains our doubts.
let's see the deep copy .deep copy needs to use this function--copy.deepcopy ()




Python Learning Route 16--tuples

Related Article

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.