{"id":8162,"date":"2024-03-19T06:54:50","date_gmt":"2024-03-19T06:54:50","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8162"},"modified":"2024-03-19T12:46:50","modified_gmt":"2024-03-19T12:46:50","slug":"python-tuples","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-tuples\/","title":{"rendered":"Python &#8211; Tuples"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-python-tuples\">What is Python Tuples?<\/a><\/li><li ><a href=\"#what-are-tuple-items\">What are Tuple Items?<\/a><\/li><li ><a href=\"#what-is-an-ordered-tuple\">What is an Ordered Tuple?<\/a><\/li><li ><a href=\"#example-to-show-that-duple-allows-duplicates\">Example &#8211; To show that duple allows duplicates<\/a><\/li><li ><a href=\"#example-to-determine-the-tuple-length\">Example-To determine the Tuple Length<\/a><\/li><li ><a href=\"#example-to-create-the-tuple-with-one-item\">Example- To create the Tuple With One Item<\/a><\/li><li ><a href=\"#example-to-create-the-data-types-for-tuple-items\">Example &#8211; to create the Data Types for  Tuple items<\/a><\/li><li ><a href=\"#what-is-type\">What is type()<\/a><\/li><li ><a href=\"#what-is-a-tuple-constructor\">What is a Tuple() Constructor?<\/a><\/li><li ><a href=\"#what-are-the-python-collection-data-types\">What are the Python Collection Data Types?<\/a><\/li><li ><a href=\"#python-tuples-fa-qs\">Python &#8211; Tuples-  FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python tuple is a collection of objects and is separated by commas. It will work similarly to the Python list regarding indexing, nested objects, and repetition. Read this article to learn more about Python-Tuples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-tuples\">What is Python Tuples?<\/h2>\n\n\n\n<p>Python tuple will allow you to store multiple items into a single <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-print-a-variable-name-in-c\/\" data-type=\"post\" data-id=\"3299\">variable<\/a>. Whereas, the tuple is considered one of the 4 built-in <a href=\"https:\/\/www.skillvertex.com\/blog\/data-types-in-c\/\" data-type=\"post\" data-id=\"1842\">data types<\/a> in Python. It is a collection that is mainly ordered and unchangeable. So, it will be written with the round brackets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-tuple-items\">What are Tuple Items?<\/h2>\n\n\n\n<p>Tuple items will be ordered, unchangeable, and can have duplicate value. These items are indexed the first item has an index of [0] and the second item will have an index of [1] etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-an-ordered-tuple\">What is an Ordered Tuple?<\/h2>\n\n\n\n<p>An ordered Tuple is referred to as an item that should be in a defined order and that order won&#8217;t be changed. Tuples are also unchangeable, where they can&#8217;t be changed, added, or removed items after the tuple is created.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-show-that-duple-allows-duplicates\">Example &#8211; To show that duple allows duplicates<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import Counter\n\ndef check_duplicates(lst):\n    count = Counter(lst)\n    for item, freq in count.items():\n        if freq &gt; 1:\n            print(f\"Element '{item}' appears {freq} times.\")\n\n# Example list with duplicates\nmy_list = &#91;1, 2, 3, 1, 4, 2, 5, 6, 3, 2]\n\n# Check for duplicates\ncheck_duplicates(my_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Element '1' appears 2 times.\nElement '2' appears 3 times.\nElement '3' appears 2 times.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-determine-the-tuple-length\">Example-To determine the Tuple Length<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def tuple_length(tup):\n    return len(tup)\n\n# Example tuple\nmy_tuple = (1, 2, 3, 4, 5)\n\n# Find the length of the tuple\nlength = tuple_length(my_tuple)\n\n# Print the length of the tuple\nprint(\"Length of the tuple:\", length)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Length of the tuple: 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-create-the-tuple-with-one-item\">Example- To create the Tuple With One Item<\/h2>\n\n\n\n<p>It is required to add the comma after the item to create the Tuple or else, it won&#8217;t be considered as the Tuple.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Tuple with a single item\nmy_tuple = (42,)  # Note the trailing comma after the single item\n\n# Output\nprint(\"Tuple with a single item:\", my_tuple)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tuple with a single item: (42,)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-create-the-data-types-for-tuple-items\">Example &#8211; to create the Data Types for  Tuple items<\/h2>\n\n\n\n<p>Tuple items will be of any data type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define a tuple with items of different data types\nmy_tuple = (\"apple\", 42, 3.14, True, &#91;1, 2, 3])\n\n# Output the data types of tuple items\nfor item in my_tuple:\n    print(f\"Item: {item}, Data Type: {type(item)}\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Item: apple, Data Type: &lt;class 'str'&gt;\nItem: 42, Data Type: &lt;class 'int'&gt;\nItem: 3.14, Data Type: &lt;class 'float'&gt;\nItem: True, Data Type: &lt;class 'bool'&gt;\nItem: &#91;1, 2, 3], Data Type: &lt;class 'list'&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-type\">What is type()<\/h2>\n\n\n\n<p>Tuple will be referred to as the objects with the data type in Python.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;class 'tuple'&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-tuple-constructor\">What is a Tuple() Constructor?<\/h2>\n\n\n\n<p>The Tuple constructor will help you to make the tuple.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using tuple() method to create a tuple\nmy_list = &#91;1, 2, 3, 4, 5]\nmy_tuple = tuple(my_list)\n\n# Output\nprint(\"Tuple created from list:\", my_tuple)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tuple created from list: (1, 2, 3, 4, 5)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-python-collection-data-types\">What are the Python Collection Data Types?<\/h2>\n\n\n\n<p>Python consists of  four  collection data types in Python Programming language:<\/p>\n\n\n\n<p><strong>List<\/strong>: It is referred to as the collection that is ordered and changeable. Hence, it will allow duplicate members.<\/p>\n\n\n\n<p><strong>Tuple<\/strong>: It is one among the data types that are ordered and unchangeable and will offer duplicate members.<\/p>\n\n\n\n<p><strong>Set<\/strong>: It is a collection that is unordered, unchangeable, and unindexed and has no duplicate members.<\/p>\n\n\n\n<p><strong>Dictionary<\/strong>: It refers to the collection that is ordered is changeable and has no duplicate members.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-tuples-fa-qs\">Python &#8211; Tuples-  FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710496437881\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the purpose of tuples in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Tuple will store the multiple items in the single variable.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710496448766\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How are tuples stored in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Tuples will be stored in a single block of memory.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710496457850\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are the two functions of a tuple?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Tuple Functions  includes len(), max(), min(), tuple().<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python tuple is a collection of objects and is separated by commas. It will work similarly to the Python list regarding indexing, nested objects, and repetition. Read this article to learn more about Python-Tuples. What is Python Tuples? Python tuple will allow you to store multiple items into a single variable. Whereas, the tuple is &#8230; <a title=\"Python &#8211; Tuples\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-tuples\/\" aria-label=\"More on Python &#8211; Tuples\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8330,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[980,916,978,979],"class_list":["post-8162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-data-type","tag-python-programming","tag-python-tuple","tag-tuple","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8162"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8162\/revisions"}],"predecessor-version":[{"id":8331,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8162\/revisions\/8331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8330"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}