Card Set Information
scripting final exam
In Python, give an example of how to add a new element to the
end of a list.
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]
In Python, give an example of a definition of a function that
takes another function as a parameter and an example of calling this
return func("hello world")
In Python, give an example of a static method and how to call it.
Account.rate = 5
print "Account.interest_rate() is ", Account.interest_rate()
In Python, what is a class property and give an example.
return self.balance * (1 + Account.rate/100.0)
In Python, give an example of operator overloading.
def __add__(self, other)
return self.balance + other
print a + 9
In Python, how do you determine the type of what is storedin a variable?
Why might you want to use the Python Decimal type?
To be more precise by avoiding floating-point encoding errors.
In Python, give an example of defining a new regularexpression object that exactly matches the string "Python" only.
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
In Python, what is the main difference between a list and a tuple?
You cannot change the components of a tuple
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 is possible, but set is illegal
In Python, what is the preferred way to document a
By adding a documentation string as the next line right
after the def.
In Python, how would you get the value of the documentation
string for a function called add?
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:
n += 1
c = count(1)
In Python, how would you define a class called Savings as a
subclass of Account with balance initialized to zero?
balance = 0
In Python, how would you define a method of Savings called
withdraw that takes as an argument the amount to withdraw from the
balance = 0
def withdraw(self, amount)
balance -= amount