Python tuple creation assignment and instance analysis of the update delete operation

Source: Internet
Author: User
Tags python list
This article mainly introduced the Python tuple operation method, combined with the concrete instance form to analyze the python's creation, assignment, update, deletion and other operation methods and related considerations, the need for friends can refer to the following

The examples in this article describe Python tuple operations. Share to everyone for your reference, as follows:


#coding =utf8 "is another container type that is very similar to the list. A tuple is an immutable type that, once created, cannot be modified by its elements. Because of this feature, tuples can do a dictionary key. This group is the tuple type by default when working with a set of objects. "' Create tuples and assign values to ' #创建并对一个元组赋值tuple_1 = (" Ewang "," demo ") #创建一个空的元组tuple_2 = () #使用tuple创建一个元组并给元组赋值tuple_3 = Tuple ("Hello") #使用tuple创建一个空的元组tuple_4 =tuple () "" to access the values in the tuple ' ' #通过索引使用元组中的值print tuple_1[0], tuple_1[2], tuple_1[4]# Use a value from a tuple in a slice print tuple_3[0:3],tuple_3[0:],tuple_3[:] #使用如下操作无法输出整个元组值 # The last element cannot output print tuple_3[:-1] "' tuples are immutable types, Elements of tuples cannot be updated or changed. Fragments of an existing string are constructed as a new string in a way that is equivalent to updating a tuple operation. "' #通过索引更新tuple_1 =tuple_1[0],tuple_1[2],tuple_1[4]print tuple_1# by slicing tuple_1=tuple_1[0:2]print tuple_1 '" It is not possible to delete a separate tuple element. Of course, when you discard unwanted elements, it is no problem to re-form a tuple. To remove an entire tuple, simply reduce the object reference count with the DEL statement. When the reference count reaches 0, the object is refactored. Most of the time, you do not need to display the Delete object with Del, and it will be destroyed in its scope. "Try:  del tuple_1  print tuple_1except exception,e:  print" The Tuple_1 Not exists ", E" "for other operations on tuples, Similar to the General list, re-test do not repeat. The relevant source code can view the operating instructions for the list. '''

Operation Result:

More Python-related content readers can view the topic of this site: "Python Introductory and Advanced Classic Tutorial", "python string Operation tips Summary", "python list" Operation tips Summary, "Python Coding skills Summary", " Python data structure and algorithm tutorial, Python function Tips Summary, and Python file and directory Operations tips summary

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.