Python notes--tuples

Source: Internet
Author: User

Meta-group Tuple

Tuples and lists are very similar, but they cannot be modified once the list is initialized.

What is the meaning of immutable tuples? Code is more secure because tuples are immutable. If possible, you can use a tuple instead of a list as much as possible.

First, Defining tuples

>>> tup1= (' A ', ' B ', ' C ', ' 1 ') >>> tup2= (' A ', ' B ', ' C ', []) >>> tup3= (,)

The elements of a tuple are enclosed in parentheses, and the things to note are as follows:

1.tup2 are "mutable", for example:

>>> tup2[3][2]=4>>> tup2 (' A ', ' B ', ' C ', [1, 2, 4])

On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of atuple is directed to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!

2. A tuple of only 1 elements must be defined with a comma, and if no commas are treated as variables.

Second, Basic Operations

The count () and index () methods and slices are used the same way as lists.

Refer to: http://fengjicheng.blog.51cto.com/11891287/1927616


This article is from the "Network Technology" blog, please be sure to keep this source http://fengjicheng.blog.51cto.com/11891287/1927624

Python notes--tuples

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.