Why is a tuple of tuples of length 1 not actually a tuple unless I add a comma?

前端 未结 3 1602
故里飘歌
故里飘歌 2021-01-22 23:14

Given a tuple of tuples T:

((\'a\', \'b\'))

and an individual tuple t1:

(\'a\',\'b\')
         


        
3条回答
  •  醉话见心
    2021-01-22 23:49

    Doing this (('a', 'b')) does not make a tuple containing a tuple as you can see here:

    >>> T = (('a','b'))
    >>> T
    ('a', 'b')
    

    To make a single element tuple you need to add a trialing comma:

    >>> T = (('a','b'),)
    >>> t1 in T
    True
    >>> T
    (('a', 'b'),)
    

    In fact the parenthesis aren't even a requirement as this will also create a tuple:

    >>> t1 = 'a','b'
    >>> t1
    ('a', 'b')
    >>> 1,2,3,4,5,6
    (1, 2, 3, 4, 5, 6)
    

提交回复
热议问题