Python Forum
Program to check whether a number is palindrome or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program to check whether a number is palindrome or not
#14
(Sep-10-2023, 11:13 PM)Skaperen Wrote:
(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?
which aspect of this are you asking?

1. why assign an extra variable y?

2. what does x[::-1] mean?

i would have written it with one statement instead of two:
x = '1234321'
if x == x[::-1]:
    print('The number is a palindrome')
else:
    print('Number is not palindrome')
is that what you were wondering about?

but this has no loops, as your assignment requires. to do it with at least one loop, i would compare the individual characters, one by one, over both ends of the string, until everything has been compared. if any comparisons are unequal then it is not a palindrome. you need to known the length of the string to do this right. i assume you have learned how to get that. you want to have a loop that has two index variables, one for the front, and the other for the back. you compare the front one character to the back one character in the string. i assume know how to do that. if they are unequal, the loop needs to end, right? but if they are equal then it goes on. where does it go and and how does it get there?

I was asking what x[::-1] is. And they helped me figure it out.

Yes that is exactly the way I tried doing it but I got an error. Then I got to know that I use an outdated version of python

When I downloaded the new one, my code worked properly

Thank you!
Reply


Messages In This Thread
RE: Program to check whether a number is palindrome or not - by PythonBoy - Sep-11-2023, 03:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,513 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,756 Feb-24-2022, 10:40 AM
Last Post: perfringo
  Palindrome checker case sensive edwdas 3 2,731 Nov-07-2019, 05:57 PM
Last Post: nilamo
  Program that displays the number with the greatest amount of factors ilusmd 3 2,892 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Palindrome program - I can't figure it out. Gabar112 3 3,633 Feb-20-2018, 07:03 PM
Last Post: Larz60+
  Palindrome.strip pirts.emordnilaP RodNintendeaux 4 3,975 Oct-08-2017, 02:30 AM
Last Post: RodNintendeaux
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,999 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020