

{"id":5542,"date":"2017-12-29T16:11:34","date_gmt":"2017-12-29T10:41:34","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5542"},"modified":"2026-04-20T16:56:00","modified_gmt":"2026-04-20T11:26:00","slug":"python-tuple","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-tuple\/","title":{"rendered":"What is Python Tuple &#8211; Creating, Functions, Methods, Operations"},"content":{"rendered":"<p>In this Python Tuple tutorial, we will take a deeper look at Python tuples. First, let\u2019s look at what a Python tuple is, and then we will discuss how to create, access, slice, and delete tuples in Python.<\/p>\n<p>Moreover, we will learn the functions, methods, and operations of Python tuples.<\/p>\n<p>Python provides a range of constructs to deal with items. These include Python lists, dictionaries, sets, tuples, and many more. It also supports built-in functions and methods that we can apply to these constructs.<\/p>\n<p>So, let&#8217;s start the Tuple in Python Tutorial.<\/p>\n<h3>What is a Python Tuple?<\/h3>\n<p><em>Python Tuples are like a list<\/em>. It can hold a sequence of items. The difference is that it is immutable.<\/p>\n<p><strong>Characteristics of a Tuple in Python:<\/strong><\/p>\n<ul>\n<li>We cannot update items in a tuple once it is created.<\/li>\n<li>A tuple cannot be extended.<\/li>\n<li>Once a tuple is created, items cannot be removed from it.<\/li>\n<\/ul>\n<p>Let\u2019s learn the syntax to create a tuple in Python.<\/p>\n<h3>How to Create a Python Tuple?<\/h3>\n<p>To declare a tuple in Python, you must type a list of items separated by commas, inside parentheses. Then assign it to a variable.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages=(90,95,89)<\/pre>\n<p>You should use a tuple when you don\u2019t want to change just an item in future.<\/p>\n<h4>1. Tuples Packing in Python<\/h4>\n<p>You can also create a Python tuple without parentheses. This is called tuple packing.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; b= 1, 2.0, 'three'<\/pre>\n<h4>2. Tuples Unpacking in Python<\/h4>\n<p>Python tuple unpacking is when you assign values from a tuple to a sequence of variables in python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages=(99,95,90,89,93,96)\r\n&gt;&gt;&gt; a,b,c,d,e,f=percentages\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">90<\/div>\n<p>You can do the same to a list.<\/p>\n<h4>3. Creating a tuple with a single item<\/h4>\n<p>Until now, we have seen how easy it is to declare a Python tuple. But when you do so with just one element, it may create some problems. Let\u2019s take a look at it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=(1)\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;int&#8217;&gt;<\/div>\n<p>Wasn\u2019t the type() method supposed to return class \u2018tuple\u2019?<\/p>\n<p>To get around this, we add a comma after the item.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=(1,)\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;tuple&#8217;&gt;<\/div>\n<p>Problem solved. And as we saw in tuple packing, we can skip the parentheses here.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=1,\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;tuple&#8217;&gt;<\/div>\n<p>Also, like a list, a Python tuple may contain items of different types.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=(1,2.0,'three')<\/pre>\n<h3>How to Access a Python Tuple?<\/h3>\n<h4>1. Accessing the entire tuple in Python<\/h4>\n<p>To access a tuple in Python, just type its name.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(90, 95, 89)<\/div>\n<p>Or, pass it to the print statement.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(percentages)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(90, 95, 89)<\/div>\n<h4>2. Accessing a single item in Tuplr<\/h4>\n<p>To get a single item from a Python tuple, use its index in square brackets. Indexing begins at 0.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[1]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">95<\/div>\n<h3>Slicing a Tuple in Python<\/h3>\n<p>If you want a part(slice) of a tuple in Python, use the slicing operator [].<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages=(99,95,90,89,93,96)<\/pre>\n<h4>1. Positive Indices<\/h4>\n<p>When using positive indices, we traverse the list from the left.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[2:4]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(90, 89)<\/div>\n<p>This prints out items from index 2 to index 3 (4-1) (items third to fourth).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[:4]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(99, 95, 90, 89)<\/div>\n<p>This prints out items from the beginning to the item at index 3.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[4:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(93, 96)<\/div>\n<p>This prints out items from index 4 to the end of the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[2:2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">()<\/div>\n<p>However, this returns an empty Python tuple.<\/p>\n<h4>2. Negative indexing<\/h4>\n<p>Now, let\u2019s look at negative indexing. Unlike positive indexing, it begins traversing from the right.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[:-2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(99, 95, 90, 89)<\/div>\n<p>This prints out the items from the tuple\u2019s beginning to two items from the end.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[-2:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(93, 96)<\/div>\n<p>This prints out items from two items from the end to the end.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[2:-2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(90, 89)<\/div>\n<p>This prints out items from index 2 to two items from the end.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[-2:2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">()<\/div>\n<p>This last piece of code, however, returns an empty tuple. This is because the<\/p>\n<p>start(-2) is behind the end(2) in this case.<\/p>\n<p>Lastly, when you provide no indices, it prints the whole Python tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; percentages[:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(99, 95, 90, 89, 93, 96)<\/div>\n<h3>Deleting a Python Tuple<\/h3>\n<p>As we discussed above, a Python tuple is immutable. This also means that you can\u2019t delete just a part of it. You must delete an entire tuple, if you may.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del percentages[4]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#19&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>del percentages[4]<\/p>\n<p>TypeError: &#8216;tuple&#8217; object doesn&#8217;t support item deletion<\/p>\n<\/div>\n<p>So, deleting a single element didn\u2019t work. Let\u2019s try deleting a slice.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del percentages[2:4]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#20&gt;&#8221;, line 1, in &lt;module&gt;del percentages[2:4]TypeError: &#8216;tuple&#8217; object does not support item deletion<\/p>\n<\/div>\n<p>As you can see, that didn\u2019t work either. Now, let\u2019s try deleting the entire tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del percentages\r\n&gt;&gt;&gt; percentages<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#40&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>percentages<\/p>\n<p>NameError: name &#8216;percentages&#8217; is not defined<\/p>\n<\/div>\n<p>We see that the Python tuple has been successfully deleted.<\/p>\n<h3>Reassigning Tuples in Python<\/h3>\n<p>As we discussed, a Python tuple is immutable. So let\u2019s try changing a value. But before that, let\u2019s take a new tuple with a list as an item in it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; my_tuple=(1,2,3,[4,5])<\/pre>\n<p>Now, let\u2019s try changing the list [4,5]. Its index is 3.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; my_tuple[3]=6<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#43&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>my_tuple[3]=6<\/p>\n<p>TypeError: &#8216;tuple&#8217; object does not support item assignment<\/p>\n<\/div>\n<p>See, that failed. Now how about changing an element from the same list]?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; my_tuple[3][0]=6\r\n&gt;&gt;&gt; my_tuple<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 2, 3, [6, 5])<\/div>\n<p>This worked without a flaw. So we can see that while tuples are immutable, a mutable item that it holds may be reassigned.<\/p>\n<h3>Tuple Functions in Python<\/h3>\n<p>A lot of functions that work on lists work on tuples too. A function applies on a construct and returns a result. It does not modify the construct. Let\u2019s see what we can do.<\/p>\n<div id=\"attachment_35764\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-35764\" class=\"size-full wp-image-35764\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01.jpg\" alt=\" Python Tuple Functions\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuple-Functions-01-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-35764\" class=\"wp-caption-text\">Python Tuple Tutorial &#8211; Python Tuple Functions<\/p><\/div>\n<h4>1. len() function in Python<\/h4>\n<p>Like a list, a\u00a0Python tuple is of a certain length. The len() function returns its length.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; my_tuple<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 2, 3, [6, 5])<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(my_tuple)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<p>It returned 4, not 5, because the list counts as 1.<\/p>\n<h4>2. max() function in Python<\/h4>\n<p>It returns the item from the tuple with the highest value.<\/p>\n<p>We can\u2019t apply this function on the tuple my_tuple, because ints cannot be compared to a list. So let\u2019s take yet another tuple in Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=(3,1,2,5,4,6)\r\n&gt;&gt;&gt; max(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<p>Let\u2019s try that on strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; max(('Hi','hi','Hello'))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;hi&#8217;<\/div>\n<p>\u2018hi\u2019 is the greatest out of these, because h has the highest ASCII value among h and H.<\/p>\n<p>But you can\u2019t compare an int and a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; max(('Hi',9))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#59&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>max((&#8216;Hi&#8217;,9))<\/p>\n<p>TypeError: &#8216;&gt;&#8217; not supported between instances of &#8216;int&#8217; and &#8216;str&#8217;<\/p>\n<\/div>\n<h4>3. min() function in Python<\/h4>\n<p>Like the max() function, the min() returns the item with the lowest values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; min(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>As you can see, 1 is the smallest item in this\u00a0Python tuple.<\/p>\n<h4>4. sum() function in Python<\/h4>\n<p>This function returns the arithmetic sum of all the items in the tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">21<\/div>\n<p>However, you can\u2019t apply this function on a tuple with strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum(('1','2','3'))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#57&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>sum((&#8216;1&#8242;,&#8217;2&#8242;,&#8217;3&#8217;))<\/p>\n<p>TypeError: unsupported operand type(s) for +: &#8216;int&#8217; and &#8216;str&#8217;<\/p>\n<\/div>\n<h4>5. any() function in Python<\/h4>\n<p>If even one item in the tuple has a Boolean value of True, then this function returns True. Otherwise, it returns False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; any(('','0',''))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>The string \u20180\u2019 does have a Boolean value of True. If it was rather the integer 0, it would\u2019ve returned False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; any(('',0,''))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>6. all() function in Python<\/h4>\n<p>Unlike any(), all() returns True only if all items have a Boolean value of True. Otherwise, it returns False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all(('1',1,True,''))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>7. sorted() function in Python<\/h4>\n<p>This function returns a sorted version of the tuple. The sorting is in ascending order, and it doesn\u2019t modify the original tuple in Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sorted(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<h4>8. tuple() function in Python<\/h4>\n<hr \/>\n<p>This function converts another construct into a Python tuple. Let\u2019s look at some of those.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list1=[1,2,3]\r\n&gt;&gt;&gt; tuple(list1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 2, 3)<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; string1=\"string\"\r\n&gt;&gt;&gt; tuple(string1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;s&#8217;, &#8216;t&#8217;, &#8216;r&#8217;, &#8216;i&#8217;, &#8216;n&#8217;, &#8216;g&#8217;)<\/div>\n<p>How well would it work with sets?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1={2,1,3}\r\n&gt;&gt;&gt; tuple(set1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 2, 3)<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3}<\/div>\n<p>As we can see, when we declared a set as 2,1,3, it automatically reordered itself to 1,2,3. Furthermore, creating a\u00a0Python tuple from it returned the new tuple in the new order, that is, ascending order.<\/p>\n<h3>Python Tuple Methods<\/h3>\n<p>A method is a sequence of instructions to perform on something. Unlike a function, it modifies the construct on which it is called. You call a method using the dot<strong> operator in Python. <\/strong>Let\u2019s learn about the two built-in methods of Python.<\/p>\n<h4>1. index() method in Python<\/h4>\n<p>This method takes one argument and returns the index of the first appearance of an item in a tuple. Let\u2019s take a new tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=(1,2,3,2,4,5,2)\r\n&gt;&gt;&gt; a.index(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>As you can see, we have 2s at indices 1, 3, and 6. But it returns only the first index.<\/p>\n<h4>2. count() method in Python<\/h4>\n<p>This method takes one argument and returns the number of times an item appears in the tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a.count(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<h3>Python Tuple Operations<\/h3>\n<p>Now, we will look at the operations that we can perform on tuples in Python.<\/p>\n<h4>1. Membership operation in Tuple<\/h4>\n<p>We can apply the \u2018in\u2019 and \u2018not in\u2019 operators on items. This tells us whether they belong to the tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'a' in tuple(\"string\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'x' not in tuple(\"string\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>2. Concatenation operation in Tuple<\/h4>\n<p>Like we\u2019ve previously discussed on several occasions, concatenation is the act of joining. We can join two tuples using the concatenation operator \u2018+\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (1,2,3)+(4,5,6)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 2, 3, 4, 5, 6)<\/div>\n<p>Other arithmetic operations do not apply on a tuple.<\/p>\n<h4>3. Logical operation in Tuple<\/h4>\n<p>All the logical operators (like &gt;,&gt;=,..) can be applied on a tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (1,2,3)&gt;(4,5,6)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (1,2)==('1','2')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>As is obvious, the ints 1 and aren\u2019t equal to the strings \u20181\u2019 and \u20182\u2019. Likewise, it returns False.<\/p>\n<h4>4. Identity operation in Tuple<\/h4>\n<p>Remember the \u2018is\u2019 and \u2018is not\u2019 operators we discussed in our tutorial on Python Operators? Let\u2019s try that on tuples.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=(1,2)\r\n&gt;&gt;&gt; (1,2) is a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>That did not make sense, did it? So what really happened? Well, in Python, two tuples or lists do not have the same identity. In other words, they are two different tuples or lists. As a result, it returns False.<\/p>\n<h3>Iterating on a Python Tuple<\/h3>\n<p>You can iterate on a Python tuple using a for loop as you would iterate on a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in (1,3,2):\r\n    print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n3<br \/>\n2<\/div>\n<h3>Nested Tuples in Python<\/h3>\n<p>Finally, we will learn about nesting tuples. You may remember how we can nest lists. Due to the similarities of a tuple to a list, we do the same with tuples.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=((1,2,3),(4,(5,6)))<\/pre>\n<p>Suppose we want to access item 6. For that, since we use indices, we write the following code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[1][1][1]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<p>Python tuples may also contain other constructs, especially lists. After all, it is a collection of items, and items can be anything.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (1,2,[3,4])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 2, [3, 4])<\/div>\n<p>This was all on the Python Tuple Tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Python Tuples<\/h3>\n<p>1. What are tuples in Python?<\/p>\n<p>2. How do tuples work in Python?<\/p>\n<p>3. How do you initialise a tuple in Python?<\/p>\n<p>4. What is the difference between a Python Tuple and a Python list?<\/p>\n<p>5. Why do we use tuples in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>Think of a Python tuple as a sealed envelope: once you slip data inside and close it, no one can swap, add, or delete its parts. This read-only nature guards settings, GPS points, dates, or any record that must stay fixed through the run.<\/p>\n<p>Tuples use round brackets and take less memory than lists because Python can store them in a tighter pack and skip safety checks for edits. They also work as keys in dictionaries or items in sets because the language trusts they will never change.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Tuple tutorial, we will take a deeper look at Python tuples. First, let\u2019s look at what a Python tuple is, and then we will discuss how to create, access, slice, and&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":42160,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[16524,16523,16520,16521,10901,16522,14984],"class_list":["post-5542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-create-python-tuples","tag-functions-on-tuples","tag-nested-tuples","tag-operations-on-python-tuples","tag-python-tuples","tag-python-tuples-methods","tag-tuples-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is Python Tuple - Creating, Functions, Methods, Operations - DataFlair<\/title>\n<meta name=\"description\" content=\"What is Python tuple-how to create a tuple in Python, Python tuples packing &amp; unpacking, access,Python tuple functions, Python tuple methods,tuple Operation\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-tuple\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Python Tuple - Creating, Functions, Methods, Operations - DataFlair\" \/>\n<meta property=\"og:description\" content=\"What is Python tuple-how to create a tuple in Python, Python tuples packing &amp; unpacking, access,Python tuple functions, Python tuple methods,tuple Operation\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-tuple\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-29T10:41:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T11:26:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuples-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Python Tuple - Creating, Functions, Methods, Operations - DataFlair","description":"What is Python tuple-how to create a tuple in Python, Python tuples packing & unpacking, access,Python tuple functions, Python tuple methods,tuple Operation","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-tuple\/","og_locale":"en_US","og_type":"article","og_title":"What is Python Tuple - Creating, Functions, Methods, Operations - DataFlair","og_description":"What is Python tuple-how to create a tuple in Python, Python tuples packing & unpacking, access,Python tuple functions, Python tuple methods,tuple Operation","og_url":"https:\/\/data-flair.training\/blogs\/python-tuple\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-12-29T10:41:34+00:00","article_modified_time":"2026-04-20T11:26:00+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuples-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"What is Python Tuple &#8211; Creating, Functions, Methods, Operations","datePublished":"2017-12-29T10:41:34+00:00","dateModified":"2026-04-20T11:26:00+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/"},"wordCount":1867,"commentCount":12,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuples-01.jpg","keywords":["Create Python Tuples","Functions on Tuples","Nested Tuples","Operations on Python Tuples","python tuples","Python Tuples Methods","Tuples in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-tuple\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/","url":"https:\/\/data-flair.training\/blogs\/python-tuple\/","name":"What is Python Tuple - Creating, Functions, Methods, Operations - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuples-01.jpg","datePublished":"2017-12-29T10:41:34+00:00","dateModified":"2026-04-20T11:26:00+00:00","description":"What is Python tuple-how to create a tuple in Python, Python tuples packing & unpacking, access,Python tuple functions, Python tuple methods,tuple Operation","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-tuple\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuples-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Tuples-01.jpg","width":802,"height":420,"caption":"What is Python Tuple - Creating, Functions, Methods, Operations"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-tuple\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"What is Python Tuple &#8211; Creating, Functions, Methods, Operations"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5542"}],"version-history":[{"count":19,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5542\/revisions"}],"predecessor-version":[{"id":147736,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5542\/revisions\/147736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/42160"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}