{"id":2646,"date":"2026-10-19T10:27:29","date_gmt":"2026-10-19T10:27:29","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=2646"},"modified":"2026-10-20T05:20:28","modified_gmt":"2026-10-20T05:20:28","slug":"python-tuple-tutorials-with-examples","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/","title":{"rendered":"Python Tuple Tutorial with Examples"},"content":{"rendered":"<h1><strong><b>Python Tuple Tutorial for Beginners<\/b><\/strong><\/h1>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Some information should not change once you have written it. The latitude and longitude of Kolkata. The days of the week. The red, green and blue values of a colour. Python has a data type made for exactly these cases: the tuple. It holds a group of values together, in order, and then locks them.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A tuple looks a lot like a list, and it is the list&#8217;s quieter cousin. The big difference is that a tuple cannot be changed after it is created. That sounds like a limit, but it is a useful promise. When you see a tuple in code, you know its contents will not shift under you. Python also uses tuples everywhere behind the scenes, for example when a function returns two values at once.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">In this tutorial, we explain what a tuple is and how to create one, including the one item tuple that trips up many beginners. We cover repeating, nesting, slicing, and what immutable really means, including a trap where a tuple appears to change. Then we go through the tuple operations: length, count, index, conversion, comparison and membership. We add unpacking, a comparison with lists, common mistakes and the full program. Every output on this page comes from a real run on Python 3.13.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">What is a Python Tuple?<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A tuple is an ordered collection of values written in round brackets, with commas between the values. Here is what defines it:<\/p>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\"><strong>Ordered.<\/strong> The values keep the order you gave them.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Indexed.<\/strong> You reach each value by position, starting at 0.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Immutable.<\/strong> You cannot change, add or remove values after the tuple is created.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Allows duplicates.<\/strong> The same value can appear more than once.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Can mix types.<\/strong> Numbers, text and other objects can sit in one tuple.<\/li>\n<\/ul>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Lists use square brackets and can be changed. Tuples use round brackets and cannot.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">How to Create a Tuple in Python?<\/h2>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">tup1 = (1, 2, 3, 6, 7, 8)\r\ntup2 = (\"green\", \"yellow\", \"red\")\r\ntup3 = \"green\", \"red\", \"yellow\"\r\nprint(tup1, tup2, tup3)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(1, 2, 3, 6, 7, 8) ('green', 'yellow', 'red') ('green', 'red', 'yellow')<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Notice that <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">tup3<\/code> has no brackets at all. It is the comma that makes a tuple, not the brackets. Brackets are only there to make it easier to read. Writing values separated by commas is called tuple packing.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">An Empty Tuple<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">empty = ()\r\nprint(empty)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">()<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">A Tuple With One Item<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">This is the most common beginner mistake. A single value in brackets is not a tuple. You need a comma after it.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">single = (15,)\r\nnot_a_tuple = (15)\r\nprint(single, type(single), type(not_a_tuple))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(15,) &lt;class 'tuple'&gt; &lt;class 'int'&gt;<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The brackets in <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">(15)<\/code> only group a number, so it stays an integer.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Using the tuple() Function<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print(tuple([1, 2, 3]))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(1, 2, 3)<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Repeating Tuples N Times<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The star repeats whatever sits before it. With a plain bracketed string, you get one long string. Add a comma and you get a tuple of repeated items.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print((\"stats\") * 4)\r\nprint((\"stats\",) * 4)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">statsstatsstatsstats\r\n('stats', 'stats', 'stats', 'stats')<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Nested Tuples in Python<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A tuple can hold other tuples. The result keeps its layers. It is not flattened.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">a = (0, 4, 8, 12)\r\nb = (\"tuple\", \"python\")\r\nnested = (a, b)\r\nprint(nested)\r\nprint(nested[1][0])<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">((0, 4, 8, 12), ('tuple', 'python'))\r\ntuple<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Use one index per layer. <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">nested[1]<\/code> is the second inner tuple, and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">nested[1][0]<\/code> is the first item inside it.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">If you do want a single flat tuple, join them with a plus sign instead:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">a = (0, 4, 8, 12)\r\nb = (\"tuple\", \"python\")\r\nprint(a + b)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(0, 4, 8, 12, 'tuple', 'python')<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Immutable Tuple in Python<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Try to change an item and Python refuses:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">tup = (0, 1, \"x\", \"y\")\r\ntry:\r\n    tup[0] = 4\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">TypeError: 'tuple' object does not support item assignment<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">To get a changed version, build a new tuple. Joining and slicing both create new tuples and leave the original as it was.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">The Trap: A Tuple Can Hold a List<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Immutable means the tuple cannot swap one item for another. But if an item is itself a list, that list can still change:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">mixed = (1, [2, 3])\r\nmixed[1].append(4)\r\nprint(mixed)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(1, [2, 3, 4])<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The tuple still holds the same list. The list changed inside it. Keep this in mind if you rely on a tuple being fully fixed.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Slicing a Tuple in Python<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Slicing works exactly like it does for strings and lists. The pattern is <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">tuple[start:end:step]<\/code>. The start is included, and the end is not.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">test = (0, 2, 4, 5, 6, 7, 8, 9, 12)\r\nprint(test[1:])\r\nprint(test[2:5])\r\nprint(test[::-1])\r\nprint(test[-3:-1])<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(2, 4, 5, 6, 7, 8, 9, 12)\r\n(4, 5, 6)\r\n(12, 9, 8, 7, 6, 5, 4, 2, 0)\r\n(8, 9)<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The reverse slice, with a step of minus one, goes backward through the whole tuple.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Tuple slice examples<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 160px;\" scope=\"col\">Slice<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Meaning<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Result<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">test[1:]<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">From position 1 to the end<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">(2, 4, 5, 6, 7, 8, 9, 12)<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">test[2:5]<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Positions 2, 3 and 4<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">(4, 5, 6)<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">test[::-1]<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Everything, backward<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">(12, 9, 8, 7, 6, 5, 4, 2, 0)<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">test[-3:-1]<\/th>\n<td style=\"padding: 12px 16px;\">From the 3rd last to before the last<\/td>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">(8, 9)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Tuple Operations in Python<\/h2>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Deleting a Tuple<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You cannot delete one item from a tuple. You can delete the whole tuple with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">del<\/code>.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">gone = (0, 1, \"a\", \"b\")\r\ndel gone\r\ntry:\r\n    print(gone)\r\nexcept NameError as error:\r\n    print(\"NameError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">NameError: name 'gone' is not defined<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The error names the tuple that was deleted. If you only want to remove one item, convert to a list, remove it, and convert back, or build a new tuple by slicing.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Length of a Tuple<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print(len((1, 2, 5, \"testing\", \"length\", \"of\", \"tuple\")))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">7<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">count()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">count<\/code> tells you how many times a value appears.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">counts = (0, 1, 2, 3, 1, 2, 4, 5, 6, 1, 2, 1)\r\nprint(counts.count(1))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">4<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">index()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">index<\/code> gives the position of the first match. If the value is missing, it raises a ValueError.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">words = (1, 2, 5, \"a\", \"python\", \"tuple\")\r\nprint(words.index(\"python\"))\r\ntry:\r\n    words.index(\"zzz\")\r\nexcept ValueError as error:\r\n    print(\"ValueError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">4\r\nValueError: tuple.index(x): x not in tuple<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">These two, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">count<\/code> and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">index<\/code>, are the only methods a tuple has. That is because it cannot change, so it has no append, remove or sort.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Converting Between Lists, Strings and Tuples<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The conversion only happens when you call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">tuple()<\/code>:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">lis1 = [1, 2, 4, \"a\", \"b\", \"c\", \"e\"]\r\nprint(lis1)\r\nprint(tuple(lis1))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">[1, 2, 4, 'a', 'b', 'c', 'e']\r\n(1, 2, 4, 'a', 'b', 'c', 'e')<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A string turns into a tuple of its characters:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print(tuple(\"python tuple\"))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">('p', 'y', 't', 'h', 'o', 'n', ' ', 't', 'u', 'p', 'l', 'e')<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">To change a tuple, go through a list and back:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">tup = (0, 1, \"x\", \"y\")\r\nas_list = list(tup)\r\nas_list[0] = 4\r\nprint(tuple(as_list))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(4, 1, 'x', 'y')<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Comparing Tuples<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The old <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">cmp()<\/code> function was removed in Python 3. Use the comparison signs, or min and max. Python compares tuples item by item from the left, and the first difference decides. If one tuple is a prefix of the other, the shorter one is smaller.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">t1 = (1, 2, 3, 4, 5)\r\nt2 = (1, 2, 3, 4, 5, 6)\r\nprint(min(t1, t2), max(t1, t2))\r\nprint(t1 &lt; t2, t1 == t2, (1, 9) &lt; (2, 0))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">(1, 2, 3, 4, 5) (1, 2, 3, 4, 5, 6)\r\nTrue False True<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The last comparison is True because 1 is less than 2, and that first difference settles it. The 9 and 0 never matter.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Checking for an Element in a Tuple<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">check = (1, 2, 3, \"a\", \"b\", \"c\")\r\nprint(3 in check, \"x\" in check, \"x\" not in check)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">True False True<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Packing and Unpacking<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Unpacking is where tuples shine in real code. You can pull the values out into separate names in one line. The number of names must match the number of values, unless you use a star to collect the rest.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">point = (3, 5)\r\nx, y = point\r\nprint(x, y)\r\nx, y = y, x\r\nprint(x, y)\r\nfirst, *rest = (10, 20, 30, 40)\r\nprint(first, rest)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">3 5\r\n5 3\r\n10 [20, 30, 40]<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The second line swaps two values with no temporary variable. This works because Python packs the right side into a tuple and then unpacks it. Functions that return two values are really returning one tuple.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Why Use a Tuple Instead of a List?<\/h2>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\"><strong>Safety.<\/strong> The values cannot be changed by mistake.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Signals intent.<\/strong> A tuple tells the reader this group is fixed.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Dictionary keys.<\/strong> A tuple can be a dictionary key, but a list cannot, because a list can change.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Slightly lighter.<\/strong> A tuple generally uses less memory than a list of the same items.<\/li>\n<\/ul>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">locations = {(22.57, 88.36): \"Kolkata\"}\r\nprint(locations[(22.57, 88.36)])<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">Kolkata<\/pre>\n<\/div>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Tuple compared with list<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 220px;\" scope=\"col\">Point<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Tuple<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">List<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Brackets<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Round, ( )<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Square, [ ]<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Changeable<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Methods<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Only count and index<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Many, such as append, remove and sort<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Use as a dictionary key<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes, if its items are unchangeable<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Best for<\/th>\n<td style=\"padding: 12px 16px;\">Fixed groups, such as coordinates<\/td>\n<td style=\"padding: 12px 16px;\">Collections that grow or change<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Common Mistakes to Avoid<\/h2>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\"><strong>Forgetting the comma in a one item tuple.<\/strong> <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">(15)<\/code> is an integer. Write <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">(15,)<\/code>.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Trying to change an item.<\/strong> It raises a TypeError. Build a new tuple instead.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Assuming a tuple is fully frozen.<\/strong> A list inside it can still change.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Using cmp().<\/strong> It does not exist in Python 3.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Capital P in Print.<\/strong> Python is case sensitive, so use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">print<\/code>.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Mismatched names when unpacking.<\/strong> Two names for three values raises a ValueError.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Expecting a tuple to flatten when nested.<\/strong> It stays layered. Use plus to join.<\/li>\n<\/ul>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Try It Yourself: The Full Program<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The program below holds every example in this tutorial. Copy it into a file, run it, and compare your output with what you see above.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import sys\r\nprint(\"Python\", sys.version_info.major, sys.version_info.minor)\r\n\r\n# Creating tuples\r\ntup1 = (1, 2, 3, 6, 7, 8)\r\ntup2 = (\"green\", \"yellow\", \"red\")\r\ntup3 = \"green\", \"red\", \"yellow\"\r\nempty = ()\r\nsingle = (15,)\r\nnot_a_tuple = (15)\r\nprint(tup1, tup2, tup3)\r\nprint(empty, single, type(single), type(not_a_tuple))\r\nprint(tuple([1, 2, 3]))\r\n\r\n# Repeating\r\nprint((\"stats\") * 4)\r\nprint((\"stats\",) * 4)\r\n\r\n# Nested tuples\r\na = (0, 4, 8, 12)\r\nb = (\"tuple\", \"python\")\r\nnested = (a, b)\r\nprint(nested)\r\nprint(nested[1][0])\r\nprint(a + b)\r\n\r\n# Immutable\r\ntup = (0, 1, \"x\", \"y\")\r\ntry:\r\n    tup[0] = 4\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)\r\n\r\n# A tuple can hold a list, and the list can change\r\nmixed = (1, [2, 3])\r\nmixed[1].append(4)\r\nprint(mixed)\r\n\r\n# Slicing\r\ntest = (0, 2, 4, 5, 6, 7, 8, 9, 12)\r\nprint(test[1:])\r\nprint(test[2:5])\r\nprint(test[::-1])\r\nprint(test[-3:-1])\r\n\r\n# Operations\r\nprint(len((1, 2, 5, \"testing\", \"length\", \"of\", \"tuple\")))\r\ncounts = (0, 1, 2, 3, 1, 2, 4, 5, 6, 1, 2, 1)\r\nprint(counts.count(1))\r\nwords = (1, 2, 5, \"a\", \"python\", \"tuple\")\r\nprint(words.index(\"python\"))\r\ntry:\r\n    words.index(\"zzz\")\r\nexcept ValueError as error:\r\n    print(\"ValueError:\", error)\r\n\r\n# Converting\r\nlis1 = [1, 2, 4, \"a\", \"b\", \"c\", \"e\"]\r\nprint(lis1)\r\nprint(tuple(lis1))\r\nprint(tuple(\"python tuple\"))\r\nas_list = list(tup)\r\nas_list[0] = 4\r\nprint(tuple(as_list))\r\n\r\n# Comparing\r\nt1 = (1, 2, 3, 4, 5)\r\nt2 = (1, 2, 3, 4, 5, 6)\r\nprint(min(t1, t2), max(t1, t2))\r\nprint(t1 &lt; t2, t1 == t2, (1, 9) &lt; (2, 0))\r\n\r\n# Membership\r\ncheck = (1, 2, 3, \"a\", \"b\", \"c\")\r\nprint(3 in check, \"x\" in check, \"x\" not in check)\r\n\r\n# Packing and unpacking\r\npoint = (3, 5)\r\nx, y = point\r\nprint(x, y)\r\nx, y = y, x\r\nprint(x, y)\r\nfirst, *rest = (10, 20, 30, 40)\r\nprint(first, rest)\r\n\r\n# A tuple can be a dictionary key, a list cannot\r\nlocations = {(22.57, 88.36): \"Kolkata\"}\r\nprint(locations[(22.57, 88.36)])\r\n\r\n# Deleting a whole tuple\r\ngone = (0, 1, \"a\", \"b\")\r\ndel gone\r\ntry:\r\n    print(gone)\r\nexcept NameError as error:\r\n    print(\"NameError:\", error)<\/pre>\n<\/div>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/Ec3RgAFJUnc\" width=\"650\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>A tuple is a small idea with a clear job: group values and lock them. Run the examples, try to break them, and watch the error messages, which teach as much as the successes. Once tuples feel natural, move on to dictionaries and sets to complete the main Python data types.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Tuple Tutorial for Beginners Some information should not change once you have written it. The latitude and longitude of Kolkata. The days of the week. The red, green and blue values of a colour. Python has a data type made for exactly these cases: the tuple. It holds a group of values together, in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3435,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,1671],"tags":[546,543,545,547,79],"class_list":["post-2646","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-data-types","tag-online-python-programming-course","tag-online-training-courses-for-python","tag-python-training-course-online","tag-python-training-online","tag-python-tuples-tutorials-with-examples"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Tuple Tutorial for Beginners With Examples | PrwaTech<\/title>\n<meta name=\"description\" content=\"Learn Python tuples step by step: create, slice, nest and unpack them, use count and index, convert lists, and see how tuples differ from lists.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tuple Tutorial for Beginners With Examples | PrwaTech\" \/>\n<meta property=\"og:description\" content=\"Learn Python tuples step by step: create, slice, nest and unpack them, use count and index, convert lists, and see how tuples differ from lists.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-19T10:27:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-20T05:20:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/\",\"url\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/\",\"name\":\"Python Tuple Tutorial for Beginners With Examples | PrwaTech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg\",\"datePublished\":\"2026-10-19T10:27:29+00:00\",\"dateModified\":\"2026-10-20T05:20:28+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn Python tuples step by step: create, slice, nest and unpack them, use count and index, convert lists, and see how tuples differ from lists.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg\",\"width\":960,\"height\":550,\"caption\":\"Python Tuples Tutorials with Best Examples\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Tuple Tutorial with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tuple Tutorial for Beginners With Examples | PrwaTech","description":"Learn Python tuples step by step: create, slice, nest and unpack them, use count and index, convert lists, and see how tuples differ from lists.","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:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/","og_locale":"en_US","og_type":"article","og_title":"Python Tuple Tutorial for Beginners With Examples | PrwaTech","og_description":"Learn Python tuples step by step: create, slice, nest and unpack them, use count and index, convert lists, and see how tuples differ from lists.","og_url":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-19T10:27:29+00:00","article_modified_time":"2026-10-20T05:20:28+00:00","og_image":[{"width":960,"height":550,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg","type":"image\/jpeg"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/","url":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/","name":"Python Tuple Tutorial for Beginners With Examples | PrwaTech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg","datePublished":"2026-10-19T10:27:29+00:00","dateModified":"2026-10-20T05:20:28+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn Python tuples step by step: create, slice, nest and unpack them, use count and index, convert lists, and see how tuples differ from lists.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Tuples-Tutorials-with-Best-Examples.jpg","width":960,"height":550,"caption":"Python Tuples Tutorials with Best Examples"},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-tuple-tutorials-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Tuple Tutorial with Examples"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=2646"}],"version-history":[{"count":15,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2646\/revisions"}],"predecessor-version":[{"id":12036,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2646\/revisions\/12036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media\/3435"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=2646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=2646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=2646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}