Help get this topic noticed by sharing it on Twitter, Facebook, or email.

don't understand code at bottom of pg 261 of learning python

At the bottom of pg 261. why does >>>db[0]['jobs'] gives no response rather than ['developer,'manager]? Same question for top of page 262 for >>>db['bob']['jobs'].
1 person has
this question
+1
Reply
  • Hello David,

    I have forwarded your question on to our Book Tech staff, so they can help you find a solution. If you ever have any questions or issues with the content of a title, you can also contact them directly at booktech@oreilly.com or call them at:

    1-800-889-8969
    or
    1-707-827-7019 (outside the USA)
    7:30 am to 5:00 PST

    Kind regards,
    Paul Fichera
    Customer Service Representative
    O'Reilly Media
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • Hi David,

    I have passed your message on to the author for him to clarify.

    Best regards,
    Chris Olson
    O'Reilly Book Support
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • Hi David,

    The author responded to your question with the following note:
    -----------------
    This code is not being run at the interactive prompt which
    prints results; it's just being listed as an example here,
    and an abstract one at that. Notice the lack of a ">>>"
    interactive prompt or bold font, the same as in the other
    output-less code on page 262. Also notice the italicized
    "other"; it's supposed to stand for another record structure.

    This code will print what you expect if run live, as in the
    following (I'm using a string for "other"):

    >>> rec = {'name': 'Bob',
    ... 'jobs': ['developer', 'manager'],
    ... 'web': 'www.bobs.org/ ̃Bob',
    ... 'home': {'state': 'Overworked', 'zip': 12345}}
    >>>
    >>> db = []
    >>> db.append(rec)
    >>> db.append('other')
    >>> db[0]['jobs']
    ['developer', 'manager']
    >>>
    >>> db = {}
    >>> db['bob'] = rec
    >>> db['sue'] = 'other'
    >>> db['bob']['jobs']
    ['developer', 'manager']

    When in doubt, try running the examples yourself (the book's
    examples package and ebook cut-and-paste can help), and don't
    expect to see outputs in the book for code not typed at the
    ">>>" prompt. This is especially true of later, larger code.

    Thanks,
    --Mark Lutz (http://learning-python.com, http://rmi.net/~lutz)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned