functions!
The plan!
Basics: data types (and operations & calculations)
Basics: conditionals & iteration
Basics: lists, tuples, dictionaries
Basics: writing functions
Reading & writing files: opening, parsing & formats
Working with numbers: numpy & scipy
Making plots: matplotlib & pylab
… if you guys want more after all of that …
Writing better code: functions, pep8, classes
Working with numbers: Numpy & Scipy (advanced)
Other modules: Pandas & Scikit-learn
Interactive notebooks: ipython & jupyter
Advanced topics: virtual environments & version control
functions
# lets define a function
def print_hello(name):
print "Hello", name, ", it's a nice day for functions!"
# now we can call (or run, or execute) the function
print_hello("Marc")
functions
# lets define a function
def print_hello(name):
print "Hello", name, ", it's a nice day for functions!"
# now we can call (or run, or execute) the function
print_hello("Marc") Defining the function:
def function_name(arg1, arg2, ...):
do stuff here
Running the function:
function_name(value1, value2, ...):
indentation!
functions
# lets define a function
def print_hello(name):
print "Hello", name, ", it's a nice day for functions!"
# now we can call (or run, or execute) the function
print_hello("Marc")
# let's add some arguments
def print_hello(name, weather):
print "Hello", name, ", it's", weather.
print "Perfect weather for writing functions!"
print_hello("Marc", "Sunny")
print hello("Gengis", "Raining")
# arguments are optional
def print_hello():
print "Hello, this function is a little boring, right?"
# to run a function with 0 arguments, use empty parentheses
print_hello()
Defining the function:
def function_name(arg1, arg2, ...):
do stuff here
Running the function:
function_name(value1, value2, ...):
indentation!
functions
# the function definition can be any code!
def print_hello(name, weather, number):
print "Hello", name, ", it's", weather.
if weather == "Sunny":
print "Go outside and play!"
else:
print "Stay inside and program!"
print "Here are some numbers:"
for i in range(number):
print i
print "This is one hell of a long function!"
# nothing happens until the function is run!
print_hello("Marc","Raining", 200)
exercise! wohoo!
Write function that takes two arguments: a name and a number. The function then
prints the name "number" time.
---- names.py ----
def name_repeat(????):
????
????
????
name_repeat("Marc", 10)
functions: arguments
# positional arguments are defined by their position
def print_hello(name, weather,):
print "Hello", name, ", the weather is", weather.
print_hello("Marc", "Sunny")
print_hello("Stormy", "Thor")
functions: arguments
# positional arguments are defined by their position
def print_hello(name, weather,):
print "Hello", name, ", the weather is", weather.
print_hello("Marc", "Sunny")
print_hello("Stormy", "Thor")
# keyword arguments are optional, and have default values.
def print_hello(name, weather, drink= "Tea", snack= "Cookie"):
print "Hello", name, ", the weather is", weather
print "Why don’t you drink a ", drink, "with some", snack
print_hello("Marc", "Sunny", drink="Beer", snack="Tremocos")
functions: arguments
# positional arguments are defined by their position
def print_hello(name, weather,):
print "Hello", name, ", the weather is", weather.
print_hello("Marc", "Sunny")
print_hello("Stormy", "Thor")
# keyword arguments are optional, and have default values.
def print_hello(name, weather, drink= "Tea", snack= "Cookie"):
print "Hello", name, ", the weather is", weather.
print "Why don’t you drink a ", drink, "with some", snack
print_hello("Marc", "Sunny", drink="Beer", snack="Tremocos")
Defining arguments:
def function_name(arg1, arg2, … kwarg1=default1, kwarg2=default2, … ):
Code goes here
positional keyword
functions: arguments
# positional arguments are defined by their position
def print_hello(name, weather,):
print "Hello", name, ", the weather is", weather.
print_hello("Marc", "Sunny")
print_hello("Stormy", "Thor")
# keyword arguments are optional, and have default values.
def print_hello(name, weather, drink= "Tea", snack= "Cookie"):
print "Hello", name, ", the weather is", weather.
print "Why don’t you drink a ", drink, "with some", snack
print_hello("Marc", "Sunny", drink="Beer", snack="Tremocos")
# keyword arguments are optional, and the order doesn’t matter
print_hello("Bob","Rainy")
print_hello("Bob","Rainy", snack="Cake", drink="Coffee")
functions: returning values
# functions are not usually used for printing
# typically they return a value
def times_two_plus_three(x):
return x * 2 + 3
# we can now use this function
n = times_two_plus_three(5)
print n
functions: returning values
# functions are not usually used for printing
# typically they return a value
def times_two_plus_three(x):
return x * 2 + 3
# we can now use this function
n = times_two_plus_three(5)
print n
Defining the function:
def function_name(arg1, arg2, ...):
do stuff here
return some_value
Running the function:
answer = function_name(value1, value2, ...):
functions: returning values
# functions are not usually used for printing
# typically they return a value
def times_two_plus_three(x):
return x * 2 + 3
# we can now use this function
n = times_two_plus_three(5)
print n
# let's make it a little more complicated.
# and add some documentation as well
def my_complex_calculation(a, b, c):
""" This returns the value of:
a * b + c
"""
result = x * b + c
return result
# we can now use this function
print my_complex_function(8, 1, 12)
Defining the function:
def function_name(arg1, arg2, ...):
""" Documentation (comment)
"""
do stuff here
return some_value
Running the function:
answer = function_name(value1, value2, ...):
exercises!
- Write function that takes three inputs (numbers x, y and z), and produces the
result of : a + b - c. Use return to be able to store and use the value later.
---- calc.py ----
def my_calculation(????):
????
????
????
x = my_calculation(12, 13, 14)
print "The complex calculation with 12, 13 and 14 resulted in", x
version 2: as above, but if no numbers for b or c are specified, use 5 and 10 as default
values.

Class 7a: Functions

  • 1.
  • 2.
    The plan! Basics: datatypes (and operations & calculations) Basics: conditionals & iteration Basics: lists, tuples, dictionaries Basics: writing functions Reading & writing files: opening, parsing & formats Working with numbers: numpy & scipy Making plots: matplotlib & pylab … if you guys want more after all of that … Writing better code: functions, pep8, classes Working with numbers: Numpy & Scipy (advanced) Other modules: Pandas & Scikit-learn Interactive notebooks: ipython & jupyter Advanced topics: virtual environments & version control
  • 3.
    functions # lets definea function def print_hello(name): print "Hello", name, ", it's a nice day for functions!" # now we can call (or run, or execute) the function print_hello("Marc")
  • 4.
    functions # lets definea function def print_hello(name): print "Hello", name, ", it's a nice day for functions!" # now we can call (or run, or execute) the function print_hello("Marc") Defining the function: def function_name(arg1, arg2, ...): do stuff here Running the function: function_name(value1, value2, ...): indentation!
  • 5.
    functions # lets definea function def print_hello(name): print "Hello", name, ", it's a nice day for functions!" # now we can call (or run, or execute) the function print_hello("Marc") # let's add some arguments def print_hello(name, weather): print "Hello", name, ", it's", weather. print "Perfect weather for writing functions!" print_hello("Marc", "Sunny") print hello("Gengis", "Raining") # arguments are optional def print_hello(): print "Hello, this function is a little boring, right?" # to run a function with 0 arguments, use empty parentheses print_hello() Defining the function: def function_name(arg1, arg2, ...): do stuff here Running the function: function_name(value1, value2, ...): indentation!
  • 6.
    functions # the functiondefinition can be any code! def print_hello(name, weather, number): print "Hello", name, ", it's", weather. if weather == "Sunny": print "Go outside and play!" else: print "Stay inside and program!" print "Here are some numbers:" for i in range(number): print i print "This is one hell of a long function!" # nothing happens until the function is run! print_hello("Marc","Raining", 200)
  • 7.
    exercise! wohoo! Write functionthat takes two arguments: a name and a number. The function then prints the name "number" time. ---- names.py ---- def name_repeat(????): ???? ???? ???? name_repeat("Marc", 10)
  • 8.
    functions: arguments # positionalarguments are defined by their position def print_hello(name, weather,): print "Hello", name, ", the weather is", weather. print_hello("Marc", "Sunny") print_hello("Stormy", "Thor")
  • 9.
    functions: arguments # positionalarguments are defined by their position def print_hello(name, weather,): print "Hello", name, ", the weather is", weather. print_hello("Marc", "Sunny") print_hello("Stormy", "Thor") # keyword arguments are optional, and have default values. def print_hello(name, weather, drink= "Tea", snack= "Cookie"): print "Hello", name, ", the weather is", weather print "Why don’t you drink a ", drink, "with some", snack print_hello("Marc", "Sunny", drink="Beer", snack="Tremocos")
  • 10.
    functions: arguments # positionalarguments are defined by their position def print_hello(name, weather,): print "Hello", name, ", the weather is", weather. print_hello("Marc", "Sunny") print_hello("Stormy", "Thor") # keyword arguments are optional, and have default values. def print_hello(name, weather, drink= "Tea", snack= "Cookie"): print "Hello", name, ", the weather is", weather. print "Why don’t you drink a ", drink, "with some", snack print_hello("Marc", "Sunny", drink="Beer", snack="Tremocos") Defining arguments: def function_name(arg1, arg2, … kwarg1=default1, kwarg2=default2, … ): Code goes here positional keyword
  • 11.
    functions: arguments # positionalarguments are defined by their position def print_hello(name, weather,): print "Hello", name, ", the weather is", weather. print_hello("Marc", "Sunny") print_hello("Stormy", "Thor") # keyword arguments are optional, and have default values. def print_hello(name, weather, drink= "Tea", snack= "Cookie"): print "Hello", name, ", the weather is", weather. print "Why don’t you drink a ", drink, "with some", snack print_hello("Marc", "Sunny", drink="Beer", snack="Tremocos") # keyword arguments are optional, and the order doesn’t matter print_hello("Bob","Rainy") print_hello("Bob","Rainy", snack="Cake", drink="Coffee")
  • 12.
    functions: returning values #functions are not usually used for printing # typically they return a value def times_two_plus_three(x): return x * 2 + 3 # we can now use this function n = times_two_plus_three(5) print n
  • 13.
    functions: returning values #functions are not usually used for printing # typically they return a value def times_two_plus_three(x): return x * 2 + 3 # we can now use this function n = times_two_plus_three(5) print n Defining the function: def function_name(arg1, arg2, ...): do stuff here return some_value Running the function: answer = function_name(value1, value2, ...):
  • 14.
    functions: returning values #functions are not usually used for printing # typically they return a value def times_two_plus_three(x): return x * 2 + 3 # we can now use this function n = times_two_plus_three(5) print n # let's make it a little more complicated. # and add some documentation as well def my_complex_calculation(a, b, c): """ This returns the value of: a * b + c """ result = x * b + c return result # we can now use this function print my_complex_function(8, 1, 12) Defining the function: def function_name(arg1, arg2, ...): """ Documentation (comment) """ do stuff here return some_value Running the function: answer = function_name(value1, value2, ...):
  • 15.
    exercises! - Write functionthat takes three inputs (numbers x, y and z), and produces the result of : a + b - c. Use return to be able to store and use the value later. ---- calc.py ---- def my_calculation(????): ???? ???? ???? x = my_calculation(12, 13, 14) print "The complex calculation with 12, 13 and 14 resulted in", x version 2: as above, but if no numbers for b or c are specified, use 5 and 10 as default values.