Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37909 > unrolled thread
| Started by | "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> |
|---|---|
| First post | 2013-01-29 18:55 -0800 |
| Last post | 2013-02-03 18:21 +1100 |
| Articles | 13 — 8 participants |
Back to article view | Back to comp.lang.python
Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 18:55 -0800
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 14:11 +1100
Re: Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 19:42 -0800
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 14:51 +1100
Re: Please provide a better explanation of tuples and dictionaries Mitya Sirenef <msirenef@lightbird.net> - 2013-01-29 22:51 -0500
Re: Please provide a better explanation of tuples and dictionaries John Gordon <gordon@panix.com> - 2013-01-30 05:15 +0000
Re: Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 22:14 -0800
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 17:25 +1100
Re: Please provide a better explanation of tuples and dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-30 07:12 +0000
Re: Please provide a better explanation of tuples and dictionaries rusi <rustompmody@gmail.com> - 2013-01-30 00:14 -0800
Re: Please provide a better explanation of tuples and dictionaries Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-02 21:20 -0800
Re: Please provide a better explanation of tuples and dictionaries Michael Torrie <torriem@gmail.com> - 2013-02-03 00:14 -0700
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-02-03 18:21 +1100
| From | "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> |
|---|---|
| Date | 2013-01-29 18:55 -0800 |
| Subject | Please provide a better explanation of tuples and dictionaries |
| Message-ID | <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d@o1.com> |
Hi all, I have recently started learning Python (2.7.3) but need a better explanation of how to use tuples and dictionaries. I am currently using "Learning Python" by Mark Lutz and David Ascher, published by O'Reilly (ISBN 1-56592-464-9)--but I find the explanations insufficient and the number of examples to be sparse. I do understand some ANSI C programming in addition to Python (and the book often wanders off into a comparison of C and Python in its numerous footnotes), but I need a better real-world example of how tuples and dictionaries are being used in actual Python code. Any recommendations of a better book that doesn't try to write such compact and clever code for a learning book? Or, can an anyone provide an example of more than a three-line example of a tuple or dictionary? The purpose of my learning Python in this case is not for enterprise level or web-based application level testing at this point. I initially intend to use it for Software QA Test Automation purposes. Thanks in advance for any replies.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-30 14:11 +1100 |
| Message-ID | <mailman.1197.1359515470.2939.python-list@python.org> |
| In reply to | #37909 |
On Wed, Jan 30, 2013 at 1:55 PM, Daniel W. Rouse Jr. <dwrousejr@nethere.comnospam> wrote: > I am currently using "Learning Python" by Mark Lutz and David Ascher, > published by O'Reilly (ISBN 1-56592-464-9)--but I find the explanations > insufficient and the number of examples to be sparse. I do understand some > ANSI C programming in addition to Python (and the book often wanders off > into a comparison of C and Python in its numerous footnotes), but I need a > better real-world example of how tuples and dictionaries are being used in > actual Python code. Have you checked out the online documentation at http://docs.python.org/ ? That might have what you're looking for. By the way, you may want to consider learning and using Python 3.3 instead of the older branch 2.7; new features are only being added to the 3.x branch now, with 2.7 getting bugfixes and such for a couple of years, but ultimately it's not going anywhere. Obviously if you're supporting existing code, you'll need to learn the language that it was written in, but if this is all new code, go with the recent version. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> |
|---|---|
| Date | 2013-01-29 19:42 -0800 |
| Message-ID | <YfmdnX6ZtOgmC5XMnZ2dnUVZ_v2dnZ2d@o1.com> |
| In reply to | #37911 |
"Chris Angelico" <rosuav@gmail.com> wrote in message news:mailman.1197.1359515470.2939.python-list@python.org... > On Wed, Jan 30, 2013 at 1:55 PM, Daniel W. Rouse Jr. > <dwrousejr@nethere.comnospam> wrote: >> I am currently using "Learning Python" by Mark Lutz and David Ascher, >> published by O'Reilly (ISBN 1-56592-464-9)--but I find the explanations >> insufficient and the number of examples to be sparse. I do understand >> some >> ANSI C programming in addition to Python (and the book often wanders off >> into a comparison of C and Python in its numerous footnotes), but I need >> a >> better real-world example of how tuples and dictionaries are being used >> in >> actual Python code. > > Have you checked out the online documentation at > http://docs.python.org/ ? That might have what you're looking for. > I'll check the online documentation but I was really seeking a book recommendation or other offline resource. I am not always online, and often times when I code I prefer local machine documentation or a book. I do also have the .chm format help file in the Windows version of Python. > By the way, you may want to consider learning and using Python 3.3 > instead of the older branch 2.7; new features are only being added to > the 3.x branch now, with 2.7 getting bugfixes and such for a couple of > years, but ultimately it's not going anywhere. Obviously if you're > supporting existing code, you'll need to learn the language that it > was written in, but if this is all new code, go with the recent > version. > Honestly, I don't know what code is being supported. I've just seen enough test automation requirements calling for Python (in addition to C# and perl) in some of the latest job listings that I figured I better get some working knowledge of Python to avoid becoming obsolete should I ever need to find another job.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-30 14:51 +1100 |
| Message-ID | <mailman.1198.1359517877.2939.python-list@python.org> |
| In reply to | #37913 |
On Wed, Jan 30, 2013 at 2:42 PM, Daniel W. Rouse Jr. <dwrousejr@nethere.comnospam> wrote: > "Chris Angelico" <rosuav@gmail.com> wrote in message > news:mailman.1197.1359515470.2939.python-list@python.org... >> Have you checked out the online documentation at >> http://docs.python.org/ ? That might have what you're looking for. >> > I'll check the online documentation but I was really seeking a book > recommendation or other offline resource. I am not always online, and often > times when I code I prefer local machine documentation or a book. I do also > have the .chm format help file in the Windows version of Python. Ah. I think the tutorial's in the chm file, but I'm not certain. But for actual books, I can't point to any; I learned from online info only, never actually sought a book (in fact, the last time I used dead-tree reference books was for C and C++). Sorry! >> By the way, you may want to consider learning and using Python 3.3 >> instead of the older branch 2.7... >> > Honestly, I don't know what code is being supported. I've just seen enough > test automation requirements calling for Python (in addition to C# and perl) > in some of the latest job listings that I figured I better get some working > knowledge of Python to avoid becoming obsolete should I ever need to find > another job. A fair point. In that case, it's probably worth learning both; they're very similar. Learn either one first, then master the differences. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-01-29 22:51 -0500 |
| Message-ID | <mailman.1199.1359517908.2939.python-list@python.org> |
| In reply to | #37909 |
On 01/29/2013 09:55 PM, Daniel W. Rouse Jr. wrote: > Hi all, > > I have recently started learning Python (2.7.3) but need a better explanation of how to use tuples and dictionaries. > > I am currently using "Learning Python" by Mark Lutz and David Ascher, published by O'Reilly (ISBN 1-56592-464-9)--but I find the explanations insufficient and the number of examples to be sparse. I do understand some ANSI C programming in addition to Python (and the book often wanders off into a comparison of C and Python in its numerous footnotes), but I need a better real-world example of how tuples and dictionaries are being used in actual Python code. > > Any recommendations of a better book that doesn't try to write such compact and clever code for a learning book? Or, can an anyone provide an example of more than a three-line example of a tuple or dictionary? > > The purpose of my learning Python in this case is not for enterprise level or web-based application level testing at this point. I initially intend to use it for Software QA Test Automation purposes. > > Thanks in advance for any replies. It's not finished yet, but you may find my text-movie tutorial on dicts useful: http://lightbird.net/larks/tmovies/dicts.html -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ Idleness is the mother of psychology. Friedrich Nietzsche
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-01-30 05:15 +0000 |
| Message-ID | <keaa9v$1ru$1@reader1.panix.com> |
| In reply to | #37909 |
In <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d@o1.com> "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> writes:
> I have recently started learning Python (2.7.3) but need a better
> explanation of how to use tuples and dictionaries.
A tuple is a linear sequence of items, accessed via subscripts that start
at zero.
Tuples are read-only; items cannot be added, removed, nor replaced.
Items in a tuple need not be the same type.
Example:
>>> my_tuple = (1, 5, 'hello', 9.9999)
>>> print my_tuple[0]
1
>>> print my_tuple[2]
hello
A dictionary is a mapping type; it allows you to access items via a
meaningful name (usually a string.)
Dictionaries do not preserve the order in which items are created (but
there is a class in newer python versions, collections.OrderedDict, which
does preserve order.)
Example:
>>> person = {} # start with an empty dictionary
>>> person['name'] = 'John'
>>> person['age'] = 40
>>> person['occupation'] = 'Programmer'
>>> print person['age']
40
Dictionaries can also be created with some initial values, like so:
>>> person = { 'name': 'John', 'age': 40, 'occupation' : 'Programmer' }
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> |
|---|---|
| Date | 2013-01-29 22:14 -0800 |
| Message-ID | <xZGdnaPMi5fuJ5XMnZ2dnUVZ_rCdnZ2d@o1.com> |
| In reply to | #37922 |
"John Gordon" <gordon@panix.com> wrote in message
news:keaa9v$1ru$1@reader1.panix.com...
> In <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d@o1.com> "Daniel W. Rouse Jr."
> <dwrousejr@nethere.comNOSPAM> writes:
>
>> I have recently started learning Python (2.7.3) but need a better
>> explanation of how to use tuples and dictionaries.
>
> A tuple is a linear sequence of items, accessed via subscripts that start
> at zero.
>
> Tuples are read-only; items cannot be added, removed, nor replaced.
>
> Items in a tuple need not be the same type.
>
> Example:
>
> >>> my_tuple = (1, 5, 'hello', 9.9999)
> >>> print my_tuple[0]
> 1
> >>> print my_tuple[2]
> hello
>
To me, this looks like an array. Is tuple just the Python name for an array?
> A dictionary is a mapping type; it allows you to access items via a
> meaningful name (usually a string.)
>
> Dictionaries do not preserve the order in which items are created (but
> there is a class in newer python versions, collections.OrderedDict, which
> does preserve order.)
>
> Example:
>
> >>> person = {} # start with an empty dictionary
> >>> person['name'] = 'John'
> >>> person['age'] = 40
> >>> person['occupation'] = 'Programmer'
> >>> print person['age']
> 40
>
> Dictionaries can also be created with some initial values, like so:
>
> >>> person = { 'name': 'John', 'age': 40, 'occupation' : 'Programmer' }
>
Thank you, I understand it better it is kind of like a hash table.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-30 17:25 +1100 |
| Message-ID | <mailman.1204.1359527154.2939.python-list@python.org> |
| In reply to | #37924 |
On Wed, Jan 30, 2013 at 5:14 PM, Daniel W. Rouse Jr. <dwrousejr@nethere.comnospam> wrote: > To me, this looks like an array. Is tuple just the Python name for an array? Not quite. An array is closer to a Python list - a tuple can be thought of as a "frozen list", if you like. Lists can be added to, removed from, and changed in many ways; tuples are what they are, and there's no changing them (the objects inside it could be changed, but WHAT objects are in it won't). Python has no strict match to a C-style array with a fixed size and changeable members; a Python list is closest to a C++ std::vector, if that helps at all. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-30 07:12 +0000 |
| Message-ID | <5108c7fb$0$11104$c3e8da3@news.astraweb.com> |
| In reply to | #37924 |
On Tue, 29 Jan 2013 22:14:42 -0800, Daniel W. Rouse Jr. wrote: > "John Gordon" <gordon@panix.com> wrote in message > news:keaa9v$1ru$1@reader1.panix.com... >> A tuple is a linear sequence of items, accessed via subscripts that >> start at zero. >> >> Tuples are read-only; items cannot be added, removed, nor replaced. >> >> Items in a tuple need not be the same type. >> >> Example: >> >> >>> my_tuple = (1, 5, 'hello', 9.9999) >> >>> print my_tuple[0] >> 1 >> >>> print my_tuple[2] >> hello >> > To me, this looks like an array. Is tuple just the Python name for an > array? Absolutely not. Arrays can be modified in place. Tuples cannot. Arrays can be resized (depending on the language). Tuples cannot. Arrays are normally limited to a single data type. Tuples are not. Python lists are closer to arrays, although the "array" type found in the array module is even closer still. You create lists either with the list() function, or list builder notation using [ ]. Tuples are intended to be the equivalent of a C struct or Pascal record. Lists are very roughly intended to be somewhat close to an array, although as I said the array.py module is even closer. >> A dictionary is a mapping type; it allows you to access items via a >> meaningful name (usually a string.) [...] > Thank you, I understand it better it is kind of like a hash table. Correct. Also known as "associative array". -- Steven
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-01-30 00:14 -0800 |
| Message-ID | <6b00f501-5234-4e0c-a297-807ead99f14f@v7g2000pbg.googlegroups.com> |
| In reply to | #37909 |
On Jan 30, 7:55 am, "Daniel W. Rouse Jr." <dwrous...@nethere.comNOSPAM> wrote: > Or, can an anyone provide an example of > more than a three-line example of a tuple or dictionary? Have you seen this byt the creator of python -- GvR? http://www.python.org/doc/essays/graphs.html > I have recently started learning Python (2.7.3) but need a better > explanation of how to use tuples and dictionaries. This is an important question: To start off you need to digest whats the diff between value oriented and object oriented. Since this is more to do with paradigm than with a specific language you may read for example: http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue3/Functional_Programming_vs_Object_Oriented_Programming In the python data structure world: value | object tuple | list XX | dictionary frozen-set | set > > I am currently using "Learning Python" by Mark Lutz and David Ascher, > published by O'Reilly (ISBN 1-56592-464-9)--but I find the explanations > insufficient and the number of examples to be sparse. I do understand some > ANSI C programming in addition to Python (and the book often wanders off > into a comparison of C and Python in its numerous footnotes), but I need a > better real-world example of how tuples and dictionaries are being used in > actual Python code. > > Any recommendations of a better book that doesn't try to write such compact > and clever code for a learning book? > The purpose of my learning Python in this case is not for enterprise level > or web-based application level testing at this point. I initially intend to > use it for Software QA Test Automation purposes. > > Thanks in advance for any replies.
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-02-02 21:20 -0800 |
| Message-ID | <36061bf4-2a65-4ac2-91a1-30f8348f4b0d@googlegroups.com> |
| In reply to | #37909 |
############################################################
# Quote: Daniel Rouse Jr. #
############################################################
# To me, this looks like an array. Is tuple just the #
# Python name for an array? #
############################################################
The problem with understanding Python collections is directly due to improper naming.
First let's consider the sequence types "list" and "tuple". For the most part lists and tuples are exactly the same. They are both containers for holding values.
GvR wisely choose to borrow the English word "list" over the esoteric CS term "array", but he /unwisely/ choose to borrow the maths term "tuple" over something more self-documenting to describe what is basically an immutable list.
Even someone who has no programming experience could most probably intuit what a "Python list" /is/. Everyone has made a grocery list, or a "to-do" list. The transformation from a tangible object like: *a linear list of items written on paper* to an intangible object like: *a Python list holding N objects* is not very difficult fathom. HOWEVER, then along comes the "seemingly" innocent tuple with fiery red hair and that devilish little grin intent on screwing up the whole logical flea circus!
Now you may ask yourself:
WHAT THE HELL IS A TUPLE? AND WHERE DID THIS ESOTERIC TERM ORIGINATE!
And if you ask the oracle (aka: Google) you might get this answer:
*Google Said:* /"""In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a non-negative integer. """"/
Okay google, so a tuple is an /ordered set/ and a list is an /ordered collection/, got it, (and thanks GvR for the mental overload!) however the names fail to convey this VERY important piece of information
For the fix, it would seem logical to simply extend the term "list" in a manner that will convey a "set" relationship. "DynamicList" and "StaticList" fit the bill HOWEVER these terms are FAR to verbose to use on a daily basis! Now, we could naively use "list" for an ordered collection, and "staticlist" for an ordered set, HOWEVER even this is a foolish choice!
The final solution is NOT two different types with verbose names, NO, the solution is ONE ordered collection type with a method to convert it into an ordered set. Observe:
py> list = list()
py> list.extend([1,2,3])
[1,2,3]
py> list.append('logical')
[1,2,3,'logical']
py> staticList = list.freeze()
py> staticList[-1]
'logical'
py> staticList.append('error')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
staticList.append('error')
AttributeError: 'StaticList' object has no attribute 'append'
*school-bell*
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-02-03 00:14 -0700 |
| Message-ID | <mailman.1307.1359875677.2939.python-list@python.org> |
| In reply to | #38096 |
On 02/02/2013 10:20 PM, Rick Johnson wrote: > *school-bell* I'm already regretting typing this, but really? The term, "tuple," was used rather consistently all through my university years. And Python's use of it is consistent with how it is used all through computer science. And for that matter it is consistent with how other languages, including LISP(!), use the term. As you say, we borrow it from Math. And for good reason. Now I know they say that foolish consistency is the hobgoblin of little minds, but using the word, "tuple," in this consistent way is hardly foolish. In this case, as is usually the case with your strange criticisms of well-established practice, there are good reasons for it. TL;DR: I find your list freezing proposal to be needlessly complicated. No the burden of proof is not on me to explain why tuples are so.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-02-03 18:21 +1100 |
| Message-ID | <mailman.1308.1359876109.2939.python-list@python.org> |
| In reply to | #38096 |
On Sun, Feb 3, 2013 at 6:14 PM, Michael Torrie <torriem@gmail.com> wrote: > TL;DR: I find your list freezing proposal to be needlessly complicated. > No the burden of proof is not on me to explain why tuples are so. We have a list-freezing mechanism already. >>> list=[1,2,3,'logical'] >>> staticList=tuple(list) Et voila! Looks fine to me. ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web