Home Browser Contributors

Tuple (tuple)

A Tuple is a type of value used in Python. A Tuple is used to represent many items of the same type of value together, but unlike a list a Tuple is immutable, which means values in the Tuple cannot be changed once the variable is defined The Tuple type is referred to as tuple internally in Python.

Tip: Python uses 0 as the first index in a list or Tuple.

Example

Here is an example of a Tuple:

``` py ("Hello!", "Hola!", "Bonjour", "こんにちは!") ```

You can read from an index like this:

``` py hello = ("Hello!", "Hola!", "Bonjour", "こんにちは!") print(f"{hello[3]}") # Gets value from 4th index in Tuple ```

You can also iterate through a Tuple, here's an example of iterating through a Tuple and then using f-strings to print the value and the index:

``` py hello = ("Hello!", "Hola!", "Bonjour", "こんにちは!") index = 0 for text in hello: index += 1 print(f"Text #{index}: {text}") ```