More Nested Tuples

  • Steve Jones - SSC Editor

    SSC Guru

    Points: 715843

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

  • Stewart "Arturius" Campbell

    SSC Guru

    Points: 71401

    Nice, easy question, thanks, Steve

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

  • Arno.Hubert.Janssen

    SSCrazy

    Points: 2236

    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...

  • Shayn Thomas

    SSCertifiable

    Points: 5411

    nice question
    cheers

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

  • George Vobr

    SSCrazy Eights

    Points: 8976

    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

  • Saurabh.D

    SSC Eights!

    Points: 988

    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.

    Thanks
    Saurabh.D

  • George Vobr

    SSCrazy Eights

    Points: 8976

    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')

  • Arno.Hubert.Janssen

    SSCrazy

    Points: 2236

    Thanks George for the clarification!!!

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

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