what is the difference between the two kinds of brackets?

N

narutocanada

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:
lst = [10, 20, 30]
print lst[0]
print lst[2]
print lst
lst = (10, 20, 30)
print lst[0]
print lst[2]
print lst

lst = [10, 20, 40, "string", 302.234]
print lst[0:2]
print lst[:3]
print lst[3:]
lst = (10, 20, 40, "string", 302.234)
print lst[0:2]
print lst[:3]
print lst[3:]

10
30
[10, 20, 30]
10
30
(10, 20, 30)
[10, 20]
[10, 20, 40]
['string', 302.23399999999998]
(10, 20)
(10, 20, 40)
('string', 302.23399999999998)

Are these two kinds of brackets mean the same thing in the "list"
context? Thanks.
 
T

Thomas Jollans

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)
l[0] 1
t[0] 1
l[0] = 12
t[0] = 12
Traceback (most recent call last):

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBHGde+JpinDvQhQ0sRAsvhAJ9GAQZjHnLYgPZXy6+oUif9yWoCeACfbQ/+
8tD9sDu3rIP+UsMeXjbryw0=
=IfJB
-----END PGP SIGNATURE-----
 
J

James Stroud

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:
Are these two kinds of brackets mean the same thing in the "list"
context? Thanks.


The square ones designate lists:

http://docs.python.org/tut/node7.html

The rounded ones designate tuples:

http://docs.python.org/tut/node7.html#SECTION007300000000000000000

The short of it is that lists can change and tuples can not.

<commentary>The long of it is that there are deep computer-science
issues that distinguish the two and the differences become more
important the more you know (presumably). However, I have been
programming this language for 5 years, and I still can't figure out the
necessity for having both. I have heard all of the arguments, however
but none are terribly pursuasive.</commentary>

Practically speaking, however, if you don't know which one to use, then
use the square kind.

James


--
James Stroud
UCLA-DOE Institute of Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
S

Steve Lamb

<commentary>The long of it is that there are deep computer-science
issues that distinguish the two and the differences become more
important the more you know (presumably). However, I have been
programming this language for 5 years, and I still can't figure out the
necessity for having both. I have heard all of the arguments, however
but none are terribly pursuasive.</commentary>

The quick answer is that tuples can be indexes into directories while
lists cannot. This doesn't seem like that big of a deal until you realize it
allows you to group related but distict bits of data together to form an index
while retaining their individual identity. A simplistic example would be if
you had a map with x,y coordinates and you wanted to place items on that map
at their location.

grey@mania:~} python
Python 2.4.4 (#2, Aug 16 2007, 02:03:40)
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
x = 1
y = 1
coord = (x, y)
map = {}
map[coord] = "Something at x%s, y%s" % coord
map {(1, 1): 'Something at x1, y1'}
map[(1,1)] 'Something at x1, y1'
if map.has_key(coord):
.... print "Found it!"
.... .... print "Found it!"
....
Found it!.... print "X: %s, Y: %s - %s" % (loc[0], loc[1], map[loc])
....
X: 1, Y: 1 - Something at x1, y1

The lists cannot be indexes to directories because they are mutable.
[1. 1] might not be [1, 1] the next time you look for it. (1, 1) always will.
 
P

Paul Hankin

what is the difference between the two kinds of brackets?
I tried a few examples but I can't make out any real difference:

The main difference in the language between tuples and lists is that
tuples (...) are immutable, and lists [...] are mutable. That means
you should use lists rather than tuples if you need a mutable
structure (for example, if you want to extend it later), and use
tuples when you need an immutable structure (for example, if you're
constructing dict keys).

Perhaps my opinion is coloured by using other languages where the
distinction between list and tuple is stronger, but when I don't need
my data to be immutable or mutable, I use something like these rules
of thumb:
If each index has a particular meaning (for example (x, y) for
describing 2d coordinates), use tuples.
If the data is uniform (ie each element of the list/tuple has the same
meaning - for example, all the filenames in a directory), use lists.
If the data has a fixed length, use tuples.
If the data has an unknown - possibly unbounded - length, use lists.
If everything else is equal, use tuples.

HTH
 
S

Steven D'Aprano

The quick answer is that tuples can be indexes into directories
while lists cannot.


A note on terminology: the things inside curly brackets {} are called
dictionaries, or dicts, not directories. And the things you use to store
data in dictionaries are called keys, not indexes:

# Lists have indexes:
L = ['x', 'y', 'z']
L[0] # returns the item in position 0

# Dicts have keys:
D = {'a': 'x', 2: 'y', 7.8: 'z'}
D['a'] # returns the item with key 'a'
 
S

Steve Lamb

A note on terminology: the things inside curly brackets {} are called
dictionaries, or dicts, not directories. And the things you use to store
data in dictionaries are called keys, not indexes:

Thanks for catching that. Kids, don't late night post while waiting for
the other computer to do its thing.
 
H

Hendrik van Rooyen

Paul Hankin said:
If everything else is equal, use tuples.

Interesting point of view - mine is just the opposite.

I wonder if its the philosophical difference between:

"Anything not expressly allowed is forbidden"

and

"Anything not expressly forbidden is allowed" ?

- Hendrik
 
J

James Stroud

Hendrik said:
Interesting point of view - mine is just the opposite.

I wonder if its the philosophical difference between:

"Anything not expressly allowed is forbidden"

and

"Anything not expressly forbidden is allowed" ?

- Hendrik

The latter is how I interpret any religious moral code--life is a lot
more fun that way. Maybe that percolates to how I use python?

James

--
James Stroud
UCLA-DOE Institute of Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
A

Alex Martelli

James Stroud said:
The latter is how I interpret any religious moral code--life is a lot
more fun that way. Maybe that percolates to how I use python?

FYI, in Security the first approach is also known as "Default Deny", the
second one as "Default Permit".
<http://www.ranum.com/security/computer_security/editorials/dumb/>
explains why "default permit" is THE very dumbest one of the "six
dumbest ideas in computer security" which the article is all about.

But then, the needs of Security are often antithetical to everything
else we wish for -- security and convenience just don't mix:-(


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top