{"id":31631,"date":"2024-11-01T09:01:07","date_gmt":"2024-11-01T09:01:07","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31631"},"modified":"2024-11-01T11:48:52","modified_gmt":"2024-11-01T11:48:52","slug":"python-data-type-tuple","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31631\/","title":{"rendered":"python data type: tuple"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Python Tuple Data Type<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, a <strong>Tuple<\/strong> is an <strong>immutable sequence data type<\/strong> that allows you to store multiple values in a single variable. Tuples store values by separating them with commas inside parentheses <code>()<\/code>, and you can mix values of different data types. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_tuple = (1, 2, 3, \"hello\", True, 3.14)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Characteristics of Tuples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Indexing and Slicing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each element of a tuple can be accessed through an index. Python indexing starts at 0, and using negative indexes allows you to access elements from the end.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = (10, 20, 30, 40, 50)\nprint(numbers[0])   # 10\nprint(numbers[-1])  # 50 (last element)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Slicing to get a part of a tuple is also possible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(numbers[1:4])  # (20, 30, 40)\nprint(numbers[:3])   # (10, 20, 30)\nprint(numbers[2:])   # (30, 40, 50)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Immutability of Tuples<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since tuples are <strong>immutable<\/strong>, you cannot add, modify, or delete elements after they are created. This immutability makes tuples useful for safely storing data that should not change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you try to modify an element of a tuple like this, an error will occur:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_tuple = (1, 2, 3)\nmy_tuple[1] = 10  # TypeError: 'tuple' object does not support item assignment<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Examples of Tuple Usage<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tuples are mainly used to store <strong>data that should not change<\/strong>. For instance, tuples can be used to return multiple values from a function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_coordinates():\n    return (37.7749, -122.4194)\n\ncoords = get_coordinates()\nprint(coords)  # (37.7749, -122.4194)\n\nlatitude, longitude = get_coordinates()\nprint(latitude)   # 37.7749\nprint(longitude)  # -122.4194<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, tuples are used when the integrity of the data should be guaranteed. For example, unchanging data such as days of the week or month names can be stored in tuples.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>days_of_week = (\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\")\nprint(days_of_week[0])  # 'Monday'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Difference Between Tuples and Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The main difference between lists and tuples is their <strong>mutability<\/strong>. Lists are mutable, allowing element modification, whereas tuples are immutable and do not allow element modification. Therefore, tuples are suitable when the integrity of the data must be maintained.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, when passing lists and tuples as function arguments, using a tuple ensures that the data cannot be changed within the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def process_data(data):\n    # If data is a tuple, it cannot be modified within the function, ensuring safety\n    print(data)\n\nmy_data = (1, 2, 3)\nprocess_data(my_data)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Tuple Unpacking<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tuples provide a feature called <strong>unpacking<\/strong> that allows you to assign values to multiple variables at once. This can enhance code readability and efficiently assign multiple values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>point = (3, 4)\nx, y = point\nprint(x)  # 3\nprint(y)  # 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unpacking can be conveniently used to assign values from functions that return multiple values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_person_info():\n    return (\"Alice\", 30, \"Engineer\")\n\nname, age, profession = get_person_info()\nprint(name)       # 'Alice'\nprint(age)        # 30\nprint(profession) # 'Engineer'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Nested Tuples<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tuples can contain other tuples, which are called <strong>nested tuples<\/strong>. Nested tuples are used to express complex data structures.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nested_tuple = (1, (2, 3), (4, (5, 6)))\nprint(nested_tuple[1])       # (2, 3)\nprint(nested_tuple[2][1])    # (5, 6)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Other Methods of Tuples<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tuples have fewer methods compared to lists but provide some useful methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tuple.count(x)<\/code>: Counts the number of occurrences of a specific element in the tuple.<\/li>\n\n\n\n<li><code>tuple.index(x)<\/code>: Returns the first position of a specific element in the tuple.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>my_tuple = (1, 2, 3, 1, 2, 1)\nprint(my_tuple.count(1))  # 3\nprint(my_tuple.index(2))  # 1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tuples are a data type that allows storing multiple values in a single variable with immutable characteristics.<\/li>\n\n\n\n<li>You can access tuple elements through indexing and slicing.<\/li>\n\n\n\n<li>Since tuples cannot have their elements modified after being created, they are suitable for storing data that should not change.<\/li>\n\n\n\n<li>Unlike lists, tuples are immutable, making them useful when data integrity needs to be maintained.<\/li>\n\n\n\n<li>Tuple unpacking allows you to assign values to multiple variables at once.<\/li>\n\n\n\n<li>You can express complex data structures using nested tuples.<\/li>\n\n\n\n<li>You can use the <code>count()<\/code> and <code>index()<\/code> methods of tuples to check the number and position of elements.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Tuples are one of the essential data types in Python, proving to be very useful for safely storing and utilizing data that should not change. Make sure to effectively utilize the characteristics of tuples to handle data efficiently!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Tuple Data Type In Python, a Tuple is an immutable sequence data type that allows you to store multiple values in a single variable. Tuples store values by separating them with commas inside parentheses (), and you can mix values of different data types. For example: Characteristics of Tuples 1. Indexing and Slicing Each &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31631\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python data type: tuple&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[98],"tags":[95],"class_list":["post-31631","post","type-post","status-publish","format-standard","hentry","category--en","tag--en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python data type: tuple - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31631\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python data type: tuple - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Python Tuple Data Type In Python, a Tuple is an immutable sequence data type that allows you to store multiple values in a single variable. Tuples store values by separating them with commas inside parentheses (), and you can mix values of different data types. For example: Characteristics of Tuples 1. Indexing and Slicing Each &hellip; \ub354 \ubcf4\uae30 &quot;python data type: tuple&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31631\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:52+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31631\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31631\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python data type: tuple\",\"datePublished\":\"2024-11-01T09:01:07+00:00\",\"dateModified\":\"2024-11-01T11:48:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31631\/\"},\"wordCount\":509,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"keywords\":[\"\ud30c\uc774\uc36c\uac15\uc88c\"],\"articleSection\":[\"Python Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31631\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31631\/\",\"name\":\"python data type: tuple - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:07+00:00\",\"dateModified\":\"2024-11-01T11:48:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31631\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31631\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31631\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python data type: tuple\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"python data type: tuple - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31631\/","og_locale":"ko_KR","og_type":"article","og_title":"python data type: tuple - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Python Tuple Data Type In Python, a Tuple is an immutable sequence data type that allows you to store multiple values in a single variable. Tuples store values by separating them with commas inside parentheses (), and you can mix values of different data types. For example: Characteristics of Tuples 1. Indexing and Slicing Each &hellip; \ub354 \ubcf4\uae30 \"python data type: tuple\"","og_url":"https:\/\/atmokpo.com\/w\/31631\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:07+00:00","article_modified_time":"2024-11-01T11:48:52+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31631\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31631\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python data type: tuple","datePublished":"2024-11-01T09:01:07+00:00","dateModified":"2024-11-01T11:48:52+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31631\/"},"wordCount":509,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"keywords":["\ud30c\uc774\uc36c\uac15\uc88c"],"articleSection":["Python Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31631\/","url":"https:\/\/atmokpo.com\/w\/31631\/","name":"python data type: tuple - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:07+00:00","dateModified":"2024-11-01T11:48:52+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31631\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31631\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31631\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python data type: tuple"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31631"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31631\/revisions"}],"predecessor-version":[{"id":31632,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31631\/revisions\/31632"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}