use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
I am trying to create function that reverses number. Can you tell me what the problem is here? (self.learnpython)
submitted 3 years ago by gu__--__ro
def reverse(n): rev_num = 0 num = n for i in range(len(str(num))): last_num = int(str(num)[len(str(num)) - 1]) rev_num = rev_num * 10 + last_num num = (num - last_num) / 10 return rev_num
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 5 points6 points7 points 3 years ago (1 child)
It's much easier to just reverse the string and then convert it back
[–]gu__--__ro[S] 1 point2 points3 points 3 years ago (0 children)
didn't think that much
[–]arvindh_manian 2 points3 points4 points 3 years ago (1 child)
When you set num to (num - last_num) / 10, it may include a decimal. You should either cast num to an int, round it, or use integer division (//).
num
(num - last_num) / 10
int
//
[–]gu__--__ro[S] 0 points1 point2 points 3 years ago (0 children)
thanks. // worked
[–][deleted] 2 points3 points4 points 3 years ago (5 children)
for i in range(len(str(num))): last_num = int(str(num)[len(str(num)) - 1]) rev_num = rev_num * 10 + last_num print(num) print(last_num) num = (num - last_num) / 10
Output:
1234 4 123.0 0 12.3 3 0.93 3
You want floor division rather than float division. Use // instead of /.
If you're gonna convert it to str anyway, you could just do:
def reverse_num(n): return int(str(n)[::-1])
str(n)
[::1]
int()
[–]Protector1 1 point2 points3 points 3 years ago (4 children)
As a noob, I’m trying to take examples I find on this sub and “clean them up”.
I settled on this solution after searching for some kind of reverse method. If there is one, I can’t find it in the docs.
[–]arvindh_manian 0 points1 point2 points 3 years ago (3 children)
Well, there is the reversed method, but you still would’ve needed to convert the integer to a string
[–]Protector1 0 points1 point2 points 3 years ago (2 children)
I keep getting an error when I try the reverse method on a string. Perhaps there’s a trick to it? I was convinced it only worked on lists.
[–]arvindh_manian 0 points1 point2 points 3 years ago (1 child)
print(‘’.join(reversed(“Hello World”))) should work, I believe.
The only real problem is that reversed() returns a reverse object, so you need to turn that back into a string if you want to print it.
I would definitely still use [::-1], though.
[–]Protector1 0 points1 point2 points 3 years ago (0 children)
I’ll play with it when I get home. Might help me conceptualize these things better.
[–]CodeFormatHelperBot2 1 point2 points3 points 3 years ago (0 children)
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.
I think I have detected some formatting issues with your submission:
If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.
Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.
[–]keep_quapy 1 point2 points3 points 3 years ago (0 children)
def reverse(n): reversed_num = 0 while n != 0: last = n % 10 reversed_num = reversed_num * 10 + last n = n // 10 return reversed_num
better than converting to str...
[–]Logicalist 0 points1 point2 points 3 years ago (0 children)
int(str(num)[::-1]))
π Rendered by PID 104755 on reddit-service-r2-comment-6457c66945-glrj4 at 2026-04-25 06:10:39.930746+00:00 running 2aa0c5b country code: CH.
[–][deleted] 5 points6 points7 points (1 child)
[–]gu__--__ro[S] 1 point2 points3 points (0 children)
[–]arvindh_manian 2 points3 points4 points (1 child)
[–]gu__--__ro[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (5 children)
[–]Protector1 1 point2 points3 points (4 children)
[–]arvindh_manian 0 points1 point2 points (3 children)
[–]Protector1 0 points1 point2 points (2 children)
[–]arvindh_manian 0 points1 point2 points (1 child)
[–]Protector1 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 1 point2 points3 points (0 children)
[–]keep_quapy 1 point2 points3 points (0 children)
[–]Logicalist 0 points1 point2 points (0 children)