{"id":1126,"date":"2024-02-24T10:47:27","date_gmt":"2024-02-24T10:47:27","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=1126"},"modified":"2024-02-24T10:47:28","modified_gmt":"2024-02-24T10:47:28","slug":"python-tuples-2","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/24\/python-tuples-2\/","title":{"rendered":"Python\u00a0Tuples"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>mytuple = (\"apple\",&nbsp;\"banana\",&nbsp;\"cherry\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple<\/h2>\n\n\n\n<p>Tuples are used to store multiple items in a single variable.<\/p>\n\n\n\n<p>Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are\u00a0List,\u00a0Set, and\u00a0Dictionary, all with different qualities and usage.<\/p>\n\n\n\n<p>A tuple is a collection which is ordered and&nbsp;<strong>unchangeable<\/strong>.<\/p>\n\n\n\n<p>Tuples are written with round brackets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Create a Tuple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thistuple = (\"apple\",\u00a0\"banana\",\u00a0\"cherry\")<br>print(thistuple)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple Items<\/h2>\n\n\n\n<p>Tuple items are ordered, unchangeable, and allow duplicate values.<\/p>\n\n\n\n<p>Tuple items are indexed, the first item has index&nbsp;<code>[0]<\/code>, the second item has index&nbsp;<code>[1]<\/code>&nbsp;etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Ordered<\/h2>\n\n\n\n<p>When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Unchangeable<\/h2>\n\n\n\n<p>Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Allow Duplicates<\/h2>\n\n\n\n<p>Since tuples are indexed, they can have items with the same value:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Tuples allow duplicate values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thistuple = (\"apple\",\u00a0\"banana\",\u00a0\"cherry\",\u00a0\"apple\",\u00a0\"cherry\")<br>print(thistuple)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple Length<\/h2>\n\n\n\n<p>To determine how many items a tuple has, use the&nbsp;<code>len()<\/code>&nbsp;function:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Print the number of items in the tuple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thistuple = (\"apple\",\u00a0\"banana\",\u00a0\"cherry\")<br>print(len(thistuple))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Create Tuple With One Item<\/h2>\n\n\n\n<p>To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>One item tuple, remember the comma:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thistuple = (\"apple\",)<br>print(type(thistuple))<br><br>#NOT a tuple<br>thistuple = (\"apple\")<br>print(type(thistuple))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple Items &#8211; Data Types<\/h2>\n\n\n\n<p>Tuple items can be of any data type:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>String, int and boolean data types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuple1 = (\"apple\",\u00a0\"banana\",\u00a0\"cherry\")\ntuple2 = (1,\u00a05,\u00a07,\u00a09,\u00a03)\ntuple3 = (True,\u00a0False,\u00a0False)\n\n<\/code><\/pre>\n\n\n\n<p>A tuple can contain different data types:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>A tuple with strings, integers and boolean values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuple1 = (\"abc\",\u00a034,\u00a0True,\u00a040,\u00a0\"male\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">type()<\/h2>\n\n\n\n<p>From Python&#8217;s perspective, tuples are defined as objects with the data type &#8216;tuple&#8217;:<\/p>\n\n\n\n<p>&lt;class &#8216;tuple&#8217;&gt;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>What is the data type of a tuple?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mytuple = (\"apple\",\u00a0\"banana\",\u00a0\"cherry\")<br>print(type(mytuple))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The tuple() Constructor<\/h2>\n\n\n\n<p>It is also possible to use the&nbsp;tuple()&nbsp;constructor to make a tuple.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Using the tuple() method to make a tuple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thistuple = tuple((\"apple\",\u00a0\"banana\",\u00a0\"cherry\"))\u00a0# note the double round-brackets<br>print(thistuple)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Collections (Arrays)<\/h2>\n\n\n\n<p>There are four collection data types in the Python programming language:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/www.w3schools.com\/python\/python_lists.asp\">List<\/a><\/strong>&nbsp;is a collection which is ordered and changeable. Allows duplicate members.<\/li>\n\n\n\n<li><strong>Tuple<\/strong>&nbsp;is a collection which is ordered and unchangeable. Allows duplicate members.<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/www.w3schools.com\/python\/python_sets.asp\">Set<\/a><\/strong>&nbsp;is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/www.w3schools.com\/python\/python_dictionaries.asp\">Dictionary<\/a><\/strong>&nbsp;is a collection which is ordered** and changeable. No duplicate members.<\/li>\n<\/ul>\n\n\n\n<p>*Set&nbsp;<em>items<\/em>&nbsp;are unchangeable, but you can remove and\/or add items whenever you like.<\/p>\n\n\n\n<p>**As of Python version 3.7, dictionaries are&nbsp;<em>ordered<\/em>. In Python 3.6 and earlier, dictionaries are&nbsp;<em>unordered<\/em>.<\/p>\n\n\n\n<p>When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tuple Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are\u00a0List,\u00a0Set, and\u00a0Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and&nbsp;unchangeable. Tuples are written with round brackets. ExampleGet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-1126","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=1126"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1126\/revisions"}],"predecessor-version":[{"id":1127,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1126\/revisions\/1127"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=1126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=1126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=1126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}