Quiz for "Learning Python for PL/SQL Developers: Part 3"

Версия 2

    by Arup Nanda

     

    Questions

     

    1. Is the following valid? If so, why, and if no, why not?

     

    mylist = [100,200,300,'Hello','World']

     

    2. How about the following? Will it cause a syntax error, and if so where?

     

    mylist = [100,200,300,(),[]]

     

    3. How about the following? Will it cause a syntax error?

     

    mylist = [100,200,300,(),None,[1,2]]

     

    4. Where will the following throw a syntax error?

     

    mylist = [100,200,300,]

     

    5. Will the following work?

     

    mylist = [100,200,300,)

     

    6. What will be the result of the following?

     

    mylist = (100,200,300,) mylist.append(400)

     

    7. What will be the output of the following?

     

    mylist = {1,2,3,4,5,5,6} len(mylist)

     

    8. What will be the output of the following?

     

    mylist = {6,5,4,3,2,1} mylist

     

    9. In the previous question, you declared a set called mylist. You want to reference the third element of the collection. How will you address it?

     

    a. mylist[2]

     

    OR

     

    b. mylist[3]

     

    10. You want to declare a tuple with only a string value, for example, "Saturday." Will the following work?

     

    sat_day = ('Saturday')

     

    11. You want to create a collection of week days. Since they will not change, you create them as a tuple, as shown below:

     

    days = ('Sun','Mon','Tue','Wed','Thu','Fri')

    Then you suddenly discover that you forgot to include Saturday. Since this is a tuple, you can't use the append() method to add an element. What are your options? Should you drop and then re-create that variable?

     

    Answers

     

    1. It's valid. Lists are merely lists of variables. There is no need for the variables to be of the same type, unlike with PL/SQL.

     

    2. No; it's valid. The last two elements are merely an empty tuple and en empty list, respectively. It is perfectly valid for them to be present there. In fact, you can write the following:

     

    mylist = [100,200,300,(),[1,2]]

     

    The last element is another list with two elements. If you check the variable, you get this:

     

    >>> mylist[4]

    [1, 2]

     

    You can even reference the elements of the list within that list:

     

    >>> mylist[4][0]

    1

     

    3. No; it's valid. "None" means null in Python. Essentially it's just a placeholder. If you print mylist[4], you will not see any output.

     

    4. Nowhere. This is valid. The trailing comma is merely ignored. Pay attention to this in Python.

     

    5. No; this will throw a syntax error. The ending parenthesis is regular (that is, "("), which indicates a tuple; but the beginning parenthesis is square (that is, "]"), which indicates a list.

     

    6. It will fail. Collections enclosed by regular parentheses are tuples, which cannot be updated. So append will not work. If you want to update the collection after it is declared, use list, which is indicated by enclosing square brackets, not by parentheses.

     

    7. The output will be "6," although you passed seven items. Since you declared it as a set, which can't contain duplicates, the extra 5 will be dropped. You can check it by displaying mylist again:

     

    >>> mylist

    {1, 2, 3, 4, 5, 6}

     

    8. It will print {1,2,3,4,5,6}, even though the numbers were entered in just the reverse manner. Sets are ordered; so regardless of how you enter them, they are stored in an ordered manner.

     

    9. Neither. Elements in sets are not positional; so they can't be referenced by indexes. If you want to reference by index, use a list or tuple.

     

    10. It will not. That code will merely create a character string variable, not a tuple variable. To create a tuple, you need to use this:

     

    sat_day = ('Saturday',)

     

    Note the trailing comma.

     

    11. Although you can't alter a tuple, you can replace it completely. So you can create another tuple with only one value:"Sat," add these two tuples, and replace the original tuple by the result. Here is an example:

     

    >>> days = ('Sun','Mon','Tue','Wed','Thu','Fri')

    >>> sat_day = ('Sat',)

    >>> days += sat_day

     

    Now if you display that tuple, you will see the new element.

     

    >>> days

    ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')

     

    Back to the Part 3 article.

     

    About the Author

     

    Arup Nanda (arup@proligence.com) has been an Oracle DBA since 1993, handling all aspects of database administration, from performance tuning to security and disaster recovery. He was Oracle Magazine's DBA of the Year in 2003 and received an Oracle Excellence Award for Technologist of the Year in 2012.