473,783 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.23399999999 998]
(10, 20)
(10, 20, 40)
('string', 302.23399999999 998)

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

Oct 20 '07 #1
5 2279
On Saturday 20 October 2007, na**********@gm ail.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/5ln3pr5Ock2ma2u 7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6

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

iD8DBQBHGde+Jpi nDvQhQ0sRAsvhAJ 9GAQZjHnLYgPZXy 6+oUif9yWoCeACf bQ/+
8tD9sDu3rIP+UsM eXjbryw0=
=IfJB
-----END PGP SIGNATURE-----

Oct 20 '07 #2
On Oct 20, 11:15 am, "narutocan...@g mail.com" <narutocan...@g mail.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.com wrote:
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
7798
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
19164
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
137
7186
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 pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain 3) when it comes true. - 5) I have developed for many years (>18) in many different environments,...
21
3017
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
669
26220
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
6
2426
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 most of the compilers. But what are the difference for those two syntaxs in the sense for the compiler, if it support both. Is one of them will be processed faster?
13
1993
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
1177
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
2438
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, internet, etc.) and found out about the CLR and how all of the .NET languages access it, the major difference being the syntax and structure of the individual languages. What I'm wondering, since VB.NET is obviously easier to learn/use than C#.NET and...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.