{"id":567,"date":"2024-07-24T15:59:23","date_gmt":"2024-07-24T10:59:23","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=567"},"modified":"2024-10-05T09:05:46","modified_gmt":"2024-10-05T04:05:46","slug":"python-tuples","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-tuples\/","title":{"rendered":"Python: Tuples"},"content":{"rendered":"\n<p><strong>Tuples<\/strong> are just like Python lists except that they are immutable. You can not add, delete or change elements after creating a Tuple instance. <strong>Tuples<\/strong> is a built-in data type in Python to store data collections. A tuple is a collection of values separated by commas and enclosed in parentheses. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Allow Duplicates<\/strong>: Tuples can contain duplicate values.<\/li>\n\n\n\n<li><strong>Ordered<\/strong>: Tuple items have a defined order, and that order remains constant.<\/li>\n\n\n\n<li><strong>Unchangeable<\/strong>: Once you create a tuple, you can\u2019t add, remove, or modify its elements.<\/li>\n<\/ul>\n\n\n\n<p>Remember, tuples are great for situations where you need an ordered, unchangeable collection of values.<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-fe9cc265 wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>tpl = (1, 4, 5, True)<\/strong>\n<strong>print (tpl &#91;1])<\/strong>     <em># outputs 4<\/em>\n<strong>tpl &#91;1] = 10<\/strong>        <em># throws an Exception of type TypeError<\/em>\n<strong>del tpl&#91;1]<\/strong>           <em># throws an Exception of type TypeError<\/em><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Accessing Items<\/h3>\n\n\n\n<p>You can access tuple items using indexing (starting from <strong>0<\/strong>):<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>tpl = (\"Aaliyan\", \"Muhammad\", \"Ali\")<\/strong>\n\n<strong>print(tpl&#91;0])<\/strong><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">Outputs =&gt; <strong>Aaliyan<\/strong><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Length of a Tuple<\/h3>\n\n\n\n<p>Use the <strong>len() <\/strong>function to find the number of items in a tuple:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>print(len(tpl))<\/strong><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">Outputs =&gt; <strong>3<\/strong><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Tuple with One Item<\/h3>\n\n\n\n<p>If you want a single-item tuple, include a comma after the item:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>single_item_tuple = (\"Aaliyan\",)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Data Types in Tuples<\/h3>\n\n\n\n<p>Tuples can hold different data types.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>mixed_tuple = (\"abc\", 34, True)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Type of a Tuple<\/h3>\n\n\n\n<p>Python defines tuples as objects with the data type &#8220;<strong>tuple<\/strong>&#8220;<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>print(type(mytuple))<\/strong><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">Outputs =&gt; <strong>&lt;class &#8216;tuple&#8217;&gt;<\/strong><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tuples are just like Python lists except that they are immutable. You can not add, delete or change elements after creating a Tuple instance. Tuples is a built-in data type in Python to store data collections. A tuple is a collection of values separated by commas and enclosed in parentheses. Remember, tuples are great for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"none","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"both","_seopress_redirections_param":"","_seopress_redirections_type":301,"_seopress_analysis_target_kw":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-567","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":22,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/567\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}