{"id":24568,"date":"2021-10-22T09:00:08","date_gmt":"2021-10-22T03:30:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=24568"},"modified":"2021-11-05T19:38:55","modified_gmt":"2021-11-05T14:08:55","slug":"python-reversed-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-reversed-function-with-examples\/","title":{"rendered":"Python reversed() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for chr() Function<\/a> The reversed() function returns an iterator object that has been reversed.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n sequence:<\/strong> This is required.Any object that is iterable. (list,tuple..etc)<\/p>\n A sequence is an object that implements the sequence protocols __len__() and __getitem__(). For instance, tuple, string, list, range, and so on.<\/p>\n In addition, we can use reversed() in any object that implements __reverse__ ().<\/p>\n Return Value:<\/strong><\/p>\n The reversed() function returns an iterator that iterates through the given sequence in reverse order.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note: <\/strong>After applying the reverse function the result is converted into the list.Hence the result is in the list form.<\/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 Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for chr() Function reversed() Function in Python: The reversed() function returns an iterator object that has been reversed. Syntax: reversed(sequence) Parameters sequence: This is required.Any object that is iterable. (list,tuple..etc) A sequence is an object that implements the sequence protocols __len__() and __getitem__(). For instance, tuple, …<\/p>\n
\nreversed() Function in Python:<\/strong><\/p>\nreversed(sequence)<\/pre>\n
Given string = 'Python-programs'\r\nGiven tuple = (\"h\", \"e\", \"l\", \"l\", \"o\")<\/pre>\n
['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']\r\n['o', 'l', 'l', 'e', 'h']<\/pre>\n
Given list = [6, 7, 8, 9, 10]\r\nGiven range = (2, 12)<\/pre>\n
[10, 9, 8, 7, 6]\r\n[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]<\/pre>\n
Program for reversed() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'Python-programs'\r\n# Get the reverse of a string by applying the reversed() function to the\r\n# given string and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_str)))\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (\"h\", \"e\", \"l\", \"l\", \"o\")\r\n# Get the reverse of a tuple by applying the reversed() function to the\r\n# given tuple and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_tupl)))\r\n# Give the list as static input and store it in another variable.\r\ngvn_lst = [6, 7, 8, 9, 10]\r\n# Get the reverse of a list by applying the reversed() function to the\r\n# given list and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_lst)))\r\n# Give the lower and upper limits as arguments to the range()function and\r\n# store it in a variable.\r\ngvn_rangee = range(2, 12)\r\n# Get the reverse order of the range by applying the reversed() function\r\n# to the given range and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_rangee)))\r\n<\/pre>\n
['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']\r\n['o', 'l', 'l', 'e', 'h']\r\n[10, 9, 8, 7, 6]\r\n[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Get the reverse of a string by applying the reversed() function to the\r\n# given string and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_str)))\r\n# Give the tuple user input using list(),map(),input(),and split() functions and\r\n# store it in another variable.\r\ngvn_tupl=tuple(map(int, input(\r\n 'Enter some random Tuple Elements separated by spaces = ').split()))\r\n# Get the reverse of a tuple by applying the reversed() function to the\r\n# given tuple and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_tupl)))\r\n# Give the list as user input using list(),map(),input(),and split() functions and\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# Get the reverse of a list by applying the reversed() function to the\r\n# given list and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_lst)))\r\n# Give the lower and upper limits as user input using the int(input()) function and\r\n# store them in two separate variables.\r\ngvn_lowrlmt=int(input(\"Enter some random number = \"))\r\ngvn_upprlmt=int(input(\"Enter some random number = \"))\r\n# Pass the given lower and upper as arguments to the range() function and store it in a variable.\r\ngvn_rangee=range(gvn_lowrlmt, gvn_upprlmt)\r\n# Get the numbers in reverse order by applying the reversed() function to the\r\n# given range and convert it into a list using the list() function.\r\n# Print the result.\r\nprint(list(reversed(gvn_rangee)))\r\n<\/pre>\n
Enter some random string = good morning\r\n['g', 'n', 'i', 'n', 'r', 'o', 'm', ' ', 'd', 'o', 'o', 'g']\r\nEnter some random Tuple Elements separated by spaces = 0 4 5 1 2\r\n[2, 1, 5, 4, 0]\r\nEnter some random List Elements separated by spaces = 7 4 2 6\r\n[6, 2, 4, 7]\r\nEnter some random number = 15\r\nEnter some random number = 22\r\n[21, 20, 19, 18, 17, 16, 15]<\/pre>\n
\n