{"id":124,"date":"2023-09-11T13:52:18","date_gmt":"2023-09-11T12:52:18","guid":{"rendered":"https:\/\/onlinecodinghelp.com\/?p=124"},"modified":"2023-09-11T14:09:17","modified_gmt":"2023-09-11T13:09:17","slug":"python-tuple-method","status":"publish","type":"post","link":"https:\/\/onlinecodinghelp.com\/index.php\/2023\/09\/11\/python-tuple-method\/","title":{"rendered":"Python tuple method"},"content":{"rendered":"\n<p>Python tuple method are ordered collections in Python that are similar to lists but with one key difference: they are immutable, meaning their elements cannot be modified after creation. While tuples don&#8217;t have as many built-in methods as lists, they do have some useful methods and operations that you can use. Here are some commonly used methods for working with tuples:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Copy-of-my_list-12345.png\" alt=\"Python tuple method\" class=\"wp-image-126\" style=\"width:840px;height:409px\" width=\"840\" height=\"409\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Copy-of-my_list-12345.png 1024w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Copy-of-my_list-12345-300x146.png 300w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Copy-of-my_list-12345-768x375.png 768w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li ><a href=\"#count\">count() <\/a><\/li><li ><a href=\"#index\">index()<\/a><\/li><li ><a href=\"#len\">len()<\/a><\/li><li ><a href=\"#max-and-min\">max() and min()<\/a><\/li><li ><a href=\"#sorted\">sorted()<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"count\">count() <\/h2>\n\n\n\n<p>This method is used to count the number of occurrences of a specific element within a tuple<\/p>\n\n\n\n<p>my_tuple = (1, 2, 2, 3, 4, 2, 5)<br>count_of_2 = my_tuple.count(2)<br>print(count_of_2) # Output: 3<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"index\">index()<\/h2>\n\n\n\n<p>The <code>index()<\/code> method is used to find the index (position) of the first occurrence of a specified element<\/p>\n\n\n\n<p>my_tuple = (10, 20, 30, 40, 50) <\/p>\n\n\n\n<p>index_of_30 = my_tuple.index(30) <\/p>\n\n\n\n<p>print(index_of_30)  # Output: 2 (because 30 is at index 2)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"len\">len()<\/h2>\n\n\n\n<p>Although not a method specific to tuples (it works with any sequence), <code>len()<\/code> can be used to determine the length (number of elements) in a tuple<\/p>\n\n\n\n<p>my_tuple = (1, 2, 3, 4, 5) <\/p>\n\n\n\n<p>length = len(my_tuple) <\/p>\n\n\n\n<p>print(length) # Output: 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"max-and-min\"><strong>max()<\/strong> and <strong>min()<\/strong><\/h2>\n\n\n\n<p><strong>max()<\/strong> and <strong>min()<\/strong>: You can use these functions to find the maximum and minimum elements in a tuple<\/p>\n\n\n\n<p>my_tuple = (10, 20, 5, 45, 30) <\/p>\n\n\n\n<p>maximum = max(my_tuple) <\/p>\n\n\n\n<p>minimum = min(my_tuple) <\/p>\n\n\n\n<p>print(maximum, minimum) # Output: 45 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sorted\">sorted()<\/h2>\n\n\n\n<p>my_tuple = (3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5) <\/p>\n\n\n\n<p>sorted_list = sorted(my_tuple) <\/p>\n\n\n\n<p>print(sorted_list) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]<\/p>\n\n\n\n<p>Remember that since tuples are immutable, these methods do not modify the original tuple but return new values or data as appropriate<\/p>\n\n\n\n<p><a href=\"https:\/\/onlinecodinghelp.com\/index.php\/2023\/09\/06\/what-is-list-in-python\/\">What is a List in Python?<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python tuple method are ordered collections in Python that are similar to lists but with&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"_links":{"self":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/124"}],"collection":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":6,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}