How to pass tuple as argument in Python?
It's because that's not a tuple, it's two arguments to the add
method. If you want to give it one argument which is a tuple, the argument itself has to be (3, 'three')
:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> li = []
>>> li.append(3, 'three')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
>>> li.append( (3,'three') )
>>> li
[(3, 'three')]
>>>
Add more parentheses:
li.append((3, 'three'))
Parentheses with a comma create a tuple, unless it's a list of arguments.
That means:
() # this is a 0-length tuple
(1,) # this is a tuple containing "1"
1, # this is a tuple containing "1"
(1) # this is number one - it's exactly the same as:
1 # also number one
(1,2) # tuple with 2 elements
1,2 # tuple with 2 elements
A similar effect happens with 0-length tuple:
type() # <- missing argument
type(()) # returns <type 'tuple'>