Tuples are used to store multiple items in a single variable
Tuples are one of 4 built-in data types in Python used to store collections of data
Tuples are written with round brackets ( )
A tuple is ordered, immutable & indexed, and allow duplicates and its is faster performance than a list/p>
mytuple = ("red", "green", "blue") print(mytuple) #Output: ('red', 'green', 'blue' |
mytuple = ("red", "green", "blue") print(mytuple[0]) #Output: red print(mytuple[1]) #Output: green print(mytuple[2]) #Output: blue |
We cannot change, add or remove items after the tuple has been created.
mytuple = ("red", "green", "blue") mytuple[0] = "yellow" TypeError: 'tuple' object does not support item assignment |
A tuple can contain different data types
tuple1 = ("apple", 1, "banana", 2, "cherry", 3.5, True, False) print(tuple1) #Output ('apple', 1, 'banana', 2, 'cherry', 3.5, True, False) |
tuple() Constructor
Using the tuple() constructor to make a tuple
thistuple = tuple(("apple", "banana", "cherry")) print(thistuple) #Output: ('apple', 'banana', 'cherry') thislist = ['apple', 'banana', 'cherry'] thislist_to_tuple = tuple(thislist) print(thislist_to_tuple) #Output: ('apple', 'banana', 'cherry') |
Tuple items are indexed and you can access them by referring to the index number
thistuple = ("Leo", "Matt", "Kane", "Scott", "Peter", "Will")
thistuple = ("Leo", "Matt", "Kane", "Scott", "Peter", "Will") # Indexing print(thistuple[0]) #Output: Leo print(thistuple[1]) #Output: Matt print(thistuple[2]) #Output: Kane print(thistuple[3]) #Output: Scott print(thistuple[4]) #Output: Peter print(thistuple[5]) #Output: Will # Negative Indexing print(thistuple[-6]) #Output: Leo print(thistuple[-5]) #Output: Matt print(thistuple[-4]) #Output: Kane print(thistuple[-3]) #Output: Scott print(thistuple[-2]) #Output: Peter print(thistuple[-1]) #Output: Will |
You 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 list with the specified items.The returned value will include the specified start index and it exclude the specified end index.
thistuple = ("Leo", "Matt", "Kane", "Scott", "Peter", "Will") print(thistuple[2:5]) #Output: ("Kane", "Scott", "Peter") |
Start index: 2
End index: 5
Index 2, Kane is included and index 5, will not included
Remember that the start\first item has index 0
By leaving out the start value, the range will start at the first item:
thistuple = ("Leo", "Matt", "Kane", "Scott", "Peter", "Will") print(thistuple[:4]) #Output: ('Leo', 'Matt', 'Kane', 'Scott') |
By leaving out the end value, the range will go on to the end of the list:
thistuple = ("Leo", "Matt", "Kane", "Scott", "Peter", "Will") print(thistuple[2:]) #Output: ('Kane', 'Scott', 'Peter', 'Will') |
Specify negative indexes if you want to start the search from the end of the list:
thistuple = ("Leo", "Matt", "Kane", "Scott", "Peter", "Will") print(thistuple[-4:-1]) #Output: ('Kane', 'Scott', 'Peter') |
Start index: -4
End index: -1
Index -4, Kane is included and index -1, will not included
Remember that the start\first item has negative index -1
Method | Description |
---|---|
count() | Returns the number of times a specified value occurs in a tuple |
index() | Searches the tuple for a specified value and returns the position of where it was found |
returns the number of occurrence of the specified value
number_tuple = (1,2,3,4,5) print(number_tuple.count(3)) #Output: 1 print(number_tuple.count(1)) #Output: 2 print(len(number_tuple)) #Output: 5 |
returns the index of the first element with the specified value
mytuple.index(value,start_index(optional),end_index(optional)) |
number_tuple = (1,2,3,4,5) return_index1 = number_tuple.index(1) print(return_index1) #Output: 0 return_index2 = number_tuple.index(1,3,6) print(return_index2) #Output: 5 return_index3 = number_tuple.index(1,3,) print(return_index3) #Output: 5 |
Unpacking in python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement by using the iterable unpacking operator, *
#Example 1 a, b, c = (1, 2, 3) print(a) print(b) print(c) #Example 2 a, b, c, *others = (1, 2, 3, 4, 5, 6, 7, 8, 9) print(a) print(b) print(c) print(others) #Example 2 a, b, c, *others, d= (1, 2, 3, 4, 5, 6, 7, 8, 9) print(a) print(b) print(c) print(others) print(d) |
If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!