[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Tuple in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1571088945448{background-color: #f6f6f6 !important;}”]

Tuple in Python is a collection of objects in a specific order that cannot be changed after the tuple is declared. Therefore, a tuple is immutable.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

WHAT IS A TUPLE IN PYTHON?

A tuple is very similar to an Array. However, unlike the array, it is not possible to change values in a created tuple. This means that once you have created a tuple, the values in the tuple will remain the same until the tuple is removed.

 

Unlike the array, it is not possible to change values in a Tuple

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_column_text]

How to create a Tuple in Python?

Similar to the Array, you create a Tuple by the following syntax

 

tupleName = (value1, value2, value3, and so on.. )
  • First you give the tuple a name
  • Then assigns all values within a parenthesis (), where each value is separated by a comma “,”.
  • Note that a tuple is created with parentheses, unlike the array that is created with brackets [].

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_column_text]

Example – How to create a Tuple in Python

Let’s take a quite simple example on how to create a Tuple

 

animal_tuple = ("Cat", "Dog", "Fish", "Horse")

print(animal_tuple)
print(type(animal_tuple))
print(animal_tuple[0])
print(type(animal_tuple[1]))

The result then becomes

 

('Cat', 'Dog', 'Fish', 'Horse')
<class 'tuple'>
Cat
<class 'str'>

 

Note, similar to the Array, the index starts at zero. So if we for example write

 

print(animal_tuple[1])

We will get

 

Dog

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_column_text]

TUPLES ARE IMMUTABLE

 

You cannot change the values in a tuple.

 

Let’s take a short example, say for instance that we want to change our element “Dog” to “Snake”. By using the same method that we used for Array will cause a compilation error. Let’s have a look at the code,

 

animal_tuple = ("Cat", "Dog", "Fish", "Horse") 

animal_tuple[1] = "Snake"

Will result in

 

TypeError: 'tuple' object does not support item assignment

 

Instead, you must remove the Tuple completely. It can be done through the reserved word del () (delete). You can then re-create the tuple:

 

So in our case, if we want to change Dog to Snake (element 1 in the animal_tuple), we have to write:

del(animal_tuple) 

animal_tuple = ("Cat", "Snake", "Fish", "Horse")

 

Therefore, be careful which data type you choose to store values in. If you know that there is a probability that the values will change, Arrays are definitely preferred.[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]For more information about Tuple in Python we recommend the Python Docs website[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]