-
In Python, give an example of how to add a new element to the
end of a list.
list.append(element)
-
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
function.
- def callfunction(func):
- return func("hello world")
- callfunction(print)
-
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()
-
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
-
In Python, give an example of operator overloading.
- class Account(object):
- 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?
isinstance(var, Type)
-
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.
re.compile("^Python$")
-
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[0] is possible, but set[0] is illegal
-
In Python, what is the preferred way to document a
function?
- 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?
add.__doc__
-
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()
-
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
-
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
|
|