Home > Backend Development > Python Tutorial > Tuples Revealed: The Ultimate Guide to Immutable Containers in Python

Tuples Revealed: The Ultimate Guide to Immutable Containers in Python

王林
Release: 2024-03-24 16:11:02
forward
417 people have browsed it

元组揭秘:Python 中不可变容器的终极指南

Tuple is an immutable data structure in python, used to store ordered sequence of data. Similar to lists, tuples can contain a variety of element types, including numbers, strings, lists, and even other tuples. However, unlike lists, tuples cannot be modified.

Create tuple

    Use commas to separate elements and enclose them in parentheses, such as:
  • my_tuple = (1, "two", 3.14)
    Copy after login

  • Use built-in function
  • tuple()

    , such as: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:python;toolbar:false;">my_tuple = tuple([1, &quot;two&quot;, 3.14])</pre><div class="contentsignin">Copy after login</div></div>

Immutability

The main feature of tuples is their immutability. Once created, elements in a tuple cannot be modified, added, or removed. This makes tuples ideal for storing data that needs to be protected or as function parameters.

Accessing tuple elements

    Use subscripts, such as:
  • my_tuple[0] # 返回第一个元素
    my_tuple[-1] # 返回最后一个元素
    Copy after login

  • Use segmentation, such as:
  • my_tuple[0:2] # 返回前两个元素
    my_tuple[2:] # 返回从第三个元素开始的所有元素
    Copy after login

Operation tuple

Although tuples are immutable, there are still some operations that can be performed on them:

  • Splicing (`)

    : Connect two or more tuples, such as:

    new_tuple = my_tuple + (4, "five")
    Copy after login

  • Copy (`)

    : Create a new copy of the tuple, such as:

    new_tuple = my_tuple[:]
    Copy after login

  • Check membership (

    in): Check whether a value is contained in a tuple, such as:

    if "two" in my_tuple:
    print("Found "two" in the tuple")
    Copy after login

  • Iteration

    : Use a for loop or iterator to traverse the elements in the tuple, such as:

    for element in my_tuple:
    print(element)
    Copy after login
Tuple as function parameter

Tuples are often used as function parameters because they are immutable and help prevent accidental modification. Functions can access elements in a tuple using subscripts or pieces.

Tuple vs. List

Tuples are immutable, while lists are mutable.
  • Tuples use parentheses
  • ()
  • , while lists use square brackets []. The elements of a tuple can only be accessed through subscripts or segments, while the elements of a list can be modified and added.
Best Practices

Prefer using tuples to store data that needs to be protected or immutable.
  • Consider using a list containing tuples as a
  • container
  • for large datasets. Use tuples as function parameters to ensure parameter integrity.
  • Avoid storing references to mutable objects in tuples, as this may lead to unexpected modifications.

The above is the detailed content of Tuples Revealed: The Ultimate Guide to Immutable Containers in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template