Python Language Tuple: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 2: Line 2:
* [[Python_Language#Tuple|Python Language]]
* [[Python_Language#Tuple|Python Language]]
=Overview=
=Overview=
A tuple is a immutable [[Python_Language#Sequence_Types|sequence type]] that contains zero or more elements and whose elements can be of different types. Once a tuple is defined, you can't add, delete or change items. A tuple is similar to a constant [[Python Language List#Overview|list]], and could be used instead of a list, if we can afford the "list" to be immutable. Naturally, the list's mutating functions  <code>append()</code>, <code>insert()</code> do not exist on tuples. There are several advantages of using a tuple instead of a list: a tuple uses less space than a list and they cannot be mutated by mistake.
A tuple is a immutable [[Python_Language#Sequence_Types|sequence type]] that contains zero or more elements and whose elements can be of different types. Once a tuple is defined, you can't add, delete or change items. A tuple is similar to a constant [[Python Language List#Overview|list]], and could be used instead of a list, if we can afford the "list" to be immutable. Naturally, the list's mutating functions  <code>append()</code>, <code>insert()</code> do not exist on tuples. There are several advantages of using a tuple instead of a list: a tuple uses less space than a list and they cannot be mutated by mistake. Positional function arguments can be [[Python_Language_Functions#Gather_Positional_Arguments_with_.2A|grouped together and provided as a tuple]] in the function body (<code>*args</code>).


=Declaration=
=Declaration=

Revision as of 05:02, 20 January 2022

Internal

Overview

A tuple is a immutable sequence type that contains zero or more elements and whose elements can be of different types. Once a tuple is defined, you can't add, delete or change items. A tuple is similar to a constant list, and could be used instead of a list, if we can afford the "list" to be immutable. Naturally, the list's mutating functions append(), insert() do not exist on tuples. There are several advantages of using a tuple instead of a list: a tuple uses less space than a list and they cannot be mutated by mistake. Positional function arguments can be grouped together and provided as a tuple in the function body (*args).

Declaration

Conversion from other Data Structures

Tuple Unpacking

Exchanging Variable Values

Tuples can be used to exchange to variable values without using a third temporary variable.

Named Tuples

Named tuples can be a simple alternative to objects.