-1

How python initializes a tuple object ? For example, when I create an object like

>>> object = ('Shark')

It shows type of object as str

>>> type(object)
<type 'str'>

While, if I create an object like

>>> object = ('Shark',)

It displays its type as a tuple

>>> type(object)
<type 'tuple'>

Why so ?? How exactly python creates/initializes a tuple, str objects ?

dotslash
  • 2,041
  • 2
  • 16
  • 15
  • 7
    You basically already have the answer: it's the comma that makes a tuple, not the parentheses. – BrenBarn Oct 18 '13 at 05:55
  • You can read about them more [here](http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) – Matt Oct 18 '13 at 06:00

3 Answers3

3

From docs:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

So, without a trailing , these two statements are equivalent:

>>> x = 1
>>> x
1
>>> x = (1)
>>> x
1

But these are all tuples:

>>> x = 1,       #or (1,)
>>> x
(1,)
>>> x = 1, 2, 3  #or (1, 2, 3)
>>> x
(1, 2, 3)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

An expression enclosed in parens is just that expression (it's like the parens in a math expression, like (2+2)*4).

But, if you add a trailing comma, or if there's more than one element inside the parens separated by commas, it will become a tuple. Also, you will get an empty tuple if you write ().

nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

Anything enclosed inside double-quotes or single-quotes will be treated as a string in Python. There's no distinction between the two-- like in PHP, that variables inside the double-quoted strings will actually be evaluated and single-quotes will display as it is.

Anything enclosed inside an opening parenthesis and closing parenthesis will be treated as tuples. However, if only one object has been placed in the tuple, it'll be treated as the object's type. For instance:

>>> a = (9)
>>> type(a)
<class 'int'>
>>> 
>>> a = ([9])
>>> type(a)
<class 'list'>
>>> 
>>> a = ('hello!')
>>> type(a)
<class 'str'>
>>> 
>>> a = {'a':1, 'b':1}
>>> type(a)
<class 'dict'>
>>> 

Note that the length of the object doesn't matter (the object placed inside of the tuple, not the tuple itself).

Now, if you were to place a comma after the object inside of the tuple, Python will presume that there's another object coming and it'll treat it as if it were a tuple and not give you an error. Also, note that the default object used by Python to store a number of things is also a tuple-- meaning that even if you don't enclose an expression in parenthesis, it'll do it by default for you:

>>> a = 9, 8, 7
>>> a
(9, 8, 7)
>>> type(a)
<class 'tuple'>
>>> 
>>> a = 'Hello', 'Python', 'Typle'
>>> a
('Hello', 'Python', 'Typle')
>>> type(a)
<class 'tuple'>
>>> 
Akshat Tripathi
  • 321
  • 2
  • 12