one-element tuples [Was: Most probably a stupid question, but I still want to ask]
Chris Angelico
rosuav at gmail.com
Sun Apr 10 20:20:32 EDT 2016
On Mon, Apr 11, 2016 at 10:13 AM, Fillmore <fillmore_remove at hotmail.com> wrote:
> Sorry guys. It was not my intention to piss off anyone...just trying to
> understand how the languare works
>
> I guess that the answer to my question is: there is no such thing as a
> one-element tuple,
> and Python will automatically convert a one-element tuple to a string...
> hence the
> behavior I observed is explained...
>
>>>> a = ('hello','bonjour')
>>>> b = ('hello')
>>>> b
> 'hello'
>>>> a
> ('hello', 'bonjour')
>>>>
>
>
> Did I get this right this time?
Okay, now you're asking a question in a reasonable way, so we can answer :)
The thing you're confused at is that it's not the parentheses that
create a tuple. Parentheses merely group.
>>> 'hello', 'bonjour'
('hello', 'bonjour')
>>> 'hello',
('hello',)
One-element tuples are perfectly possible, but you MUST have a comma,
so you have one at the end. The trailing comma is perfectly legal (and
ignored) on larger tuples.
>>> 'hello', 'bonjour',
('hello', 'bonjour')
ChrisA
More information about the Python-list
mailing list