Tuple
A tuple stores several items in one variable.
It is one of the 4 built-in types Python offers for collections of data. The others are List, Set and Dictionary, and each has its own qualities and uses.
What sets a tuple apart is that it is ordered and unchangeable.
You write one using round brackets.
Example
Building a simple tuple:
Output: complete Python program
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Intro
mytuple = ("apple", "banana", "cherry")
Tuple Items
Three things define tuple items: they keep their order, they cannot be changed, and the same value may appear more than once.
Each item also has an index. Counting starts at zero, so index [0] is the first item, index [1] is the next one, and so on.
Ordered
Being ordered means a tuple keeps its items in a set sequence, and that sequence stays put.
Unchangeable
A tuple is fixed once it exists: you can't change its items, add new ones or remove any.
Allow Duplicates
Because every item has its own index, two items can share a value without any conflict:
Example
Here "apple" and "cherry" each appear twice:
Output: complete Python program
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
Tuple Length
The len() function tells you how many items a tuple holds:
Example
Count the items and print the result:
Output: complete Python program
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
Create Tuple With One Item
A tuple with a single item needs a comma after that item. Without the comma, Python won't treat it as a tuple.
Example
The comma makes all the difference:
Output: complete Python program
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Tuple Items - Data Types
A tuple can hold values of any data type:
Example
One tuple each of strings, integers and booleans:
Output: complete Python program
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
Example
A single tuple can also mix those types:
Output: complete Python program
tuple1 = ("abc", 34, True, 40, "male")
type()
To Python, a tuple is an object whose data type is 'tuple':
Example
Ask Python for the type of a tuple:
Output: complete Python program
mytuple = ("apple", "banana", "cherry")
print(type(mytuple))
The tuple() Constructor
You can also build a tuple by calling the tuple() constructor.
Example
Making a tuple with the tuple() constructor:
Output: complete Python program
thistuple = tuple(("apple", "banana", "cherry"))
print(thistuple)
Python Collections (Arrays)
Python gives you four collection data types, and each one behaves a little differently:
- List keeps its order and can be changed. Duplicates are fine.
- Tuple keeps its order but cannot be changed. Duplicates are fine.
- Set has no order and no indexes, and is unchangeable*. Duplicates are not allowed.
- Dictionary keeps its order** and can be changed. Duplicates are not allowed.
*You can't change the items of a set once they are in it, but you are free to add new ones or take existing ones out at any time.
**Dictionaries are ordered from Python 3.7 onward. In Python 3.6 and earlier they are unordered.
It pays to know these properties before you pick a type for your data. The right choice can keep the meaning of the data intact, and it can also make your program more efficient or more secure.