More Nested Tuples

  • Comments posted to this topic are about the item More Nested Tuples

  • Nice, easy question, thanks, Steve

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • Misread the question and picked option 5 - Costumes[1][2][0:1]
    Would that return both "Blue" and "Orange"?
    I'm not a regular SQL Server user anymore and not familiar with tuples...

  • nice question
    cheers

    ---------------------------------------------------------------------------------------
    The more you know, the more you know that you dont know

  • Arno.Hubert.Janssen - Wednesday, October 31, 2018 6:53 AM

    Misread the question and picked option 5 - Costumes[1][2][0:1]
    Would that return both "Blue" and "Orange"?
    I'm not a regular SQL Server user anymore and not familiar with tuples...

    Hi Arno,
    today's Qotd is about accessing values of Tuples in Python.

    print Costumes[1][2][0:1]; returns ('Blue',)
    print Costumes[1][2][1:2]; returns ('Orange',)
    print Costumes[1][2][0:2]; returns ('Blue', 'Orange')
    print Costumes[0][1][0:6]; returns Batman

  • I tried below example from w3schools.com and getting an error. 

    Example:
    thistuple = ("apple", "banana", "cherry")
    thistuple[1] = "blackcurrant"
    # The values will remain the same:
    print(thistuple)

    Result:
    >>> thistuple = ("apple", "banana", "cherry")
    >>> thistuple[1] = "blackcurrant"
    Traceback (most recent call last):
    File "<pyshell#33>", line 1, in <module>
      thistuple[1] = "blackcurrant"
    TypeError: 'tuple' object does not support item assignment
    >>>

    Any Idea.

    ThanksSaurabh.D

  • Saurabh.D - Wednesday, October 31, 2018 6:35 PM

    I tried below example from w3schools.com and getting an error. 

    Example:
    thistuple = ("apple", "banana", "cherry")
    thistuple[1] = "blackcurrant"
    # The values will remain the same:
    print(thistuple)

    Result:
    >>> thistuple = ("apple", "banana", "cherry")
    >>> thistuple[1] = "blackcurrant"
    Traceback (most recent call last):
    File "<pyshell#33>", line 1, in <module>
      thistuple[1] = "blackcurrant"
    TypeError: 'tuple' object does not support item assignment
    >>>

    Any Idea.

    Tuples are immutable which means you cannot update or change the values of tuple elements.
    You are able to take portions of existing tuples to create new tuples as the following example demonstrates:

    #!/usr/bin/python

    thistuple = ("apple", "banana", "cherry");
    print thistuple;
    fruits = ("apricot", "strawberry", "pear");
    print fruits;
    all_tuples = thistuple + fruits;
    print all_tuples;

    Result
    $python main.py
    ('apple', 'banana', 'cherry')
    ('apricot', 'strawberry', 'pear')
    ('apple', 'banana', 'cherry', 'apricot', 'strawberry', 'pear')

  • Thanks George for the clarification!!!

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply