{"id":8183,"date":"2021-02-16T01:28:12","date_gmt":"2021-02-15T19:58:12","guid":{"rendered":"https:\/\/pynative.com\/?p=8183"},"modified":"2021-04-09T10:55:24","modified_gmt":"2021-04-09T05:25:24","slug":"python-tuples","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-tuples\/","title":{"rendered":"Tuples in Python"},"content":{"rendered":"\n<p>In this article, you will learn how to use a tuple data structure in Python. Also, learn how to create, access, and modify a tuple in Python and all other operations we can perform on a tuple.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-a-tuple\">What is a Tuple<\/h2>\n\n\n\n<p><strong>Tuples are ordered collections of heterogeneous data that are unchangeable<\/strong>. Heterogeneous means tuple can store variables of all types.<\/p>\n\n\n\n<p>Tuple has the following characteristics<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Ordered<\/strong>: Tuples are part of sequence data types, which means they hold the order of the data insertion. It maintains the index value for each item.<\/li><li><strong>Unchangeable<\/strong>: Tuples are unchangeable, which means that we cannot add or delete items to the tuple after creation.<\/li><li><strong>Heterogeneous<\/strong>: Tuples are a sequence of data of different data types (like&nbsp;integer, float, list, string, etc;) and can be accessed through indexing and slicing.<\/li><li><strong>Contains Duplicates<\/strong>: Tuples can contain duplicates, which means they can have items with the same value.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-columns sectionpy has-black-color has-text-color has-background is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\" style=\"background-color:#edf9f8\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Also See<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-tuple-exercise-with-solutions\/\">Python Tuple Exercise<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-tuple-quiz\/\">Python Tuple Quiz<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-tuples\/#h-summary-of-tuples-operations\">Summary of Tuple Operations<\/a><\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"401\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg\" alt=\"Python tuples\" class=\"wp-image-8186\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg 682w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple-300x176.jpg 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><figcaption>Python tuples<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-what-is-a-tuple\" data-level=\"2\">What is a Tuple<\/a><\/li><li><a href=\"#h-creating-a-tuple\" data-level=\"2\">Creating a Tuple<\/a><ul><li><a href=\"#h-create-a-tuple-with-a-single-item\" data-level=\"3\">Create a tuple with a single item<\/a><\/li><li><a href=\"#h-packing-and-unpacking\" data-level=\"3\">Packing and Unpacking<\/a><\/li><\/ul><\/li><li><a href=\"#h-length-of-a-tuple\" data-level=\"2\">Length of a Tuple<\/a><\/li><li><a href=\"#h-iterating-a-tuple\" data-level=\"2\">Iterating a Tuple<\/a><\/li><li><a href=\"#h-accessing-items-of-a-tuple\" data-level=\"2\">Accessing items of a Tuple<\/a><ul><li><a href=\"#h-indexing\" data-level=\"3\">Indexing<\/a><\/li><li><a href=\"#h-negative-indexing\" data-level=\"3\">Negative Indexing<\/a><\/li><li><a href=\"#h-slicing-a-tuple\" data-level=\"3\">Slicing a tuple<\/a><\/li><\/ul><\/li><li><a href=\"#h-finding-an-item-in-a-tuple\" data-level=\"2\">Finding an item in a Tuple<\/a><ul><li><a href=\"#h-find-within-a-range\" data-level=\"3\">Find within a range<\/a><\/li><li><a href=\"#h-checking-if-an-item-exists\" data-level=\"3\">Checking if an item exists<\/a><\/li><\/ul><\/li><li><a href=\"#h-adding-and-changing-items-in-a-tuple\" data-level=\"2\">Adding and changing items in a Tuple<\/a><ul><li><a href=\"#h-modify-nested-items-of-a-tuple\" data-level=\"3\">Modify nested items of a tuple<\/a><\/li><\/ul><\/li><li><a href=\"#h-removing-items-from-a-tuple\" data-level=\"2\">Removing items from a tuple<\/a><ul><li><a href=\"#h-using-del-keyword\" data-level=\"3\">Using del keyword<\/a><\/li><li><a href=\"#h-by-converting-it-into-a-list\" data-level=\"3\">By converting it into a List<\/a><\/li><\/ul><\/li><li><a href=\"#h-count-the-occurrence-of-an-item-in-a-tuple\" data-level=\"2\">Count the occurrence of an item in a tuple<\/a><\/li><li><a href=\"#h-copying-a-tuple\" data-level=\"2\">Copying a tuple<\/a><\/li><li><a href=\"#h-concatenating-two-tuples\" data-level=\"2\">Concatenating two Tuples<\/a><ul><li><a href=\"#h-using-the-operator\" data-level=\"3\">Using the + operator<\/a><\/li><li><a href=\"#h-using-the-sum-function\" data-level=\"3\">Using the sum() function<\/a><\/li><li><a href=\"#h-using-the-chain-function\" data-level=\"3\">Using the chain() function<\/a><\/li><\/ul><\/li><li><a href=\"#h-nested-tuples\" data-level=\"2\">Nested tuples<\/a><\/li><li><a href=\"#h-use-built-in-functions-with-tuple\" data-level=\"2\">Use built-in functions with tuple<\/a><ul><li><a href=\"#h-min-and-max\" data-level=\"3\">min() and max()<\/a><\/li><li><a href=\"#h-all\" data-level=\"3\">all()<\/a><\/li><li><a href=\"#h-any\" data-level=\"3\">any()<\/a><\/li><\/ul><\/li><li><a href=\"#h-when-to-use-tuple\" data-level=\"2\">When to use Tuple?<\/a><\/li><li><a href=\"#h-summary-of-tuples-operations\" data-level=\"2\">Summary of tuples operations<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-tuple\">Creating a Tuple<\/h2>\n\n\n\n<p>We can create a tuple using the two ways<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Using parenthesis (): <\/strong>A tuple is created by enclosing comma-separated items inside rounded brackets.<\/li><li>Using a&nbsp;<code>tuple()<\/code> constructor: Create a tuple by passing the comma-separated items inside the <code>tuple()<\/code>.<\/li><\/ol>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>A tuple can have items of different data type integer, float, list, string, etc;<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create a tuple using ()<\/span>\n<span class=\"hljs-comment\"># number tuple<\/span>\nnumber_tuple = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">25.75<\/span>)\nprint(number_tuple)\n<span class=\"hljs-comment\"># Output (10, 20, 25.75)<\/span>\n\n<span class=\"hljs-comment\"># string tuple<\/span>\nstring_tuple = (<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'Emma'<\/span>, <span class=\"hljs-string\">'Kelly'<\/span>)\nprint(string_tuple)\n<span class=\"hljs-comment\"># Output ('Jessa', 'Emma', 'Kelly')<\/span>\n\n<span class=\"hljs-comment\"># mixed type tuple<\/span>\nsample_tuple = (<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">45.75<\/span>, &#91;<span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">78<\/span>])\nprint(sample_tuple)\n<span class=\"hljs-comment\"># Output ('Jessa', 30, 45.75, &#91;25, 78])<\/span>\n\n<span class=\"hljs-comment\"># create a tuple using tuple() constructor<\/span>\nsample_tuple2 = tuple((<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">45.75<\/span>, &#91;<span class=\"hljs-number\">23<\/span>, <span class=\"hljs-number\">78<\/span>]))\nprint(sample_tuple2)\n<span class=\"hljs-comment\"># Output ('Jessa', 30, 45.75, &#91;23, 78])<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output, the different items are added in the tuple like integer, string, and list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-a-tuple-with-a-single-item\">Create a tuple with a single item<\/h3>\n\n\n\n<p>A single item tuple is created by enclosing one item inside parentheses followed by a comma. If the tuple time is a string enclosed within parentheses and not followed by a comma, Python treats it as a <code>str<\/code> type. Let us see this with an example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># without comma<\/span>\nsingle_tuple = (<span class=\"hljs-string\">'Hello'<\/span>)\nprint(type(single_tuple))  \n<span class=\"hljs-comment\"># Output class 'str'<\/span>\nprint(single_tuple)  \n<span class=\"hljs-comment\"># Output Hello<\/span>\n\n<span class=\"hljs-comment\"># with comma<\/span>\nsingle_tuple1 = (<span class=\"hljs-string\">'Hello'<\/span>,)  \n<span class=\"hljs-comment\"># output class 'tuple'<\/span>\nprint(type(single_tuple1))  \n<span class=\"hljs-comment\"># Output ('Hello',)<\/span>\nprint(single_tuple1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output the first time, we did not add a comma after the &#8220;Hello&#8221;. So the variable type was class <code>str<\/code>, and the second time it was a class <code>tuple<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-packing-and-unpacking\">Packing and Unpacking<\/h3>\n\n\n\n<p>A tuple can also be created without using a <code>tuple()<\/code> constructor or enclosing the items inside the parentheses. It is called the variable &#8220;Packing.&#8221;<\/p>\n\n\n\n<p>In Python, we can create a tuple by packing a group of variables. Packing can be used when we want to collect multiple values in a single variable. Generally, this operation is referred to as tuple packing.<\/p>\n\n\n\n<p>Similarly, we can unpack the items by just assigning the tuple items to the same number of variables. This process is called &#8220;Unpacking.&#8221;<\/p>\n\n\n\n<p>Let us see this with an example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># packing variables into tuple<\/span>\ntuple1 = <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">\"Hello\"<\/span>\n<span class=\"hljs-comment\"># display tuple<\/span>\nprint(tuple1)  \n<span class=\"hljs-comment\"># Output (1, 2, 'Hello')<\/span>\n\nprint(type(tuple1))  \n<span class=\"hljs-comment\"># Output class 'tuple'<\/span>\n\n<span class=\"hljs-comment\"># unpacking tuple into variable<\/span>\ni, j, k = tuple1\n<span class=\"hljs-comment\"># printing the variables<\/span>\nprint(i, j, k) \n<span class=\"hljs-comment\"># Output 1 2 Hello<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output, three tuple items are assigned to individual variables i, j, k, respectively.<\/p>\n\n\n\n<p>In case we assign fewer variables than the number of items in the tuple, we will get the value error with the message too many values to unpack<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-length-of-a-tuple\">Length of a Tuple<\/h2>\n\n\n\n<p>We can find the length of the tuple using the <code>len()<\/code> function. This will return the number of items in the tuple.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-string\">'P'<\/span>, <span class=\"hljs-string\">'Y'<\/span>, <span class=\"hljs-string\">'T'<\/span>, <span class=\"hljs-string\">'H'<\/span>, <span class=\"hljs-string\">'O'<\/span>, <span class=\"hljs-string\">'N'<\/span>)\n<span class=\"hljs-comment\"># length of a tuple<\/span>\nprint(len(tuple1))  \n<span class=\"hljs-comment\"># Output 6<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-iterating-a-tuple\">Iterating a Tuple<\/h2>\n\n\n\n<p>We can iterate a tuple using a for loop Let us see this with an example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create a tuple<\/span>\nsample_tuple = tuple((<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">\"Hello\"<\/span>, &#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">16<\/span>]))\n<span class=\"hljs-comment\"># iterate a tuple<\/span>\n<span class=\"hljs-keyword\">for<\/span> item <span class=\"hljs-keyword\">in<\/span> sample_tuple:\n    print(item)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n2\n3\nHello\n[4, 8, 16]<\/pre>\n\n\n\n<p>As we can see in the above output we are printing each and every item in the tuple using a loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-accessing-items-of-a-tuple\">Accessing items of a Tuple<\/h2>\n\n\n\n<p>Tuple can be accessed through indexing and slicing. This section will guide you by accessing tuple using the following two ways<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using indexing<\/strong>, we can access any item from a tuple using its index number<\/li><li><strong>Using slicing<\/strong>, we can access a range of items from a tuple<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-indexing\">Indexing<\/h3>\n\n\n\n<p>A tuple is an ordered sequence of items, which means they hold the order of the data insertion. It maintains the index value for each item.<\/p>\n\n\n\n<p>We can access an item of a tuple by using its index number inside the index operator <code>[]<\/code> and this process is called <strong>&#8220;Indexing&#8221;<\/strong>.<\/p>\n\n\n\n<p><strong>Note<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>As tuples are ordered sequences of items, the index values start from 0 to the tuple&#8217;s length.<\/li><li>Whenever we try to access an item with an index more than the tuple&#8217;s length, it will throw the <code>'Index Error'<\/code>.<\/li><li>Similarly, the index values are always integer. If we give any other type, then it will throw <code>Type Error<\/code>.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"231\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/positive-and-negative-indexing.jpg\" alt=\"Python positive and negative indexing\" class=\"wp-image-8185\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/positive-and-negative-indexing.jpg 745w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/positive-and-negative-indexing-300x93.jpg 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/figure><\/div>\n\n\n\n<p>In the above image, we can see that the index values start from zero and it goes till the last item whose index value will be <code>len(tuple) - 1<\/code>&nbsp;.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-string\">'P'<\/span>, <span class=\"hljs-string\">'Y'<\/span>, <span class=\"hljs-string\">'T'<\/span>, <span class=\"hljs-string\">'H'<\/span>, <span class=\"hljs-string\">'O'<\/span>, <span class=\"hljs-string\">'N'<\/span>)\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">4<\/span>):\n    print(tuple1&#91;i])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">P\nY\nT\nH<\/pre>\n\n\n\n<p>As seen in the above example, we print the tuple&#8217;s first four items with the indexing.<\/p>\n\n\n\n<p><strong>Note<\/strong>: If we mention the index value greater than the length of a tuple then it will throw an index error.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-string\">'P'<\/span>, <span class=\"hljs-string\">'Y'<\/span>, <span class=\"hljs-string\">'T'<\/span>, <span class=\"hljs-string\">'H'<\/span>, <span class=\"hljs-string\">'O'<\/span>, <span class=\"hljs-string\">'N'<\/span>)\n\n<span class=\"hljs-comment\"># IndexError: tuple index out of range<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">7<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code7');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Also, if you mention any index value other than integer then it will throw Type Error.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-string\">'P'<\/span>, <span class=\"hljs-string\">'Y'<\/span>, <span class=\"hljs-string\">'T'<\/span>, <span class=\"hljs-string\">'H'<\/span>, <span class=\"hljs-string\">'O'<\/span>, <span class=\"hljs-string\">'N'<\/span>)\n\n<span class=\"hljs-comment\"># TypeError: tuple indices must be integers or slices, not float<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">2.0<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code8');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-negative-indexing\">Negative Indexing<\/h3>\n\n\n\n<p>The index values can also be negative, with the last but the first items having the index value as -1 and second last -2 and so on.<\/p>\n\n\n\n<p>For example, We can access the last item of a tuple using <code class=\"language-python\">tuple_name[-1]<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s do two things here<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Access tuple items using the negative index value<\/li><li>Iterate tuple using negative indexing<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-string\">'P'<\/span>, <span class=\"hljs-string\">'Y'<\/span>, <span class=\"hljs-string\">'T'<\/span>, <span class=\"hljs-string\">'H'<\/span>, <span class=\"hljs-string\">'O'<\/span>, <span class=\"hljs-string\">'N'<\/span>)\n<span class=\"hljs-comment\"># Negative indexing<\/span>\n<span class=\"hljs-comment\"># print last item of a tuple<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">-1<\/span>])  <span class=\"hljs-comment\"># N<\/span>\n<span class=\"hljs-comment\"># print second last<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">-2<\/span>])  <span class=\"hljs-comment\"># O<\/span>\n\n<span class=\"hljs-comment\"># iterate a tuple using negative indexing<\/span>\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">-6<\/span>, <span class=\"hljs-number\">0<\/span>):\n    print(tuple1&#91;i], end=<span class=\"hljs-string\">\", \"<\/span>)  \n<span class=\"hljs-comment\"># Output P, Y, T, H, O, N,<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-a-tuple\">Slicing a tuple<\/h3>\n\n\n\n<p>We can even specify a range of items to be accessed from a tuple using the technique called &#8216;Slicing.&#8217; The operator used is <code>':'<\/code>.<\/p>\n\n\n\n<p>We can specify the start and end values for the <strong>range of items to be accessed from the tuple<\/strong>. The output will be a tuple, and it includes the range of items with the index values from the start till the end of the range. The end value item will be excluded.<\/p>\n\n\n\n<p>We should keep in mind that the index value always starts with a 0.<\/p>\n\n\n\n<p>For easy understanding, we will be using an integer tuple with values from 0 to 9 similar to how an index value is assigned.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code10\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>)\n\n<span class=\"hljs-comment\"># slice a tuple with start and end index number<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">5<\/span>])\n<span class=\"hljs-comment\"># Output (1, 2, 3, 4)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code10');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output the values starting from 1 to 4 are printed. Here the last value in the range 5 is excluded.<\/p>\n\n\n\n<p><strong>Note<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the start value is not mentioned while slicing a tuple, then the values in the tuples start from the first item until the end item in the range. Again the end item in the range will be excluded.<\/li><li>Similarly, we can mention a slicing range without the end value. In that case, the item with the index mentioned in the start value of the range till the end of the tuple will be returned.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code11\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>)\n\n<span class=\"hljs-comment\"># slice a tuple without start index<\/span>\nprint(tuple1&#91;:<span class=\"hljs-number\">5<\/span>])\n<span class=\"hljs-comment\"># Output (0, 1, 2, 3, 4)<\/span>\n\n<span class=\"hljs-comment\"># slice a tuple without end index<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">6<\/span>:])\n<span class=\"hljs-comment\"># Output (6, 7, 8, 9, 10)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code11');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Similarly, we can slice tuple using negative indexing as well. The last but first item will have the index -1.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code12\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>)\n\n<span class=\"hljs-comment\"># slice a tuple using negative indexing<\/span>\nprint(tuple1&#91;<span class=\"hljs-number\">-5<\/span>:<span class=\"hljs-number\">-1<\/span>])\n<span class=\"hljs-comment\"># Output (6, 7, 8, 9)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code12');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Here we can see that the items with the negative indexes starting from -1 till -4 are printed excluding -5.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-finding-an-item-in-a-tuple\">Finding an item in a Tuple<\/h2>\n\n\n\n<p>We can search for a certain item in a tuple using the <code>index()<\/code> method and it will return the position of that particular item in the tuple.<\/p>\n\n\n\n<p>The <code>index()<\/code> method accepts the following three arguments<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>item<\/strong> &#8211; The item which needs to be searched<\/li><li><strong>start<\/strong> &#8211; (Optional) The starting value of the index from which the search will start<\/li><li><strong>end<\/strong> &#8211; (Optional) The end value of the index search<\/li><\/ol>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code13\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>)\n\n<span class=\"hljs-comment\"># get index of item 30<\/span>\nposition = tuple1.index(<span class=\"hljs-number\">30<\/span>)\nprint(position)\n<span class=\"hljs-comment\"># Output 2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code13', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code13');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output the index value of item 30 is printed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-find-within-a-range\"><strong>Find within a range<\/strong><\/h3>\n\n\n\n<p>We can mention the start and end values for the <code>index()<\/code> method so that our search will be limited to those values.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code14\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-number\">80<\/span>)\n<span class=\"hljs-comment\"># Limit the search locations using start and end<\/span>\n<span class=\"hljs-comment\"># search only from location 4 to 6<\/span>\n<span class=\"hljs-comment\"># start = 4 and end = 6<\/span>\n<span class=\"hljs-comment\"># get index of item 60<\/span>\nposition = tuple1.index(<span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>)\nprint(position)  \n<span class=\"hljs-comment\"># Output 5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code14', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code14');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output we have limited the search from the index position 4 to 6 as the number 60 is present in this range only. In case we mention any item that is not present then it will throw a value error.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code15\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-number\">80<\/span>)\n<span class=\"hljs-comment\">#index out of range<\/span>\nposition= tuple1 .index(<span class=\"hljs-number\">10<\/span>)\nprint(postion)\n<span class=\"hljs-comment\"># Output ValueError: tuple.index(x): x not in tuple<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code15', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code15');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-checking-if-an-item-exists\">Checking if an item exists<\/h3>\n\n\n\n<p>We can check whether an item exists in a tuple by using the <code>in<\/code> operator. This will return a boolean <code>True<\/code> if the item exists and <code>False<\/code> if it doesn&#8217;t.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code16\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-number\">80<\/span>)\n<span class=\"hljs-comment\"># checking whether item 50 exists in tuple<\/span>\nprint(<span class=\"hljs-number\">50<\/span> <span class=\"hljs-keyword\">in<\/span> tuple1)\n<span class=\"hljs-comment\"># Output True<\/span>\nprint(<span class=\"hljs-number\">500<\/span> <span class=\"hljs-keyword\">in<\/span> tuple1)\n<span class=\"hljs-comment\"># Output False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code16', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code16');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output we can see that the item &#8217;50&#8217; exists in the tuple so we got <code>True<\/code> and &#8216;500&#8217; doesn&#8217;t and so we got <code>False<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-and-changing-items-in-a-tuple\">Adding and changing items in a Tuple<\/h2>\n\n\n\n<p>A list is a mutable type, which means we can add or modify values in it, but tuples are immutable, so they cannot be changed.<\/p>\n\n\n\n<p>Also, because a tuple is immutable there are no built-in methods to add items to the tuple.<\/p>\n\n\n\n<p>If you try to modify the value you will get an error.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code17\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\ntuple1&#91;<span class=\"hljs-number\">1<\/span>] = <span class=\"hljs-number\">10<\/span>\n<span class=\"hljs-comment\"># Output TypeError: 'tuple' object does not support item assignment<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code17', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code17');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As a workaround solution, we can convert the tuple to a list, add items, and then convert it back to a tuple. As tuples are ordered collection like lists the items always get added in the end.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code18\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># converting tuple into a list<\/span>\nsample_list = list(tuple1)\n<span class=\"hljs-comment\"># add item to list<\/span>\nsample_list.append(<span class=\"hljs-number\">6<\/span>)\n\n<span class=\"hljs-comment\"># converting list back into a tuple<\/span>\ntuple1 = tuple(sample_list)\nprint(tuple1)  \n<span class=\"hljs-comment\"># Output (0, 1, 2, 3, 4, 5, 6)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code18', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code18');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output the item is added to the tuple in the end.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-modify-nested-items-of-a-tuple\">Modify nested items of a tuple<\/h3>\n\n\n\n<p>One thing to remember here, If one of the items is itself a mutable data type as a list, then we can change its values in the case of a nested tuple.<\/p>\n\n\n\n<p>For example, let&#8217;s assume you have the following tuple which has a list as its last item and you wanted to modify the list items.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tuple1 = (10, 20, <strong>[25, 75, 85]<\/strong>)<\/pre>\n\n\n\n<p>Let&#8217;s see how to modify the set item if it contains mutable types.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code19\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, &#91;<span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">75<\/span>, <span class=\"hljs-number\">85<\/span>])\n<span class=\"hljs-comment\"># before update<\/span>\nprint(tuple1)\n<span class=\"hljs-comment\"># Output (10, 20, &#91;25, 75, 85])<\/span>\n\n<span class=\"hljs-comment\"># modify last item's first value<\/span>\ntuple1&#91;<span class=\"hljs-number\">2<\/span>]&#91;<span class=\"hljs-number\">0<\/span>] = <span class=\"hljs-number\">250<\/span>\n<span class=\"hljs-comment\"># after update<\/span>\nprint(tuple1)\n<span class=\"hljs-comment\"># Output (10, 20, &#91;250, 75, 85])<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code19', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code19');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As tuples are immutable we cannot change the values of items in the tuple. Again with the same workaround we can convert it into a list, make changes and convert it back into a tuple.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code20\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># converting tuple into a list<\/span>\nsample_list = list(tuple1)\n<span class=\"hljs-comment\"># modify 2nd item<\/span>\nsample_list&#91;<span class=\"hljs-number\">1<\/span>] = <span class=\"hljs-number\">10<\/span>\n\n<span class=\"hljs-comment\"># converting list back into a tuple<\/span>\ntuple1 = tuple(sample_list)\nprint(tuple1)  \n<span class=\"hljs-comment\"># Output (0, 10, 2, 3, 4, 5)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code20', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code20');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output the last item has been updated from 3 to 11.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-removing-items-from-a-tuple\">Removing items from a tuple<\/h2>\n\n\n\n<p>Tuples are immutable so there are no <code>pop()<\/code> or <code>remove()<\/code> methods for the tuple. We can remove the items from a tuple using the following two ways.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Using del keyword<\/li><li>By converting it into a list<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-del-keyword\">Using del keyword<\/h3>\n\n\n\n<p>The <code>del<\/code> keyword will delete the entire tuple.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code21\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">sampletup1 =(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">8<\/span>,<span class=\"hljs-number\">9<\/span>,<span class=\"hljs-number\">10<\/span>)\n<span class=\"hljs-keyword\">del<\/span> sampletup1\n\nprint(sampletup1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code21', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code21');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      3 \n----&gt; 4 print(sampletup1)\n\nNameError: name 'sampletup1' is not defined<\/pre>\n\n\n\n<p>As seen in the above output we are getting error when we try to access a deleted tuple.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-by-converting-it-into-a-list\">By converting it into a List<\/h3>\n\n\n\n<p>We can convert a tuple into a list and then remove any one item using the <code>remove()<\/code> method. Then again we will convert it back into a tuple using the <code>tuple()<\/code> constructor.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code22\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># converting tuple into a list<\/span>\nsample_list = list(tuple1)\n<span class=\"hljs-comment\"># reomve 2nd item<\/span>\nsample_list.remove(<span class=\"hljs-number\">2<\/span>)\n\n<span class=\"hljs-comment\"># converting list back into a tuple<\/span>\ntuple1 = tuple(sample_list)\nprint(tuple1)  \n<span class=\"hljs-comment\"># Output (0, 1, 3, 4, 5)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code22', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code22');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output item 3 has been removed from the tuple.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-count-the-occurrence-of-an-item-in-a-tuple\">Count the occurrence of an item in a tuple<\/h2>\n\n\n\n<p>As we learned, a tuple can contain duplicate items. To determine how many times a specific item occurred in a tuple, we can use the <code>count()<\/code> method of a tuple object.<\/p>\n\n\n\n<p>The <code>count()<\/code> method accepts any value as a parameter and returns the number of times a particular value appears in a tuple.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code23\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">60<\/span>)\n<span class=\"hljs-comment\"># Count all occurrences of item 60<\/span>\ncount = tuple1.count(<span class=\"hljs-number\">60<\/span>)\nprint(count)\n<span class=\"hljs-comment\"># Output 3<\/span>\n\ncount = tuple1.count(<span class=\"hljs-number\">600<\/span>)\nprint(count)\n<span class=\"hljs-comment\"># Output 0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code23', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code23');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-copying-a-tuple\">Copying a tuple<\/h2>\n\n\n\n<p>We can create a copy of a tuple using the assignment operator <code>'='<\/code> . This operation will create only a reference copy and not a deep copy because tuples are immutable.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code24\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># copy tuple<\/span>\ntuple2 = tuple1\nprint(tuple2)\n<span class=\"hljs-comment\"># Output (0, 1, 2, 3, 4, 5)<\/span>\n\n<span class=\"hljs-comment\"># changing tuple2<\/span>\n<span class=\"hljs-comment\"># converting it into a list<\/span>\nsample_list = list(tuple2)\nsample_list.append(<span class=\"hljs-number\">6<\/span>)\n\n<span class=\"hljs-comment\"># converting list back into a tuple2<\/span>\ntuple2 = tuple(sample_list)\n\n<span class=\"hljs-comment\"># printing the two tuples<\/span>\nprint(tuple1)\n<span class=\"hljs-comment\"># Output (0, 1, 2, 3, 4, 5)<\/span>\nprint(tuple2)\n<span class=\"hljs-comment\"># Output (0, 1, 2, 3, 4, 5, 6)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code24', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code24');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output the tuple1 is not affected by the changes made in tuple2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-concatenating-two-tuples\">Concatenating two Tuples<\/h2>\n\n\n\n<p>We can concatenate two or more tuples in different ways. One thing to note here is that tuples allow duplicates, so if two tuples have the same item, it will be repeated twice in the resultant tuple. Let us see each one of them with a small example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-operator\">Using the <code>+<\/code> operator<\/h3>\n\n\n\n<p>We can add two tuples using the + operator. This is a very and straightforward method and the resultant tuple will have items from both the tuples.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code25\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\ntuple2 = (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>)\n\n<span class=\"hljs-comment\"># concatenate tuples using + operator<\/span>\ntuple3 = tuple1 + tuple2\nprint(tuple3)\n<span class=\"hljs-comment\"># Output (1, 2, 3, 4, 5, 3, 4, 5, 6, 7)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code25', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code25');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output the resultant tuple has items from both the tuples and the item 3, 4, 5 are repeated twice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-sum-function\">Using the sum() function<\/h3>\n\n\n\n<p>We can also use the Python built-in function <code>sum<\/code> to concatenate two tuples. But the sum function of two iterables like tuples always needs to start with Empty Tuple. Let us see this with an example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code26\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\ntuple2 = (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>)\n\n<span class=\"hljs-comment\"># using sum function<\/span>\ntuple3 = sum((tuple1, tuple2), ())\nprint(tuple3)\n<span class=\"hljs-comment\"># Output (1, 2, 3, 4, 5, 3, 4, 5, 6, 7)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code26', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code26');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above output the sum function takes an Empty tuple as an argument and it returns the items from both the tuples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-chain-function\">Using the chain() function<\/h3>\n\n\n\n<p>The <code>chain()<\/code> function is part of the itertools module in python. It makes an iterator, which will return all the first iterable items (a tuple in our case), which will be followed by the second iterable. We can pass any number of tuples to the chain() function.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code27\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> itertools\n\ntuple1 = (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>)\ntuple2 = (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>)\n\n<span class=\"hljs-comment\"># using itertools<\/span>\ntuple3 = tuple(item <span class=\"hljs-keyword\">for<\/span> item <span class=\"hljs-keyword\">in<\/span> itertools.chain(tuple1, tuple2))\nprint(tuple3)\n<span class=\"hljs-comment\"># Output (1, 2, 3, 4, 5, 3, 4, 5, 6, 7)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code27', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code27');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above output we can concatenate any number of tuples using the above method and it is more time-efficient than other methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-nested-tuples\">Nested tuples<\/h2>\n\n\n\n<p>Nested tuples are tuples within a tuple i.e., when a tuple contains another tuple as its member then it is called a nested tuple.<\/p>\n\n\n\n<p>In order to retrieve the items of the inner tuple we need a nested for loop<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code28\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">nested_tuple = ((<span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">60<\/span>), (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">50<\/span>), <span class=\"hljs-string\">\"Python\"<\/span>)\n\n<span class=\"hljs-comment\"># access the first item of the third tuple<\/span>\nprint(nested_tuple&#91;<span class=\"hljs-number\">2<\/span>]&#91;<span class=\"hljs-number\">0<\/span>])  <span class=\"hljs-comment\"># P<\/span>\n\n<span class=\"hljs-comment\"># iterate a nested tuple<\/span>\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> nested_tuple:\n    print(<span class=\"hljs-string\">\"tuple\"<\/span>, i, <span class=\"hljs-string\">\"elements\"<\/span>)\n    <span class=\"hljs-keyword\">for<\/span> j <span class=\"hljs-keyword\">in<\/span> i:\n        print(j, end=<span class=\"hljs-string\">\", \"<\/span>)\n    print(<span class=\"hljs-string\">\"\\n\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code28', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code28');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">P\ntuple (20, 40, 60) items\n20, 40, 60, \n\ntuple (10, 30, 50) items\n10, 30, 50, \n\ntuple Python items\nP, y, t, h, o, n,<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-use-built-in-functions-with-tuple\">Use built-in functions with tuple<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-min-and-max\">min() and max()<\/h3>\n\n\n\n<p>As the name suggests the <code>max()<\/code> function returns the maximum item in a tuple and <code>min()<\/code> returns the minimum value in a tuple.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code29\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-string\">'xyz'<\/span>, <span class=\"hljs-string\">'zara'<\/span>, <span class=\"hljs-string\">'abc'<\/span>)\n<span class=\"hljs-comment\"># The Maximum value in a string tuple<\/span>\nprint(max(tuple1))  \n<span class=\"hljs-comment\"># Output zara<\/span>\n\n<span class=\"hljs-comment\"># The minimum value in a string tuple<\/span>\nprint(min(tuple1))\n<span class=\"hljs-comment\"># Output abc<\/span>\n\ntuple2 = (<span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">22<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">4<\/span>)\n<span class=\"hljs-comment\"># The Maximum value in a integer tuple<\/span>\nprint(max(tuple2))\n<span class=\"hljs-comment\"># Output 22<\/span>\n<span class=\"hljs-comment\"># The minimum value in a integer tuple<\/span>\nprint(min(tuple2))\n<span class=\"hljs-comment\"># Output 4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code29', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code29');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note<\/strong>: We can&#8217;t find the <code>max() <\/code>and <code>min()<\/code> for a heterogeneous tuple (mixed types of items). It will throw <code>Type Error<\/code><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code30\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple3 = (<span class=\"hljs-string\">'a'<\/span>, <span class=\"hljs-string\">'e'<\/span>, <span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">22<\/span>, <span class=\"hljs-number\">15<\/span>)\n<span class=\"hljs-comment\"># max item<\/span>\nprint(max(tuple3))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code30', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code30');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-all\">all()<\/h3>\n\n\n\n<p>In the case of <code>all()<\/code> function, the return value will be true only when all the values inside are true. Let us see the different item values and the return values.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Item values in a tuple<\/th><th>Return value<\/th><\/tr><\/thead><tbody><tr><td>All values are True<\/td><td>True<\/td><\/tr><tr><td>One or more False values<\/td><td>False<\/td><\/tr><tr><td>All False values<\/td><td>False<\/td><\/tr><tr><td>Empty tuple<\/td><td>True<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code31\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># all() with All True values<\/span>\ntuple1 = (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">True<\/span>)\nprint(all(tuple1))  <span class=\"hljs-comment\"># True<\/span>\n\n<span class=\"hljs-comment\"># all() All True values<\/span>\ntuple1 = (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">True<\/span>)\nprint(all(tuple1))  <span class=\"hljs-comment\"># True<\/span>\n\n<span class=\"hljs-comment\"># all() with One false value<\/span>\ntuple2 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">True<\/span>, <span class=\"hljs-number\">1<\/span>)\nprint(all(tuple2))  <span class=\"hljs-comment\"># False<\/span>\n\n<span class=\"hljs-comment\"># all() with all false values<\/span>\ntuple3 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-literal\">False<\/span>)\nprint(all(tuple3))  <span class=\"hljs-comment\"># False<\/span>\n\n<span class=\"hljs-comment\"># all() Empty tuple<\/span>\ntuple4 = ()\nprint(all(tuple4))  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code31', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code31');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-any\">any()<\/h3>\n\n\n\n<p>The any() method will return true if there is at least one true value. In the case of the empty tuple, it will return false. Let us see the same possible combination of values for <code>any()<\/code> function in a tuple and its return values.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Item values in a tuple<\/th><th>Return value<\/th><\/tr><\/thead><tbody><tr><td>All values are True<\/td><td>True<\/td><\/tr><tr><td>One or more False values<\/td><td>True<\/td><\/tr><tr><td>All False values<\/td><td>False<\/td><\/tr><tr><td>Empty tuple<\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Similarly, let&#8217;s see each one of the above scenarios with a small example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code32\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># any() with All True values<\/span>\ntuple1 = (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">True<\/span>)\nprint(any(tuple1))  <span class=\"hljs-comment\"># True<\/span>\n\n<span class=\"hljs-comment\"># any() with One false value<\/span>\ntuple2 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">True<\/span>, <span class=\"hljs-number\">1<\/span>)\nprint(any(tuple2))  <span class=\"hljs-comment\"># True<\/span>\n\n<span class=\"hljs-comment\"># any() with all false values<\/span>\ntuple3 = (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-literal\">False<\/span>)\nprint(any(tuple3))  <span class=\"hljs-comment\"># False<\/span>\n\n<span class=\"hljs-comment\"># any() with Empty tuple<\/span>\ntuple4 = ()\nprint(any(tuple4))  <span class=\"hljs-comment\"># False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code32', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code32');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-when-to-use-tuple\"><strong>When to use Tuple?<\/strong><\/h2>\n\n\n\n<p>As tuples and lists are similar data structures, and they both allow sequential data storage, tuples are often referred to as immutable lists. So the tuples are used for the following requirements instead of lists.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>There are no <code>append()<\/code> or <code>extend()<\/code> to add items and similarly no <code>remove()<\/code> or <code>pop()<\/code> methods to remove items. This ensures that the data is write-protected. As the tuples are Unchangeable, they can be used to represent read-only or fixed data that does not change.<\/li><li>As they are immutable, they can be used as a key for the dictionaries, while lists cannot be used for this purpose.<\/li><li>As they are immutable, the search operation is much faster than the lists. This is because the id of the items remains constant.<\/li><li>Tuples contain heterogeneous (all types) data that offers huge flexibility in data that contains combinations of data types like alphanumeric characters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary-of-tuples-operations\">Summary of tuples operations<\/h2>\n\n\n\n<p>For the following examples, we assume that <code>t1<\/code> and <code>t2<\/code> are tuples, <code>x<\/code>, <code>i<\/code>, <code>j<\/code>, <code>k<\/code>, <code>n<\/code> are integers.<\/p>\n\n\n\n<p><code>t1 = (10, 20, 30, 40, 50)<\/code> and <code>t2 = (60, 70, 80, 60)<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Operation<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>x in t1<\/code><\/td><td>Check if the tuple <code>t1<\/code> contains the item <code>x<\/code>.<\/td><\/tr><tr><td><code>x not in t2<\/code><\/td><td>Check if the tuple <code>t1<\/code> does not contain the item <code>x<\/code>.<\/td><\/tr><tr><td><code>t1 + t2<\/code><\/td><td>Concatenate the tuples <code>t1<\/code> and<code> t2<\/code>. Creates a new tuple containing the items from <code>t1<\/code> and <code>t2<\/code>.<\/td><\/tr><tr><td><code>t1 * 5<\/code><\/td><td>Repeat the tuple <code>t1<\/code> 5 times.<\/td><\/tr><tr><td><code>t1[i]<\/code><\/td><td>Get the item at the index <code>i<\/code>. Example, <code>t1[2]<\/code> is 30<\/td><\/tr><tr><td><code>t1[i:j]<\/code><\/td><td>Tuple slicing. Get the items from index <code>i<\/code> up to index <code>j<\/code> (excluding j) as a tuple. An example <code>t1[0:2]<\/code> is (10, 20)<\/td><\/tr><tr><td><code>t1[i:j:k]<\/code><\/td><td>Tuple slicing with step. Return a tuple with the items from index <code>i<\/code> up to index j taking every k-th item. An example <code>t1[0:4:2]<\/code> is (10, 30)<\/td><\/tr><tr><td><code>len(t1)<\/code><\/td><td>Returns a count of total items in a tuple<\/td><\/tr><tr><td><code>t2.count(60)<\/code><\/td><td>Returns the number of times a particular item (60) appears in a tuple. Answer is 2<\/td><\/tr><tr><td><code>t1.index(30)<\/code><\/td><td>Returns the index number of a particular item(30) in a tuple. Answer is 2<\/td><\/tr><tr><td><code>t1.index(40, 2, 5)<\/code><\/td><td>Returns the index number of a particular item(30) in a tuple. But search only from index number 2 to 5.<\/td><\/tr><tr><td><code>min(t1)<\/code><\/td><td>Returns the item with a minimum value from a tuple<\/td><\/tr><tr><td><code>max<\/code>(t1)<\/td><td>Returns the item with maximum value from a tuple<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Next Steps<\/strong>:-<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-tuple-exercise-with-solutions\/\">Python Tuple Exercise<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-tuple-quiz\/\">Python Tuple Quiz<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn how to use a tuple data structure in Python. Also, learn how to create, access, and modify a tuple in Python and all other operations we can perform on a tuple. What is a Tuple Tuples are ordered collections of heterogeneous data that are unchangeable. Heterogeneous means tuple can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,1],"tags":[32],"class_list":{"0":"post-8183","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-basics","8":"tag-python-basics","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Tuples in Python &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Learn how to use a tuple data structure in Python. Create, access, and modify a tuple and all other operations we can perform on a tuple.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-tuples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tuples in Python\" \/>\n<meta property=\"og:description\" content=\"Learn how to use a tuple data structure in Python. Create, access, and modify a tuple and all other operations we can perform on a tuple.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-tuples\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-15T19:58:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-09T05:25:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Tuples in Python\",\"datePublished\":\"2021-02-15T19:58:12+00:00\",\"dateModified\":\"2021-04-09T05:25:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/\"},\"wordCount\":2706,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-tuple.jpg\",\"keywords\":[\"Python Basics\"],\"articleSection\":[\"Python\",\"Python Basics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/\",\"name\":\"Tuples in Python &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-tuple.jpg\",\"datePublished\":\"2021-02-15T19:58:12+00:00\",\"dateModified\":\"2021-04-09T05:25:24+00:00\",\"description\":\"Learn how to use a tuple data structure in Python. Create, access, and modify a tuple and all other operations we can perform on a tuple.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#primaryimage\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-tuple.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-tuple.jpg\",\"width\":682,\"height\":401,\"caption\":\"Python tuples\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-tuples\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Tuples in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Tuples in Python &#8211; PYnative","description":"Learn how to use a tuple data structure in Python. Create, access, and modify a tuple and all other operations we can perform on a tuple.","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:\/\/pynative.com\/python-tuples\/","og_locale":"en_US","og_type":"article","og_title":"Tuples in Python","og_description":"Learn how to use a tuple data structure in Python. Create, access, and modify a tuple and all other operations we can perform on a tuple.","og_url":"https:\/\/pynative.com\/python-tuples\/","og_site_name":"PYnative","article_published_time":"2021-02-15T19:58:12+00:00","article_modified_time":"2021-04-09T05:25:24+00:00","og_image":[{"url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg","type":"","width":"","height":""}],"author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-tuples\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-tuples\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Tuples in Python","datePublished":"2021-02-15T19:58:12+00:00","dateModified":"2021-04-09T05:25:24+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-tuples\/"},"wordCount":2706,"commentCount":5,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"image":{"@id":"https:\/\/pynative.com\/python-tuples\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg","keywords":["Python Basics"],"articleSection":["Python","Python Basics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-tuples\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-tuples\/","url":"https:\/\/pynative.com\/python-tuples\/","name":"Tuples in Python &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/pynative.com\/python-tuples\/#primaryimage"},"image":{"@id":"https:\/\/pynative.com\/python-tuples\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg","datePublished":"2021-02-15T19:58:12+00:00","dateModified":"2021-04-09T05:25:24+00:00","description":"Learn how to use a tuple data structure in Python. Create, access, and modify a tuple and all other operations we can perform on a tuple.","breadcrumb":{"@id":"https:\/\/pynative.com\/python-tuples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-tuples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/python-tuples\/#primaryimage","url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-tuple.jpg","width":682,"height":401,"caption":"Python tuples"},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-tuples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Tuples in Python"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/8183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=8183"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/8183\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=8183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=8183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=8183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}