what is the difference between the two kinds of brackets?

Thomas Jollans thomas at jollans.com
Sat Oct 20 06:25:58 EDT 2007


On Saturday 20 October 2007, narutocanada at gmail.com wrote:
> hi
>
> what is the difference between the two kinds of brackets?
> I tried a few examples but I can't make out any real difference:

Lists are mutable, tuples aren't:

Python 2.4.4 (#2, Aug 16 2007, 00:34:54)
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l = [1,2,3]
>>> t = (1,2,3)
>>> type(l)
<type 'list'>
>>> type(t)
<type 'tuple'>
>>> l[0]
1
>>> t[0]
1
>>> l[0] = 12
>>> t[0] = 12
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
>>>

Also, parentheses can be skipped (as in t = 1,2,3), the comma is the maker for 
a tuple (exception: empty tuple == (), but one-element-tuple == ("foo",))


-- 
      Regards,                       Thomas Jollans
GPG key: 0xF421434B may be found on various keyservers, eg pgp.mit.edu
Hacker key <http://hackerkey.com/>:
v4sw6+8Yhw4/5ln3pr5Ock2ma2u7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/python-list/attachments/20071020/f6d11b49/attachment.sig>


More information about the Python-list mailing list