{"id":24852,"date":"2021-11-02T09:54:44","date_gmt":"2021-11-02T04:24:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=24852"},"modified":"2021-11-02T09:54:44","modified_gmt":"2021-11-02T04:24:44","slug":"python-tuple-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-tuple-function-with-examples\/","title":{"rendered":"Python tuple() Function with Examples"},"content":{"rendered":"
tuple() Function in Python:<\/strong><\/p>\n To create tuples in Python, use the tuple() built-in function.<\/p>\n A tuple is an immutable sequence type in Python. The tuple() construct is one method for generating tuples.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n iterable:<\/strong> This is optional.An iterable (list, range, etc.) or an iterator object<\/p>\n Return Value: <\/strong>This function returns a tuple.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n tuple() Function in Python: To create tuples in Python, use the tuple() built-in function. A tuple is an immutable sequence type in Python. The tuple() construct is one method for generating tuples. Syntax: tuple(iterable) Parameters iterable: This is optional.An iterable (list, range, etc.) or an iterator object If tuple() is not passed an iterable, the …<\/p>\ntuple(iterable)<\/pre>\n
\n
Given list = [-1, 3, 5, 6]\r\nGiven string = \"good\"\r\nGiven tuple = (10, 35, 45)<\/pre>\n
The result after applying tuple() function on the given list = (-1, 3, 5, 6)\r\nThe result after applying tuple() function on the given string = ('g', 'o', 'o', 'd')\r\nThe result after applying tuple() function on the given tuple = (10, 35, 45)<\/pre>\n
Given list = [10, 20, 30, 40]\r\nGiven string = \"hello\"\r\nGiven tuple = (1, 2, 3, 4, 5)<\/pre>\n
The result after applying tuple() function on the given list = (10, 20, 30, 40)\r\nThe result after applying tuple() function on the given string = ('h', 'e', 'l', 'l', 'o')\r\nThe result after applying tuple() function on the given tuple = (1, 2, 3, 4, 5)<\/pre>\n
tuple() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [-1, 3, 5, 6]\r\n# Give the string as static input and store it in another variable.\r\ngvn_str = \"good\"\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (10, 35, 45)\r\n# Pass the given list as an argument to the tuple() function that converts a given\r\n# list to a tuple and returns a tuple.\r\n# Store it in another variable.\r\nrslt1 = tuple(gvn_lst)\r\n# Print the result tuple after applying tuple() function on the given list.\r\nprint(\"The result after applying tuple() function on the given list = \", rslt1)\r\n# similarly do the same for the given string and tuple and print it.\r\nrslt2 = tuple(gvn_str)\r\nprint(\"The result after applying tuple() function on the given string = \", rslt2)\r\nrslt3 = tuple(gvn_tupl)\r\nprint(\"The result after applying tuple() function on the given tuple = \", rslt3)\r\n<\/pre>\n
The result after applying tuple() function on the given list = (-1, 3, 5, 6)\r\nThe result after applying tuple() function on the given string = ('g', 'o', 'o', 'd')\r\nThe result after applying tuple() function on the given tuple = (10, 35, 45)<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the string as user input using the input() function and \r\n# store it in another variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the tuple as user input using tuple(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_tupl = tuple(map(int, input(\r\n 'Enter some random Tuple Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the tuple() function that converts a given\r\n# list to a tuple and returns a tuple.\r\n# Store it in another variable.\r\nrslt1 = tuple(gvn_lst)\r\n# Print the result tuple after applying tuple() function on the given list.\r\nprint(\"The result after applying tuple() function on the given list = \", rslt1)\r\n# similarly do the same for the given string and tuple and print it.\r\nrslt2 = tuple(gvn_str)\r\nprint(\"The result after applying tuple() function on the given string = \", rslt2)\r\nrslt3 = tuple(gvn_tupl)\r\nprint(\"The result after applying tuple() function on the given tuple = \", rslt3)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 23 45 6 1\r\nEnter some random string = hello\r\nEnter some random Tuple Elements separated by spaces = 3 6 7 9\r\nThe result after applying tuple() function on the given list = (23, 45, 6, 1)\r\nThe result after applying tuple() function on the given string = ('h', 'e', 'l', 'l', 'o')\r\nThe result after applying tuple() function on the given tuple = (3, 6, 7, 9)<\/pre>\n","protected":false},"excerpt":{"rendered":"