Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Python Questions

  1. Registered TeamPlayer
    Join Date
    10-29-07
    Posts
    4,953
    Post Thanks / Like
    Stat Links

    Python Questions
    #11

    Re: Python Questions

    @tuples, packing, and unpacking


    First, tuples are lists that you can't change once you make them. The reason they are great is because they are cheap and easy to make.

    For instance
    a = (1, 2)
    makes an immutable list of 2 elements, the first being 1 and the second being 2

    Where this gets interesting with with packing and unpacking. Python has built in syntax for copying values into tuples for you (packing) and copying values out of tuples and into variables for you (unpacking).

    For instance I can do packing
    a = 1, 2
    which makes a tuple of (1, 2) and stores it in a

    Also, I can do unpacking
    b, c = (1, 2)
    b is now 1, and c is no 2

    Why does this matter? It saves a lot of typing

    Remember that for loop example I gave with enumerate?
    for index, value in enumerate(myList):

    "index, value" was unpacking a tuple for you. Otherwise you would have had to do
    for pair in enumerate(myList):
    index = pair[0]
    value = pair[1]

    which is a lot more typing

    The other place you'll see packing and unpacking a lot is in calling functions and returning values. The age old issue of functions is how to pass back more than one value. In C/C++/Java, you have to make a struct/class, pass it as input and fill it out.

    In python, you just make a tuple-on-the-fly and return that compound value. And then the caller unpacks it on the fly

    Example:
    def multipleReturnValues():
    a = ... some calculation
    b = ... some other calculation
    return a, b

    firstReturnValue, secondReturnValue = multipleReturnValues()


  2. Registered TeamPlayer
    Join Date
    10-29-07
    Posts
    4,953
    Post Thanks / Like
    Stat Links

    Python Questions
    #12

    Re: Python Questions

    @maps

    In python there is a "map interface" and a dictionary type (which implements the map interface).

    So at any time in python use { and } and you have an on-the-fly map

    a = {} //empty map

    a["asd"] = 2
    a["qwe"] = 3

    b = a["asd"]
    (b is now 2)

    You can also create them with initial values using special syntax

    a = {"key1": value1, "key2": value2} (and so on, just keep adding commas and key/value pairs)


    Any immutable type in python can be a map key. So strings, numbers, and tuples are all allowed (and some other things).


  3. Registered TeamPlayer Imisnew2's Avatar
    Join Date
    01-19-08
    Posts
    4,588
    Post Thanks / Like
    Blog Entries
    9
    #13

    Re: Python Questions

    Thank you very much raven (and you're definately not stepping on my toes...)

    And yea... I stumbled upon enumerate... just wasn't sure if it was the "right" way

    @maps:

    so I can say
    Code:
    a = {0:"Bob", 5:"Sally"}
    
    print a[0]
    print a["Bob"]
    will print
    'Bob'
    0
    ?

    Or does it not work bidirectional? And what if it was a 3-ary tuple?

    Code:
    a={0:"Bob":15, 5:"Sally":16}
    is that allowed?

  4. Registered TeamPlayer
    Join Date
    10-29-07
    Posts
    4,953
    Post Thanks / Like
    Stat Links

    Python Questions
    #14

    Re: Python Questions

    so between brackets are only keys
    and it always stores or returns values

    so print[0] would return "Bob"
    print["Bob"] would raise KeyError since you don't have the key "Bob" in your dict

    syntax for creating dictionaries is
    varName = { key1: value1, key2: value2, ...}

    so that 3-ary thing isn't valid

    And dictionaries have methods for enumerating keys, values, and pairs of key/values (.keys(), .values(), .items() respectively)

  5. Registered TeamPlayer nsRaven's Avatar
    Join Date
    08-17-07
    Posts
    2,587
    Post Thanks / Like
    #15

    Re: Python Questions

    I always forget about enumerate. The problem I have with tuples though is since you can't change them after they're created, they're not practical for anything but something like enumerate and zip and using them in iterations if you don't already have a list instead.

    @Imisnew2
    If you're used to something like a switch/case statement you won't find that in Python. The dictionary type that Ewok explained though is very versatile and can be used as a switch/case if you need something like that.

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Title