ICS215Final

  1. In Python, give an example of how to add a new element to the
    end of a list.
    list.append(element)
  2. In Python, what is the shorthand notation for creating a
    new list by doing something to every element of another list?
    [action using var for var in list]
  3. In Python, give an example of a definition of a function that
    takes another function as a parameter and an example of calling this
    function.
    • def callfunction(func):
    • return func("hello world")
    • callfunction(print)
  4. In Python, give an example of a static method and how to call it.
    • class Account(object):
    • @staticmethod
    • def interest_rate():
    • return Account.rate
    • Account.rate = 5
    • print "Account.interest_rate() is ", Account.interest_rate()
  5. In Python, what is a class property and give an example.
    • class Account(object):
    • @property
    • def new_balance(self):
    • return self.balance * (1 + Account.rate/100.0)
    • print a.new_balance
  6. In Python, give an example of operator overloading.
    • class Account(object):
    • def __add__(self, other):
    • return self.balance + other
    • print a + 9
  7. In Python, how do you determine the type of what is storedin a variable?
    isinstance(var, Type)
  8. Why might you want to use the Python Decimal type?
    To be more precise by avoiding floating-point encoding errors.
  9. In Python, give an example of defining a new regularexpression object that exactly matches the string "Python" only.
    re.compile("^Python$")
  10. In Python how do you write a comment?
    • # starts a comment
    • f matchtext in line: ... fne = (yield) ... matchtext in line: ... tne = (yield) ... o the end of the line
  11. In Python, what is the main difference between a list and a tuple?
    You cannot change the components of a tuple
  12. In Python, what is the main difference between a list and a
    set and give an example?
    • A set is unordered and cannot be indexed by numbers,
    • for example list[0] is possible, but set[0] is illegal
  13. In Python, what is the preferred way to document a
    function?
    • By adding a documentation string as the next line right
    • after the def.
  14. In Python, how would you get the value of the documentation
    string for a function called add?
    add.__doc__
  15. What is a Python generator and give an example? A generator is a function that returns a new value everytime it's next() method is called on an instance of the function:
    • def count(n):
    • while true:
    • n += 1
    • yield n
    • c = count(1)
    • c.next()
  16. In Python, how would you define a class called Savings as a
    subclass of Account with balance initialized to zero?
    • class Savings(Account):
    • def __init__(self):
    • balance = 0
  17. In Python, how would you define a method of Savings called
    withdraw that takes as an argument the amount to withdraw from the
    balance?
    • class Savings(Account):
    • def __init__(self):
    • balance = 0
    • def withdraw(self, amount):
    • balance -= amount
Author
8ko8
ID
17783
Card Set
ICS215Final
Description
scripting final exam
Updated