Python Tuples: Immutable, Versatile, and Efficient - w9school
In Python, a tuple is an ordered, immutable sequence of elements enclosed in parentheses (). Tuples are similar to lists, but they cannot be modified once created.
Python Tuples |
TupleTuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets. ExampleCreate a Tuple:
|
Tuple ItemsTuple items are ordered, unchangeable, and allow duplicate values. Tuple items are indexed, the first item has index OrderedWhen we say that tuples are ordered, it means that the items have a defined order, and that order will not change. UnchangeableTuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. Allow DuplicatesSince tuples are indexed, they can have items with the same value: ExampleTuples allow duplicate values:
|
Create Tuple With One ItemTo create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple. ExampleOne item tuple, remember the comma:
|
type()From Python's perspective, tuples are defined as objects with the data type 'tuple': <class 'tuple'>
ExampleWhat is the data type of a tuple?
|
Python Collections (Arrays)There are four collection data types in the Python programming language:
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security. |
Python - Access Tuple Items |
Access Tuple ItemsYou can access tuple items by referring to the index number, inside square brackets: ExamplePrint the second item in the tuple:
|
Negative IndexingNegative indexing means start from the end.
ExamplePrint the last item of the tuple:
Range of IndexesYou can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new tuple with the specified items. ExampleReturn the third, fourth, and fifth item:
ExampleThis example returns the items from the beginning to, but NOT included, "kiwi":
By leaving out the end value, the range will go on to the end of the list: ExampleThis example returns the items from "cherry" and to the end:
|
Check if Item ExistsTo determine if a specified item is present in a tuple use the ExampleCheck if "apple" is present in the tuple:
|
Python - Update Tuples |
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created. But there are some workarounds. Change Tuple ValuesOnce a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple. ExampleConvert the tuple into a list to be able to change it:
|
Python - Unpack Tuples |
Unpacking a TupleWhen we create a tuple, we normally assign values to it. This is called "packing" a tuple: ExamplePacking a tuple:
But, in Python, we are also allowed to extract the values back into variables. This is called "unpacking":
ExampleUnpacking a tuple:
|
Python - Loop Tuples |
Loop Through a TupleYou can loop through the tuple items by using a ExampleIterate through the items and print the values:
|
Python - Join Tuples |
Join Two TuplesTo join two or more tuples you can use the ExampleJoin two tuples:
Multiply TuplesIf you want to multiply the content of a tuple a given number of times, you can use the ExampleMultiply the fruits tuple by 2:
|
Python - Tuple Methods |
Tuple MethodsPython has two built-in methods that you can use on tuples.
|
Python - Tuple Exercises |
Test Yourself With Exercises
Now you have learned a lot about tuples, and how to use them in Python.
Are you ready for a test?
If Yes, then Click Here
What's Your Reaction?