{"id":3515,"date":"2023-10-22T15:54:18","date_gmt":"2023-10-22T10:24:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=3515"},"modified":"2023-11-10T11:54:30","modified_gmt":"2023-11-10T06:24:30","slug":"python-programming-tuple","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-tuple\/","title":{"rendered":"Python Programming – Tuple"},"content":{"rendered":"

In this Page, We are Providing Python Programming – Tuple. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Python Programming – Tuple<\/h2>\n

Tuple<\/strong><\/p>\n

There is also another sequence type- tuple. Tuples is a sequence just like list. The differences are that tuple cannot be changed i.e. tuple is immutable, while list is mutable, and tuple use parentheses, while list use square brackets. Tuple items need not to be of same data type. Also, as mentioned previously, Python allow adding a trailing comma after last item of tuple.<\/p>\n

>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a\r\n( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 , )\r\n>>> a\r\n( ' spam ' , ' eggs ' , 100 , 1234 )<\/pre>\n

The above expressions are examples of a “tuple packing” operation i.e. the values ‘ spam ‘, ‘ eggs ‘, 10 0, and 123 4 are packed together in a tuple. The reverse operation is also possible, for example:<\/p>\n

>>> a1 , a2 , a3 , a4=a \r\n>>> a1\r\n' spam '\r\n>>> a2 ' eggs '\r\n>>> a3 100\r\n>>> a4 1234<\/pre>\n

This is called “sequence unpacking”, and works for any sequence on the right-hand side. Sequence unpacking requires the group of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignments are really just a combination of tuple packing and sequence unpacking.<\/p>\n

>>> a , b=10 , 20 \r\n>>> a \r\n10\r\n>>> b \r\n20<\/pre>\n

Tuple creation<\/strong><\/p>\n

Tuple can be created in many ways.<\/p>\n

Using parenthesis<\/strong><\/p>\n

Creating a tuple is as simple as grouping various comma-separated-values, and optionally these comma-separated values between parentheses.<\/p>\n

>>> a= ( ' spam ' eggs ' 100 , 1234 )\r\n>>> a\r\n( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a\r\n( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a= ' spam ' , ' eggs ' , 100 , 1234 \r\n>>> a\r\n( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a= ' spam ' , ' eggs ' , 100 , 1234 ,\r\n>>> a\r\n( ' spam ' , ' eggs ' , 100 , 1234 )<\/pre>\n

A tuple with one item is created by a comma after the item (it is not necessary to enclose a single item in parentheses).<\/p>\n

>>> a= ( 10 )\r\n>>> a \r\n10\r\n>>> type ( a )\r\n<type ' int ' >\r\n>>> b= ( 10 ,)\r\n>>> b\r\n( 10 , )\r\n>>> type ( b )\r\n<type ' tuple ' >\r\n>>> c=10,\r\n>>> c\r\n( 10 , )\r\n>>> type ( c )\r\n<type ' tuple ' ><\/pre>\n

It is also possible to create an empty tuple by assigning parenthesis to a variable.<\/p>\n

>>> a= ( ) \r\n>>> a\r\n( )<\/pre>\n

Using other tuples<\/strong><\/p>\n

A tuple can also be created by copying a tuple or slicing a tuple.<\/p>\n

>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> b=a [ : ]\r\n>>> b\r\n( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> c=a [ 1 : 3 ]\r\n>>> c\r\n( ' eggs ' , 100 )<\/pre>\n

Using built-in function<\/strong><\/p>\n

Tuple can also be created using the built-in function tuple ( ). The tuple ( [iterable] ) function return a tuple whose items are the same and in the same order as items of the iterable. The iterable may be a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. If no argument is given, an empty tuple is returned.<\/p>\n

>>> tuple ( [ ' apple ' , ' pineapple ' , ' banana ' ] )\r\n( ' apple ' , ' pineapple ' , ' banana ' )\r\n>>> tuple ( ' apple ' )\r\n( ' a ' , ' p ' , \u2019 p ' , \u2019 1 \u2019 , \u2019 e \u2019 )\r\n>>> tuple ( [ ' apple ' ] ) \r\n( ' apple ' , )<\/pre>\n

Accessing tuple elements<\/strong><\/p>\n

The tuple index starts with 0, and to access values in a tuple, use the square brackets with the index or indices to obtain a slice of the tuple.<\/p>\n

>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )\r\n>>> a[ 0 ]\r\n' spam '\r\n>>> a [ 1 : 3 ]\r\n( ' eggs ' , 100 )<\/pre>\n

Update tuple<\/strong><\/p>\n

Tuple is immutable, so changing element value or adding new elements is not possible. But one can take portions of existing tuples to create a new tuples.<\/p>\n

>>> tuple1= ( 1 , 2 , 3 )\r\n>>> tuple2=( ' a ' , ' b ' , ' c ' )\r\n>>> tuple3=tuple1+tuple2 \r\n>>> tuple3\r\n( 1 , 2 , 3 , ' a ' , ' b ' , ' c ' )\r\n>>> tuple3=tuple1 [ 0 : 2 ]+tuple2 [ 1 : 3 ] \r\n>>> tuple3\r\n( 1 , 2 , ' b ' , ' c ' )<\/pre>\n

It is also possible to create tuple which contain mutable objects, such as lists.<\/p>\n

>>> a= [ 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ]\r\n>>> a\r\n( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ] )\r\n>>> a [ 0 ] [ 1 ]=200\r\n>>> a \r\n( [ 1 , 200 , 3 ] , [ 4 , 5 , 6 , 7 ] )<\/pre>\n

Deleting tuple<\/strong><\/p>\n

Removing individual tuple elements is not possible. To explicitly remove an entire tuple, just use the del statement.<\/p>\n

>>> a= ( ' spam ' ' eggs ' , 100 , 1234 )\r\n>>> del a<\/pre>\n

Swapping tuples<\/strong><\/p>\n

There might be a scenario in which multiple tuples needs to be swapped among themselves. This is can done easily using multiple assignments expression.<\/p>\n

>>> a= ( 10 , 20 , 30 )\r\n>>> b= ( 40 , 50 , 60 )\r\n>> c= ( 70 , 80 , 90 )\r\n>>> a , b , c=c , a , b \r\n>>> a\r\n( 70 , 80 , 90 )\r\n>>> b\r\n( 10 , 20 , 30 )\r\n>>> c\r\n( 40 , 50 , 60 )<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming – Tuple. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming – Tuple Tuple There is also another sequence type- tuple. Tuples is a sequence just like list. The differences are that tuple cannot be changed i.e. tuple is immutable, while list …<\/p>\n

Python Programming – Tuple<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming - Tuple - Python Programs<\/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:\/\/python-programs.com\/python-programming-tuple\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Programming - Tuple - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming – Tuple. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming – Tuple Tuple There is also another sequence type- tuple. Tuples is a sequence just like list. The differences are that tuple cannot be changed i.e. tuple is immutable, while list … Python Programming – Tuple Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-tuple\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-22T10:24:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:24:30+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-programming-tuple\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-tuple\/\",\"name\":\"Python Programming - Tuple - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-22T10:24:18+00:00\",\"dateModified\":\"2023-11-10T06:24:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-tuple\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-tuple\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-tuple\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming – Tuple\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-tuple\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-tuple\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming – Tuple\",\"datePublished\":\"2023-10-22T10:24:18+00:00\",\"dateModified\":\"2023-11-10T06:24:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-tuple\/#webpage\"},\"wordCount\":458,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-programming-tuple\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Programming - Tuple - Python Programs","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:\/\/python-programs.com\/python-programming-tuple\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming - Tuple - Python Programs","og_description":"In this Page, We are Providing Python Programming – Tuple. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming – Tuple Tuple There is also another sequence type- tuple. Tuples is a sequence just like list. The differences are that tuple cannot be changed i.e. tuple is immutable, while list … Python Programming – Tuple Read More »","og_url":"https:\/\/python-programs.com\/python-programming-tuple\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-22T10:24:18+00:00","article_modified_time":"2023-11-10T06:24:30+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-programming-tuple\/#webpage","url":"https:\/\/python-programs.com\/python-programming-tuple\/","name":"Python Programming - Tuple - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-22T10:24:18+00:00","dateModified":"2023-11-10T06:24:30+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-tuple\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-tuple\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-tuple\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming – Tuple"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-tuple\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-tuple\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming – Tuple","datePublished":"2023-10-22T10:24:18+00:00","dateModified":"2023-11-10T06:24:30+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-tuple\/#webpage"},"wordCount":458,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-programming-tuple\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3515"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3515"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3515\/revisions"}],"predecessor-version":[{"id":3519,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3515\/revisions\/3519"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}