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. 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-----
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
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.
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...
-------------------------------+---------------------------------------------
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
by: b83503104 |
last post by:
Hi,
Can someone tell me the difference between single quote and double
quote?
Thanks
|
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...
|
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...
|
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"
|
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; }
|
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,...
|
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...
|
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"....
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
| |