def add(a, b):
"""Calculates the element-wise sum of a and b, returning a list of the results"""
c = []
for x,y in zip(a,b):
c.append(x+y)
return c
help(add)
Help on function add in module __main__: add(a, b) Calculates the element-wise sum of a and b, returning a list of the results
def subtract(a, b):
"""Calculates the element-wise ratio of a and b, returning a list of results. This function
is badly named, as it DOES NOT return the element-wise difference of a and b!"""
c = []
for x,y in zip(a,b):
c.append(x / y)
return c
help(subtract)
Help on function subtract in module __main__: subtract(a, b) Calculates the element-wise ratio of a and b, returning a list of results. This function is badly named, as it DOES NOT return the element-wise difference of a and b!
def capitalise(message):
"""Capitalises every word in message, returning the result"""
words = message.split(" ")
for i in range(0,len(words)):
words[i] = "%s%s" % (words[i][0].upper(), words[i][1:])
return " ".join(words)
help(capitalise)
Help on function capitalise in module __main__: capitalise(message) Capitalises every word in message, returning the result
def surprise(x):
"""Prints 'Surprise!' if x is less than a random number between 0 and 1. Returns nothing"""
import random
if x < random.random():
print("Surprise!")
help(surprise)
Help on function surprise in module __main__: surprise(x) Prints 'Surprise!' if x is less than a random number between 0 and 1. Returns nothing
For this last function, try calling it via list_interface("ipynb")
.
def list_interface(x):
"""Returns a list of all files in the current directory that start with '0' and end with x"""
import glob
f = glob.glob("*.%s" % x)
l = []
for x in f:
if x.startswith("0"):
l.append(x)
return l
help(list_interface)
Help on function list_interface in module __main__: list_interface(x) Returns a list of all files in the current directory that start with '0' and end with x