473,289 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

what is the difference between the two kinds of brackets?

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.

Oct 20 '07 #1
5 2255
On Saturday 20 October 2007, na**********@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

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

iD8DBQBHGde+JpinDvQhQ0sRAsvhAJ9GAQZjHnLYgPZXy6+oUi f9yWoCeACfbQ/+
8tD9sDu3rIP+UsMeXjbryw0=
=IfJB
-----END PGP SIGNATURE-----

Oct 20 '07 #2
On Oct 20, 11:15 am, "narutocan...@gmail.com" <narutocan...@gmail.com>
wrote:
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
--
Paul Hankin

Oct 20 '07 #3
On Sat, 20 Oct 2007 12:43:31 +0000, Steve Lamb wrote:
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'

--
Steven.
Oct 21 '07 #4
On 2007-10-21, Steven D'Aprano <st***@REMOVE-THIS-cybersource.com.auwrote:
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.

--
Steve C. Lamb | But who decides what they dream?
PGP Key: 1FC01004 | And dream I do...
-------------------------------+---------------------------------------------

Oct 21 '07 #5
"Paul Hankin" <pa....mail.comwrote:
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

Oct 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Naresh Agarwal | last post by:
Hi What are the different kinds of JVMs exist and in what respect do they differ. What is the difference between client, server, classic and hotspot jvms? thanks, Naresh
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
21
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: cdrsir | last post by:
we can use 1) // my comments a 2) /* my comments b */ when we want to add some comments. I know some compilers just support the 2nd syntax, but normally both of these 2 syntaxs are supported...
13
by: Herman Jones | last post by:
I found this statement in some sample code. It seems to be syntactically correct. What do the brackets around "String" mean? Dim sWork As = "Some number of characters"
6
by: basheer.md | last post by:
Hi all, What is the difference in both types of intializations?. Class Abc { int x,y,z; public: Abc() { x=0, y=0, z=0; }
49
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.