{"id":329478,"date":"2023-10-04T14:00:00","date_gmt":"2023-10-04T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python-tuple\/"},"modified":"2023-10-04T14:00:00","modified_gmt":"2023-10-04T14:00:00","slug":"pythons-tuple-data-type-a-deep-dive-with-examples","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/10\/04\/pythons-tuple-data-type-a-deep-dive-with-examples\/","title":{"rendered":"Python&#8217;s tuple Data Type: A Deep Dive With Examples"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p>In Python, a <strong><code>tuple<\/code><\/strong> is a built-in data type that allows you to create <strong>immutable sequences<\/strong> of values. The values or items in a tuple can be of any type. This makes tuples pretty useful in those situations where you need to store heterogeneous data, like that in a database record, for example.<\/p>\n<p>Through this tutorial, you\u2019ll dive deep into Python tuples and get a solid understanding of their key features and use cases. This knowledge will allow you to write more efficient and reliable code by taking advantage of tuples.<\/p>\n<p><strong>In this tutorial, you\u2019ll learn how to:<\/strong><\/p>\n<ul>\n<li><strong>Create<\/strong> tuples in Python<\/li>\n<li><strong>Access<\/strong> the items in an existing tuple<\/li>\n<li><strong>Unpack<\/strong>, <strong>return<\/strong>, <strong>copy<\/strong>, and <strong>concatenate<\/strong> tuples<\/li>\n<li><strong>Reverse<\/strong>, <strong>sort<\/strong>, and <strong>traverse<\/strong> existing tuples<\/li>\n<li>Explore other <strong>features<\/strong> and common <strong>gotchas<\/strong> of tuples<\/li>\n<\/ul>\n<p>In addition, you\u2019ll explore some alternative tools that you can use to replace tuples and make your code more readable and explicit.<\/p>\n<p>To get the most out of this tutorial, you should have a good understanding of a few Python concepts, including <a href=\"https:\/\/realpython.com\/python-variables\/\">variables<\/a>, <a href=\"https:\/\/realpython.com\/defining-your-own-python-function\/\">functions<\/a>, and <a href=\"https:\/\/realpython.com\/python-for-loop\/\"><code>for<\/code> loops<\/a>. Familiarity with other built-in <a href=\"https:\/\/realpython.com\/python-data-types\/\">data structures<\/a>, especially <a href=\"https:\/\/realpython.com\/python-list\/\">lists<\/a>, is also a plus.<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Get Your Code:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/python-tuple-code\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download the free sample code<\/a> that shows you how to write more readable code with tuples in Python.<\/p>\n<\/div>\n<h2 id=\"getting-started-with-pythons-tuple-data-type\">Getting Started With Python\u2019s <code>tuple<\/code> Data Type<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python-tuple\/#getting-started-with-pythons-tuple-data-type\" title=\"Permanent link\"><\/a><\/h2>\n<p>The built-in <a href=\"https:\/\/realpython.com\/python-lists-tuples\/#python-tuples\"><code>tuple<\/code><\/a> data type is probably the most elementary <a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-sequence\">sequence<\/a> available in Python. Tuples are <a href=\"https:\/\/realpython.com\/python-mutable-vs-immutable-types\/#tuples\">immutable<\/a> and can store a fixed number of items. For example, you can use tuples to represent <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cartesian_coordinate_system\">Cartesian<\/a> coordinates <code>(x, y)<\/code>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/RGB_color_model\">RGB<\/a> colors <code>(red, green, blue)<\/code>, records in a database table <code>(name, age, job)<\/code>, and many other sequences of values.<\/p>\n<p>In all these use cases, the number of elements in the underlying tuple is <em>fixed<\/em>, and the items are <em>unchangeable<\/em>. You may find several situations where these two characteristics are desirable. For example, consider the RGB color example:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">red<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"mi\">255<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">)<\/span>\n<\/code><\/pre>\n<\/div>\n<p>Once you\u2019ve defined <code>red<\/code>, then you won\u2019t need to add or change any components. Why? If you change the value of one component, then you won\u2019t have a pure red color anymore, and your variable name will be misleading. If you add a new component, then your color won\u2019t be an RGB color. So, tuples are perfect for representing this type of object.<\/p>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> Throughout this tutorial, you\u2019ll find the terms <strong>items<\/strong>, <strong>elements<\/strong>, and <strong>values<\/strong> used interchangeably to refer to the objects stored in a tuple.<\/p>\n<\/div>\n<p>Some of the most relevant characteristics of <code>tuple<\/code> objects include the following:<\/p>\n<ul>\n<li><strong>Ordered<\/strong>: They contain elements that are sequentially arranged according to their specific insertion order.<\/li>\n<li><strong>Lightweight<\/strong>: They consume relatively small amounts of memory compared to other sequences like lists.<\/li>\n<li><strong>Indexable through a zero-based index<\/strong>: They allow you to access their elements by integer indices that start from zero.<\/li>\n<li><strong>Immutable<\/strong>: They don\u2019t support in-place mutations or changes to their contained elements. They don\u2019t support growing or shrinking operations.<\/li>\n<li><strong>Heterogeneous<\/strong>: They can store objects of different data types and domains, including mutable objects.<\/li>\n<li><strong>Nestable<\/strong>: They can contain other tuples, so you can have tuples of tuples.<\/li>\n<li><strong>Iterable<\/strong>: They support iteration, so you can traverse them using a loop or comprehension while you perform operations with each of their elements.<\/li>\n<li><strong>Sliceable<\/strong>: They support slicing operations, meaning that you can extract a series of elements from a tuple.<\/li>\n<li><strong>Combinable<\/strong>: They support concatenation operations, so you can combine two or more tuples using the concatenation operators, which creates a new tuple.<\/li>\n<li><strong>Hashable<\/strong>: They can work as keys in <a href=\"https:\/\/realpython.com\/python-dicts\/\">dictionaries<\/a> when all the tuple items are immutable.<\/li>\n<\/ul>\n<p>Tuples are sequences of objects. They\u2019re commonly called <strong>containers<\/strong> or <strong>collections<\/strong> because a single tuple can contain or collect an arbitrary number of other objects.<\/p>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> In Python, tuples support several operations that are common to other sequence types, such as lists, <a href=\"https:\/\/realpython.com\/python-strings\/\">strings<\/a>, and <a href=\"https:\/\/realpython.com\/python-range\/\">ranges<\/a>. These operations are known as <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#common-sequence-operations\">common sequence operations<\/a>. Throughout this tutorial, you\u2019ll learn about several operations that fall into this category.<\/p>\n<\/div>\n<p>In Python, tuples are ordered, which means that they keep their elements in the original insertion order:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">record<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"s2\">\"John\"<\/span><span class=\"p\">,<\/span> <span class=\"mi\">35<\/span><span class=\"p\">,<\/span> <span class=\"s2\">\"Python Developer\"<\/span><span class=\"p\">)<\/span>\n\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">record<\/span>\n<span class=\"go\">('John', 35, 'Python Developer')<\/span>\n<\/code><\/pre>\n<\/div>\n<p>The items in this tuple are objects of different data types representing a record of data from a database table. If you access the tuple object, then you\u2019ll see that the data items keep the same original insertion order. This order remains unchanged during the tuple\u2019s lifetime.<\/p>\n<p>You can access individual objects in a tuple by position, or index. These indices start from zero:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">record<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span>\n<span class=\"go\">'John'<\/span>\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">record<\/span><span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]<\/span>\n<span class=\"go\">35<\/span>\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">record<\/span><span class=\"p\">[<\/span><span class=\"mi\">2<\/span><span class=\"p\">]<\/span>\n<span class=\"go\">'Python Developer'<\/span>\n<\/code><\/pre>\n<\/div>\n<p>Positions are numbered from zero to the length of the tuple minus one. The element at index <code>0<\/code> is the first element in the tuple, the element at index <code>1<\/code> is the second, and so on.<\/p>\n<p>Cool! You\u2019ve had a first glance at tuples. It\u2019s time to dive deeper into all of the above characteristics of tuples and more. To kick things off, you\u2019ll start by learning the different ways to create tuples in Python.<\/p>\n<h2 id=\"constructing-tuples-in-python\">Constructing Tuples in Python<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python-tuple\/#constructing-tuples-in-python\" title=\"Permanent link\"><\/a><\/h2>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python-tuple\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python-tuple\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In Python, a tuple is a built-in data type that allows you to create immutable sequences of values. The values or items in a tuple can be of any type. This makes tuples pretty useful in those situations where you need to store heterogeneous data, like that in a database record, for example.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python's tuple Data Type: A Deep Dive With Examples - ITTeacherITFreelance.hk","description":"In Python, a tuple is a built-in data type that allows you to create immutable sequences of values. The values or items in a tuple can be of any type. This make"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329478"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329478"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329478\/revisions"}],"predecessor-version":[{"id":329479,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329478\/revisions\/329479"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}