Analysis of Operation instances of Python tuples [Create, assign, update, delete, etc.], and analysis of python instances

Source: Internet
Author: User
Tags python list

Analysis of Operation instances of Python tuples [Create, assign, update, delete, etc.], and analysis of python instances

This example describes how to operate Python tuples. We will share this with you for your reference. The details are as follows:

# Coding = utf8''' tuples are another type of container that is very similar to the list. Tuples are an unchangeable type. Once created, elements cannot be modified. Because of this feature, tuples can be used as a dictionary key. When processing a group of objects, this group is of the tuples by default. ''' Create a tuple and assign a value ''' # create and assign a value to a tuple tuple_1 = (1, 2, 3, "ewang", "demo ") # create an empty tuple_2 = () # Use tuple to create a tuple and assign tuple_3 = tuple ("hello") to the tuple_2 = ") # using tuple to create an empty tuple_4 = tuple () ''' access the value ''' in the tuples # using the index to use the value print tuple_1 [0] In the tuples, tuple_1 [2], tuple_1 [4] # Use the value print tuple_3 [0: 3], tuple_3 [0:], tuple_3 [:] # The following operation cannot output the entire tuple value # The last element cannot output print tuple_3 [:-1] '''. The tuples are of an unchangeable type, elements of tuples cannot be updated or changed. The construction of a new string using the fragments of an existing string is equivalent to the operation of updating tuples. ''' # Update tuple_1 = tuple_1 [0], tuple_1 [2], tuple_1 [4] print tuple_1 # It is impossible to update tuple_1 = tuple_1 [0: 2] print tuple_1 ''' to delete a single tuples. Of course, it is no problem to re-form a tuples after removing unnecessary elements. To delete an entire tuple explicitly, you only need to use the del statement to reduce the object reference count. When the reference count reaches 0, the object will be destructed. In most cases, you do not need to use del to delete an object. Once it is out of its scope, it will be destructed. '''Try: del tuple_1 print tuple_1partition t Exception, e: print "The tuple_1 not exists", e ''' other operations on tuples are similar to those on The list, do not repeat the test. For relevant source code, you can view the operation instructions on the list. '''

Running result:

For more Python-related content, refer to this topic: getting started with Python and advanced classic tutorial, Python string operation skills summary, and Python list) operation Skills summary, Python coding skills summary, Python data structure and algorithm tutorial, Python function usage skills summary, and Python file and directory operation skills Summary

I hope this article will help you with Python programming.

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.