{"id":66,"date":"2024-01-25T09:29:24","date_gmt":"2024-01-25T09:29:24","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=66"},"modified":"2024-01-25T09:29:26","modified_gmt":"2024-01-25T09:29:26","slug":"python-tuples","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-tuples\/","title":{"rendered":"Python Tuples"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python tuples and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python tuples<\/h2>\n\n\n\n<p>Sometimes, you want to create a\u00a0list\u00a0of items that cannot be changed throughout the program. Tuples allow you to do that.<\/p>\n\n\n\n<p>A tuple is a list that cannot change. Python refers to a value that cannot change as\u00a0<strong>immutable<\/strong>. So by definition,\u00a0<strong>a tuple is an immutable list.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining a tuple<\/h3>\n\n\n\n<p>A tuple is like a list except that it uses parentheses&nbsp;<code>()<\/code>&nbsp;instead of square brackets&nbsp;<code>[]<\/code>.<\/p>\n\n\n\n<p>The following example defines a tuple called\u00a0<code>rgb<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>rgb = ('red', 'green', 'blue')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Once defining a tuple, you can access an individual element by its index. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>rgb = ('red', 'green', 'blue') print(rgb&#91;0]) print(rgb&#91;1]) print(rgb&#91;2])<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>red green blue<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since a tuple is immutable, you cannot change its elements. The following example attempts to change the first element of the\u00a0<code>rgb<\/code>\u00a0tuple to\u00a0<code>'yellow'<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>rgb = ('red', 'green', 'blue') rgb&#91;0] = 'yellow'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And it results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>TypeError: 'tuple' object does not support item assignment<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Defining a tuple that has one element<\/h3>\n\n\n\n<p>To define a tuple with one element, you need to include a trailing comma after the first element. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = (3,) print(type(numbers))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;class 'tuple'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you exclude the trailing comma, the type of the\u00a0<code>numbers<\/code>\u00a0will be\u00a0<code>int<\/code>\u00a0, which stands for\u00a0integer. And its value is 3. Python won\u2019t create a tuple that includes the number 3:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = (3) print(type(numbers))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;class 'int'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Assigning a tuple<\/h2>\n\n\n\n<p>Even though you can\u2019t change a tuple, you can assign a new tuple to a\u00a0variable\u00a0that\u00a0references\u00a0a tuple. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = ('red', 'green', 'blue') print(colors) colors = ('Cyan', 'Magenta', 'Yellow', 'black') print(colors)<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python tuples and how to use them effectively. Introduction to Python tuples Sometimes, you want to create a\u00a0list\u00a0of items that cannot be changed throughout the program. Tuples allow you to do that. A tuple is a list that cannot change. Python refers to a value that cannot change [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-5-lists"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/66","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=66"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/66\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}