Python Function Definition Flashcards

 

card 1 of 22

 

def add(x,y):
  return x+y
>>> add(3,8)

 

substitute arguments for parameters x and y, then evaluate

11

card 2 of 22

 

def subtract(x,y):
return x-y
>>> subtract("one","two")

 

substitute and evaluate

IndentationError: expected an indented block

card 3 of 22

 

def add(x,y):
  return x+y
>>> add(3,add(4,2))-add(4,4)

 

substitute and evaluate "innermost" parenthesized parts first

1

card 4 of 22

 

def add(x,y):
  return x+y
>>> add(12,2,5)

 

substitute and evaluate

TypeError: add() takes 2 arguments (3 given)

card 5 of 22

 

def add(x,y):
  return x+y
>>> add("one","two")

 

substitute and evaluate

'onetwo'

card 6 of 22

 

def foo():
  print "Hello World"
>>> foo(7)

 

try to substitute argument for function parameter

TypeError: foo() takes no arguments (1 given)

card 7 of 22

 

def foo():
  print "Hello World"
>>> foo()

 

try to substitute argument for function parameter

Hello World
(in Python 3 the print statement would be an error)

card 8 of 22

 

def multiply(x,y)
   return x*y
>>> multiply(0.5,2.0)

 

substitute and evaluate

SyntaxError: missing colon on function definition line

card 9 of 22

 

def isEven(x):
  return (x%2)==0
>>> isEven(12)
>>> isEven(7)

 

the % operator, called modulus, computes the remainder:
for example, 15%11 is 4, and 15%5 is 0

True
False

card 10 of 22

 

def isEven(x):
  return (x%2)==0
def isOdd(y):
  return not isEven(y)
>>> isOdd(12)
>>> isOdd(7)

 

observe that argument is tied to y, then to x in evaluation

False
True

card 11 of 22

 

def not(x):
  return not x
>>> not(False)

 

remember Python rule about reserved names

SyntaxError: 'not' is reserved name

card 12 of 22

 

def sqrt(r):
   return r**0.5
>>>  sqrt(16)

 

this is easy

4

card 13 of 22

 

def plusminus(u,v):
   return [u+v,u-v]
>>>  plusminus(15,3)

 

observe that the result will be a sequence

[18,12]

card 14 of 22

 

# suppose sqrt and plusminus are defined as in previous two questions
def roots(a,b,c):
   return plusminus(-b,sqrt(b**2-4*a*c))
>>> roots(1,0,-4)

 

evaluate both plusminus arguments first

[4.0,-4.0]

card 15 of 22

 

def numCharsGt(s,c):
   return len([ e for e in s if e > c ])
>>> numCharsGt("one for every letter bigger than d",'d')

 

review list comprehension to evaluate what function returns

25

card 16 of 22

 

def isPrime(n):
   return 0 == [ n%k for k in range(2,n) ].count(0)
>>> isPrime(93)
>>> isPrime(27)
>>> isPrime(101)
>>> isPrime(1)

 

review list comprehension to evaluate what function returns

False
False
True
True

card 17 of 22

 

def backwards(s):
   return [ s[i] for i in range(len(s),0,-1) ]
>>> backwards([19,12,2,7,3])

 

work out the list comprehension when s is substituted

IndexError: list index out of range

card 18 of 22

 

def backwards(s):
   return [ s[i] for i in range(len(s)-1,0,-1) ]
>>> backwards([19,12,2,7,3])

 

work out the list comprehension when s is substituted

[3,7,2,12]

card 19 of 22

 

def backwards(s):
   return [ s[i-1] for i in range(len(s),0,-1) ]
>>> backwards([19,12,2,7,3])

 

work out the list comprehension when s is substituted

[3,7,2,12,19]

card 20 of 22

 

def backwards(s):
   return [ s[i] for i in range(len(s)-1,-1,-1) ]
>>> backwards([19,12,2,7,3])

 

work out the list comprehension when s is substituted

[3,7,2,12,19]

card 21 of 22

 

def backwards(s):
   return [ s[len(s)-i] for i in range(1,len(s)+1)]
>>> backwards([19,12,2,7,3])

 

remember that len(s) does not change during evaluation

[3,7,2,12,19]

card 22 of 22

 

def backwards(s):
   return [ s[len(s)-i] for i in range(1,len(s)+1)]
>>> backwards([19,12,2,7,3])
>>> backwards("wound")
>>> "".join(backwards("wound"))
>>> "".join(backwards(backwards("crazy")))

 

strings are somewhat like lists of characters

[3,7,2,12,19]
['d','n','u','o','w']
'dnuow'
'crazy'

 

End of Problem Set

(use browser reload to restart this problem set)