- A Tuple consists of a number of values separated by commas enclosed with or without parentheses
- Tuples are immutable
- Tuples can contain mutable objects such as lists
- A Tuple with one item is constructed by following a value with a comma
- Empty Tuples are constructed by an empty pair of parentheses
>>> emptyTuple = ()
>>> emptyTuple
()
>>> type(emptyTuple)
<class 'tuple'>
>>> len(emptyTuple)
0
--------------------------------------------------------
>>> myTuple = tuple(("Girish","Sanjay"))
>>> myTuple
('Girish', 'Sanjay')
>>> myTuple = tuple(["Girish","Python"])
>>> myTuple
('Girish', 'Python')
--------------------------------------------------------
>>> myTuple1 = "Girish","Python",1,2,5.5,3.2,True
>>> myTuple1
('Girish', 'Python', 1, 2, 5.5, 3.2, True)
>>>
--------------------------------------------------------
>>> singleItemTuple = "Girish",
>>> singleItemTuple
('Girish',)
>>> singleItemTuple1 = ("Girish",)
>>> singleItemTuple1
('Girish',)
>>> len(singleItemTuple)
1
>>> len(singleItemTuple1)
1
--------------------------------------------------------
>>> notaTuple = ("Girish")
>>> notaTuple
'Girish'
>>> type(notaTuple)
<class 'str'>
>>> len(notaTuple)
6
>>>
-------------------------------------------------------
>>> myTuple = ("One","Two",3,4)
>>> type(myTuple)
<class 'tuple'>
>>> myTuple
('One', 'Two', 3, 4)
>>> for x in myTuple:
... print(x)
...
One
Two
3
4
-------------------------------------------------------
>>> myNumTuple = (1,2,3,4,5,6)
>>> myNumTuple
(1, 2, 3, 4, 5, 6)
>>> for x in myNumTuple:
... print(x)
...
1
2
3
4
5
6
-------------------------------------------------------
>>> myStrTuple = ("One","Two","Three")
>>> myStrTuple
('One', 'Two', 'Three')
>>> for x in myStrTuple:
... print(x)
...
One
Two
Three
-------------------------------------------------------
>>> myBoolTuple = (True,False)
>>> myBoolTuple
(True, False)
>>> for x in myBoolTuple:
... print(x)
...
True
False
-------------------------------------------------------
>>> mytestTuple =(TRUE,One,"Test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'TRUE' is not defined
>>> myFltTuple = (1.1,2.1,3.4,5.7)
>>> myFltTuple
(1.1, 2.1, 3.4, 5.7)
>>> for x in myFltTuple:
... print(x)
...
1.1
2.1
3.4
5.7
-------------------------------------------------------
>>> myListTuple = (["one","two","three"],[1,2,3], [1.1,2.2,3.4],[True,False],["Four","Five","Six",7,8,9,True,12.4])
>>> myListTuple
(['one', 'two', 'three'], [1, 2, 3], [1.1, 2.2, 3.4], [True, False], ['Four', 'Five', 'Six', 7, 8, 9, True, 12.4])
>>> for x in myListTuple:
... print(x)
...
['one', 'two', 'three']
[1, 2, 3]
[1.1, 2.2, 3.4]
[True, False]
['Four', 'Five', 'Six', 7, 8, 9, True, 12.4]
Tuple Indexing
>>> myTuple = ("one","two","three","four","five","six")
>>> myTuple[0]
'one'
>>> myTuple[5]
'six'
>>> myTuple[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> myTuple[-1]
'six'
>>> myTuple[-6]
'one'
>>> myTuple[-7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
Tuple Slicing
>>> myTuple[2:5]
('three', 'four', 'five')
>>> myTuple[2:]
('three', 'four', 'five', 'six')
>>> myTuple[:5]
('one', 'two', 'three', 'four', 'five')
>>> myTuple[:3]
('one', 'two', 'three')
>>> myTuple[-4:-1]
('three', 'four', 'five')
>>> myTuple[:-1]
('one', 'two', 'three', 'four', 'five')
>>> myTuple[2:-1]
('three', 'four', 'five')
>>> myTuple[:]
('one', 'two', 'three', 'four', 'five', 'six')
>>> myTuple[::]
('one', 'two', 'three', 'four', 'five', 'six')
>>> myTuple[::2]
('one', 'three', 'five')
>>>
Tuple Operations and other examples
>>> myTuple = (1,2,3)
>>> myTuple1 = ("one","two","three")
>>> myTuple + myTuple1
(1, 2, 3, 'one', 'two', 'three')
>>> 2 in myTuple
True
>>> ("Girish",) * 3
('Girish', 'Girish', 'Girish')
>>> myTuple1[2] = "THREE"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> myTuple1[3] ="four"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>
>>> myTuple
(1, 2, 3)
>>> del myTuple
>>> myTuple
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'myTuple' is not defined
References
- https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
Learn more about Python features in our upcoming blog articles
Happy Learning!