/    /  Python – Reversed

Python Reversed

In python, to reverse the order of a sequence of different types of data, we can apply reversed() function. To get the output we use list form of reversed function.

Syntax:

reversed(sequence)


Python Code:

#reversing the list 
number=[2,20,14,27,16,35,1,19,54,75]
num=reversed(number)
print(list(num))
#reversing the string
string="programming"
char=reversed(string)
print(list(char))
#reversing the tuple
tup=("v","a","p","f","n","k","c","m","s")
tupp=reversed(tup)
print(list(tupp))
#revesing the range of numbers
ran = range(5, 15) 
rev_ran=reversed(ran)
print(list(rev_ran))


Output:

[75, 54, 19, 1, 35, 16, 27, 14, 20, 2]

[‘g’, ‘n’, ‘i’, ‘m’, ‘m’, ‘a’, ‘r’, ‘g’, ‘o’, ‘r’, ‘p’]

[‘s’, ‘m’, ‘c’, ‘k’, ‘n’, ‘f’, ‘p’, ‘a’, ‘v’]

[14, 13, 12, 11, 10, 9, 8, 7, 6, 5]