{"id":2934,"date":"2018-03-10T05:17:17","date_gmt":"2018-03-10T05:17:17","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2934"},"modified":"2018-03-10T12:33:33","modified_gmt":"2018-03-10T12:33:33","slug":"python-2-7-fundamentals-collections-tuples","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/03\/10\/python-2-7-fundamentals-collections-tuples\/","title":{"rendered":"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Tuples"},"content":{"rendered":"<p>Tuples are very similar to lists, except that they are immutable. In other words, once a tuple is created you cannot modify it in any way. Tuples are enclosed in normal brackets and the items separated by commas. Items can be of mixed data types. Some examples are shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3030\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuple1.png\" alt=\"\" width=\"605\" height=\"140\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuple1.png 605w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuple1-300x69.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Operators<\/strong><\/h3>\n<p>The following operators are available for tuples:<\/p>\n<ol>\n<li>[] &#8211; index operator.<\/li>\n<li>\u00a0+ &#8211; concatenation<\/li>\n<li>* &#8211; repetition<\/li>\n<li>[:] \u00a0&#8211; slicing<\/li>\n<li>\u00a0in &#8211; membership<\/li>\n<li>len &#8211; length (this is a function not really an operator)<\/li>\n<\/ol>\n<p>We will look at the above in more detail.<\/p>\n<p><strong>[] &#8211; index operator<\/strong><\/p>\n<p>Tuples are collections of data types. So to access individual items in a tuple we use the index operator. The lowest index value is zero which represents the first item and the highest index value is the length of the tuple minus one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3033\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplea.png\" alt=\"\" width=\"599\" height=\"131\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplea.png 599w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplea-300x66.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Trying to access an index beyond the length of the tuple gives an error.<\/p>\n<p><strong>+ &#8211; concatenation<\/strong><\/p>\n<p>This operator lets you join two tuples together.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3034\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tupleb.png\" alt=\"\" width=\"592\" height=\"88\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tupleb.png 592w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tupleb-300x45.png 300w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>*- repetition<\/strong><\/p>\n<p>This repeats the tuple x number of times.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3035\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplec.png\" alt=\"\" width=\"582\" height=\"58\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplec.png 582w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplec-300x30.png 300w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>[:] slicing<\/strong><\/p>\n<p>The index operator lets you extract one item from the tuple. The slicing operator lets us extract multiple items from a tuple. The operator takes two numbers [x:y] where x is the starting index and y is the ending index. Note that the ending index is not inclusive eg. if you try print name[1:3] it will extract character at index 1 and then stop at index 2.<\/p>\n<p>The slicing operator has various permutations and combinations as shown below:<\/p>\n<ul>\n<li>[x:y] &#8211; extract from x and stop before y<\/li>\n<li>[x:] &#8211; extract from x till the rest of the string<\/li>\n<li>[:y] &#8211; extract from the first item till y<\/li>\n<li>[-x:] &#8211; extract x item from the end<\/li>\n<li>[:-y] &#8211; extract from the first item and stop before y<\/li>\n<li>[:] &#8211; no x and y . Extract the whole tuple<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3036\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tupled.png\" alt=\"\" width=\"606\" height=\"135\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tupled.png 606w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tupled-300x67.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>in &#8211; membership<\/strong><\/p>\n<p>The in operator returns True if an item is in the tuple else False<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3037\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplee.png\" alt=\"\" width=\"598\" height=\"84\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplee.png 598w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplee-300x42.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>len &#8211; length<\/strong><\/p>\n<p>The len function returns the length of a tuple<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3038\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplef.png\" alt=\"\" width=\"545\" height=\"89\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplef.png 545w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/tuplef-300x49.png 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>lists vs tuples<\/h3>\n<p>Seeing the similarities between tuples and lists, one would want to ask when a tuple should be used and when a list should be used. The general consensus is that tuples should be used when you just want a container to store items, specially items which will remain constant and do not need to change. lists can also do the same thing but they will allow the altering of the items within the list. Since lists have more functionality than tuples, tuples are lightweight in comparison and process faster than lists. lists are best used when you to work with a collection of items which need changing in terms of addition, updating and removal of items.<\/p>\n<p>The next collection we will examine are <a href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/10\/python-2-7-fundamentals-collections-sets\/\" target=\"_blank\" rel=\"noopener\">sets<\/a> .<\/p>\n<p><span style=\"border-radius: 2px; text-indent: 20px; width: auto; padding: 0px 4px 0px 0px; text-align: center; font: bold 11px\/20px 'Helvetica Neue',Helvetica,sans-serif; color: #ffffff; background: #bd081c no-repeat scroll 3px 50% \/ 14px 14px; position: absolute; opacity: 1; z-index: 8675309; display: none; cursor: pointer;\">Save<\/span><\/p>\n<p><span style=\"border-radius: 2px; text-indent: 20px; width: auto; padding: 0px 4px 0px 0px; text-align: center; font: bold 11px\/20px 'Helvetica Neue',Helvetica,sans-serif; color: #ffffff; background: #bd081c no-repeat scroll 3px 50% \/ 14px 14px; position: absolute; opacity: 1; z-index: 8675309; display: none; cursor: pointer;\">Save<\/span><\/p>\n<p><span style=\"border-radius: 2px; text-indent: 20px; width: auto; padding: 0px 4px 0px 0px; text-align: center; font: bold 11px\/20px 'Helvetica Neue',Helvetica,sans-serif; color: #ffffff; background: #bd081c no-repeat scroll 3px 50% \/ 14px 14px; position: absolute; opacity: 1; z-index: 8675309; display: none; cursor: pointer;\">Save<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Tuples are very similar to lists, except that they are immutable. In other words, once a tuple is created you cannot modify it in any <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/10\/python-2-7-fundamentals-collections-tuples\/\" title=\"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Tuples\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2934","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2934"}],"version-history":[{"count":9,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2934\/revisions"}],"predecessor-version":[{"id":3054,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2934\/revisions\/3054"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}