python program to reverse a number code example
Example 1: how to reverse a list in python
mylist
[1, 2, 3, 4, 5]
mylist[::-1]
[5, 4, 3, 2, 1]
Example 2: reverse a number in python
example_number = 12143
example_string = "Hello there"
def reverse(thing):
thing = str(thing)
list_of_chars = [char for char in thing]
reversed_list_of_chars = []
x = -1
for char in list_of_chars:
reversed_list_of_chars.append(list_of_chars[x])
x += -1
reversed_thing = ''.join(reversed_list_of_chars)
return reversed_thing
print(reversed_thing)
reverse(example_number)
reverse(example_string)
Example 3: reverse intergers in python
num = int(input("Enter your favourite number: "))
test_num = 0
while(num>0):
remainder = num % 10
test_num = (test_num * 10) + remainder
num = num//10
print("The reverse number is : {}".format(test_num))