[Tutor] Help regarding lists, dictionaries and tuples
Robert Sjöblom
robert.sjoblom at gmail.com
Mon Nov 22 01:48:45 CET 2010
[Snip]
>> I don't want a direct answer on how to proceed, but a question that
>> arose during my thinking of the problem was whether dictionaries can
>> have integral data in them, like so:
>> attributes = {"Strength" : 28, "Health" : 12}
>> or if they have to be:
>> attributes = {"Strength" : "28", "Health" : "12"}
>>
>> Ok, I'm clearly thinking in circles here. I used the interpreter to
>> figure out that both are fine but the first example has integers,
>> whereas the second has strings. Good to know. What I'm wondering then,
>> instead, is whether there's a good way to sum up the value of integral
>> data in a dictionary?
>>
>
> I will point you toward two things. First, the dict class has a method
> called values(). Read the documentation on that, go into your
> interpreter, make some dictionaries, and call the values() method, see
> what comes up.
>
> The second thing is the sum() method. Again, read the documentation on
> it and experiment with it a little. Those two should suffice to answer
> your question.
They did indeed! I did run across values() before sending the message,
but didn't quite get it to work properly -- reading up on it, I
figured out that I've been calling the method completely wrong.
> I always encourage people to play around with their problems in the
> interactive interpreter, discovering what works and what doesn't for
> yourself is often a great way to learn. Combine that with
> http://docs.python.org and you've got some very powerful learning
> tools
Yeah, using the interpreter is very helpful, I've found. I find that
even if I've studied something in the documentation, I need to use the
interpreter to figure out how it actually works. Maybe that will get
better in time, but for now it's a very useful tool in
trial-and-error. I had to try out values() and sum() before I could
understand them fully.
Thanks again, much appreciated.
best regards,
Robert S.
More information about the Tutor
mailing list