473,385 Members | 1,693 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,385 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 2259
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,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.