{"id":4003,"date":"2021-04-27T18:59:35","date_gmt":"2021-04-27T13:29:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=4003"},"modified":"2021-11-22T18:43:04","modified_gmt":"2021-11-22T13:13:04","slug":"python-tuple-different-ways-to-create-a-tuple-and-iterate-over-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-tuple-different-ways-to-create-a-tuple-and-iterate-over-it\/","title":{"rendered":"Python Tuple: Different ways to Create a Tuple and Iterate over it"},"content":{"rendered":"
Tuples are a type of variable that allows you to store multiple items in a single variable. Tuple is one of four built-in data types in Python that are used to store data collections. The other three are List, Set, and Dictionary, all of which have different qualities and applications. A tuple is a collection that is both ordered and immutable.<\/p>\n
In this post we are going to discuss different ways to create the tuple and traverse through it.<\/p>\n
We can make a tuple of different elements by separating them with \u2018,’ and wrapping them in braces i.e ( ).<\/p>\n
Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can also create a tuple from unpacked elements i.e without any braces just elements separated by comma.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can create tuple directly using () symbol.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can convert list to tuple by using the syntax tuple(list_name).<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can iterate over the tuple using for loop.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Tuples are a type of variable that allows you to store multiple items in a single variable. Tuple is one of four built-in data types in Python that are used to store data collections. The other three are List, Set, and Dictionary, all of which have different qualities and applications. A tuple is a collection …<\/p>\n# Tuple\r\nnewtuple = ('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks')\r\n# print tuple\r\nprint(newtuple)\r\n<\/pre>\n
('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks')<\/pre>\n
3)Create a tuple out of unpacked elements<\/h3>\n
# Tuple\r\nnewtuple = 'hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks'\r\n# print tuple\r\nprint(newtuple)\r\n<\/pre>\n
('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks')<\/pre>\n
4)Create empty tuple<\/h3>\n
# creating new tuple\r\nnewtuple = ()\r\n# print tuple\r\nprint(newtuple)\r\n<\/pre>\n
()<\/pre>\n
5)Convert list to tuple<\/h3>\n
# given list\r\ngivenlist = [100, 'hello', 4.5, 'BTechGeeks']\r\n# converting this list to tuple\r\nnewtuple = tuple(givenlist)\r\n# print new tuple\r\nprint(newtuple)\r\n<\/pre>\n
(100, 'hello', 4.5, 'BTechGeeks')<\/pre>\n
6)Traverse the tuple<\/h3>\n
# Tuple\r\nnewtuple = ('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks')\r\n# Traverse the tuple\r\nfor i in newtuple:\r\n print(i)\r\n<\/pre>\n
hello\r\n3.5\r\nthis\r\n200\r\nis\r\nTrue\r\nBTechGeeks<\/pre>\n
\n