Introductions: Hi, I'm .... I'll be co-facilitating 'Programming with Python' with [our mentors names] and [our volunteers names]. If you would like to follow along and type in some of the snippets of code that we demo, our volunteers can assist you in setting up your computer.
Let's begin by giving a quick recap on what we did in the earlier workshops today. In the first workshop we learned about numbers, variables, strings, booleans, and making choices with the use of if-statements. In the second workshop we added lists and looping to our skills. Along the way we used a few functions, such as the built-in functions, print() and len().
In this workshop, we're going to go a little further in depth into functions and Python modules.
We have already seen a few functions (recall len() for calculating list and string lengths and sum() for summing lists).
Python also lets us make our own functions.
We can use functions for a number of things:
Let's start with an example. Say we have a program that wishes someone a happy birthday:
In [ ]:
print("Happy Birthday, dear Carol!")
We can make this code into a function like so. Let's take a quick look at what we have here.
In [ ]:
def happy_birthday_carol():
print("Happy Birthday, dear Carol!")
The top part is called the 'header' of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.
The keyword 'def' is short for 'define', and it's how we let Python know that we are creating, that is, defining our new function.
The name of our function is 'happy_birthday_carol'. It has 0 parameters.
The body of our function contains four print lines. We need to indent these lines, and any other lines of code in function's body, by 4 spaces.
Notice that nothing was printed out after we defined our function. To use our function we need to call it, like this:
In [ ]:
happy_birthday_carol()
Don't forget those parenthesis. Without the parenthesis Python wouldn't know we were trying to call our function, and will print out a statement
In [ ]:
happy_birthday_carol
We can create a slew of functions named in a similar fashion:
In [ ]:
def happy_birthday_rise():
print("Happy Birthday, dear Rise!")
In [ ]:
happy_birthday_rise()
Ok, now, let's work on reusing our code so that we reduce code duplication.
Wouldn't it be cool if we could pass a name into our function so we don't need to write a different function for every name?
Python allows us to do just that with function parameters. We can add a parameter by typing a variable name in between the parethesis after our function name. We can then use the parameter variable in our function.
We'll create a new function called 'happy_birthday', and add a parameter, 'name'.
In [ ]:
def happy_birthday(name):
print("Happy Birthday, dear " + name + "!")
To execute or run our new function, we simply call it by its name, 'happy_birthday' and pass a value in between the parenthesis, such as the string, 'Trey'.
In [ ]:
happy_birthday("Trey")
Let's get a little bolder and make a list of names that we can loop over and then pass each one into our 'happy_birthday' function. What we get is the same results as the other earlier lines of code!
In [ ]:
names = ["Carol", "Rise", "Trey", "Alain"]
In [ ]:
for name in names:
happy_birthday(name)
Now, let's see what we need to do to make a function return a value to where the function was initially called.
So let's create a new function, 'get_cupcake_count'.
We'll multiple the number of guests by two and return the result because each guest wants to eat two cupcakes.
In [ ]:
def get_cupcake_count(guests):
return 2 * guests
In [ ]:
print "We need to make " + str(get_cupcake_count(10)) + " cupcakes."
Ok, so we call our function, get_cupcake_count, and pass in the integer 10 as the value of the parameter, 'guests'.
Then we multiply the number of guests by 2 and return the result back to where the function was initially called.
Lastly, it prints out the returned integer value of 20.
Note if you are wondering what the str() function is... Python does not allow us to concatenate strings with integers and will tell you so. We need to cast it with the string function to convert the integer into a string object
In [ ]:
print "We need to make " + str(get_cupcake_count(10)) + " cupcakes."
Let's talk about one last thing that functions can do and that is, they can be given more than one parameter. Let's demonstrate this by passing 2 parameters to our cupcake function.
We want to be able to specify the number of cupcakes each guest will eat. Let's change our function to accept two parameters:
In [ ]:
def get_cupcake_count(cupcakes, guests):
return cupcakes * guests
In [ ]:
print get_cupcake_count(4, 15)
Note that we have a 'cupcakes' parameter as well as a 'guests' parameter. Our function is called and passes in two integer values of 4 and 15. It then prints out the returned result, 60.
Modules are used to make a programmer's life easier. A module is used to group functions, variables, and other things together in an understandable bundle.
Python comes with lots of modules built-in. For example there's a random module for generating randomnes. Let's import it so we can use some of its functions:
In [ ]:
import random
The random module has a randint function which takes two parameters. randint will return a random integer in between (or including) the two parameters. For example we can generate a random number between 1 and 6.
In [ ]:
random.randint(1, 6)
Let's use the random integer function to simulate rolling a die in a game
In [ ]:
vegas_die_1 = random.randint(1, 6)
vegas_die_2 = random.randint(1, 6)
print ("First die: " + str(vegas_die_1))
print ("Second die: " + str(vegas_die_2))
print ("You rolled a " + str(vegas_die_1 + vegas_die_2))
choice is a function in the random module that returns a random item from a given list or string
In [ ]:
random.choice('abcdefghijklmnopqrstuvwxyz')
We could use random.choice to wish happy birthday to a random friend.
Let's make a function random_happy_birthday that takes a list of names and wishes a random person happy birthday
In [ ]:
def random_happy_birthday(names):
name = random.choice(names)
happy_birthday(name)
Now let's try it out with a list of names:
In [ ]:
random_happy_birthday(["Alain", "Carol", "Rise", "Trey"])
Modules and functions can make things really simple to do. For example we could in a few lines of code get and display a YouTube video
In [ ]:
from IPython.display import YouTubeVideo
# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar
YouTubeVideo('MirG-vJOg04')