Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91751
| Date | 2015-06-02 02:10 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Where is 'palindrome' defined? |
| References | <83a279a3-133d-4a50-9af3-054233bcc6af@googlegroups.com> <888b51eb-0ede-4120-a914-aeec45d6366d@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.30.1433207459.13271.python-list@python.org> (permalink) |
On 2015-06-02 01:55, fl wrote:
> On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote:
>> Hi,
>>
>> When I search solution of reverse a string/number, I came across a short
>> function online:
>>
>> >>> def palindrome(num):
>> return str(num) == str(num)[::-1]
>>
>> I thought that it is a general function. And with the following variable:
>>
>> >>> a
>> '1234_5555'
>>
>> >>> parlindrome(a)
>>
>> Traceback (most recent call last):
>> File "<pyshell#126>", line 1, in <module>
>> parlindrome(a)
>> NameError: name 'parlindrome' is not defined
>>
>>
>> Then, I find that parlindrome is a special checking mirrored word.
>> I use Python 2.7.9. Why does the error message show
>>
>> name 'parlindrome' is not defined
>>
>>
>>
>> Thanks,
>
> Thanks, I realize that it was spelled wrong. Now, with the correct spelling
> the result is a 'False'. I have expected it gives reversed string. Is the
> function behaves correct or not?
>
>
It compares 'num' as a string to the reverse of that to test whether
it's a palindrome,
so, yes, it's behaving correctly.
Perhaps its purpose would have been clearer if it had been called
"is_palindrome".
>
>>>> a='1234'
>>>> def palindrome(num):
> return str(num) == str(num)[::-1]
>>>> palindrome(a)
> False
>>>> palindrome("fread")
> False
>
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Where is 'palindrome' defined? fl <rxjwg98@gmail.com> - 2015-05-31 21:46 -0700
Re: Where is 'palindrome' defined? Gary Herron <gherron@digipen.edu> - 2015-05-31 23:55 -0700
Re: Where is 'palindrome' defined? Grant Edwards <invalid@invalid.invalid> - 2015-06-01 15:02 +0000
Re: Where is 'palindrome' defined? Larry Hudson <orgnut@yahoo.com> - 2015-05-31 23:56 -0700
Re: Where is 'palindrome' defined? David Palao <dpalao.python@gmail.com> - 2015-06-01 08:58 +0200
Re: Where is 'palindrome' defined? Marko Rauhamaa <marko@pacujo.net> - 2015-06-01 12:21 +0300
Re: Where is 'palindrome' defined? David Palao <dpalao.python@gmail.com> - 2015-06-01 11:41 +0200
Re: Where is 'palindrome' defined? John Ladasky <john_ladasky@sbcglobal.net> - 2015-06-02 14:14 -0700
Re: Where is 'palindrome' defined? Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 01:30 +0300
Re: Where is 'palindrome' defined? Omar Abou Mrad <omar.aboumrad@gmail.com> - 2015-06-01 10:00 +0300
Re: Where is 'palindrome' defined? Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-01 15:17 +0000
Re: Where is 'palindrome' defined? fl <rxjwg98@gmail.com> - 2015-06-01 17:55 -0700
Re: Where is 'palindrome' defined? sohcahtoa82@gmail.com - 2015-06-01 18:07 -0700
Re: Where is 'palindrome' defined? MRAB <python@mrabarnett.plus.com> - 2015-06-02 02:10 +0100
csiph-web