Q:

reverse intergers in python

mylist
[1, 2, 3, 4, 5]

mylist[::-1]
[5, 4, 3, 2, 1]
5
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
5
# Python string/number reverser
example_number = 12143
example_string = "Hello there"

def reverse(thing): 
    thing = str(thing) # convert it to a string, just in case it was a number
    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
    # Or alternatively do:
    print(reversed_thing)

# Run it by doing this
reverse(example_number)
reverse(example_string)
3
# The Reverse operation - Python 3:
Some_List = [1, 2, 3, 4, 1, 2, 6]  
Some_List.reverse()  
print(Some_List)
# Result: [6, 2, 1, 4, 3, 2, 1]
0
# Get the number from user manually 
num = int(input("Enter your favourite number: "))
 
# Initiate value to null
test_num = 0
 
# Check using while loop
 
while(num>0):
  #Logic
  remainder = num % 10
  test_num = (test_num * 10) + remainder
  num = num//10
 
# Display the result
print("The reverse number is : {}".format(test_num))
0

New to Communities?

Join the community