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.

Functions

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.

Example: Greeting Program

Say we have a simple program that prints a happy birthday greeting for Carol.

It's not Carol's birthday, but let's run this program anyway, just to confuse her in good fun ;)


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!")

Examine the function in detail

The top part is the header of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.

def is short for define. It's the Python keyword used to indicate the start of a function definition.

happy_birthday_carol is the name of the function.

This is a function with no parameters.

The body of our function contains one print line. We need to indent this line, and any other lines of code in a function's body, by 4 spaces.

Let's call the function

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 parentheses.

Without the parentheses, Python wouldn't know that we were trying to call our function, and will print out the value of the thing called happy_birthday_carol.


In [ ]:
happy_birthday_carol

Here, the Python interpreter is basically saying, "The value of happy_birthday_carol is a function."

Let's define and call another function

That function printed a nice greeting for Carol. But Rise's now feeling left out. Let's define a function for her too.


In [ ]:
def happy_birthday_rise():
    print("Happy Birthday, dear Rise!")

Now, let's call the function:


In [ ]:
happy_birthday_rise()

Refactor time!

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 + "!")

Here, we are concatenating 3 strings:

  • "Happy Birthday, dear "
  • whatever the value of name is (Note: for the purposes of this example, let's assume that we're always getting a string)
  • "!"

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")

Calling it over and over in a loop

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)

Exercise: Try to break happy_birthday()

1) Try calling happy_birthday() with other values, such as:

  • A really, really, really long name
  • Something ridiculous that makes no sense grammatically
  • Numbers
  • Other Python types, if you know about any other ones

This is how malicious "black hat" hackers think when they try to break into desktop and web applications.

2) Try adding a bad list item to names. Experiment with the position of the bad item.

Example: Better String Formatting

Rather than using string concatenation, let's use a string formatting operator that doesn't care what type of value it receives.


In [ ]:
def happy_birthday(name):
    print("Happy Birthday, dear {0}!".format(name))

Now, the function can print happy birthday greetings for any value of name, even for people with bizarre names like:

  • 100
  • 3.1415927
  • Ƭ̵̬̊ (The Artist Formerly Known As Prince)

In [ ]:
happy_birthday(100)

In [ ]:
happy_birthday(3.1415927)

In [ ]:
happy_birthday("Ƭ̵̬̊")

It's better to use format() just in case name isn't guaranteed to be a string.

Advanced Exercise: Why not use str() like this?


In [ ]:
def happy_birthday(name):
    print("Happy Birthday, dear " + str(name) + "!")

Answer: You can, but keep in mind that this only works on Python 3:


In [ ]:
str(u'\xff')

Now you have some advanced knowledge about how Python 3 handles Unicode better than Python 2!

Example: Getting Fancy With Math

What about a function that does some math for us?

Every year on my birthday, I like to figure out what numbers are divisible by my new age. That is, the factors of my age:


In [ ]:
def print_factors(age):
    for i in range(1, age + 1):
        if age % i == 0:
            print(i)

This year, I turned 32.


In [ ]:
print_factors(32)

From the output of print_factors(), I now know that I'm:

  • Twice as old as a 16-year-old
  • Four times as old as an 8-year-old
  • Eight times as old as a 4-year-old
  • Sixteen times as old as a 2-year-old

How is this useful? Well, the next time a 4-year-old bullies me, I'm going to say, "Watch it, I'm exactly eight times as old as you."

Next year, I'll be able to brag to 3-year-olds: "Ha ha ha, I'm exactly eleven times as old as you."


In [ ]:
print_factors(33)

Example: Cupcake Tally

So far we've explored functions that:

  • Take strings or other values as input
  • Print strings

Let's create a new function, cupcake_tally(). This function will be different from the others that I covered. Rather than just printing stuff, it'll return a value when called.

A good birthday party should allocate:

  • 2 cupcakes per guest
  • 13 cupcakes for the birthday person, because if it's your birthday, you should be allowed to eat a baker's dozen of cupcakes.

In [ ]:
def cupcake_tally(guests):
    """ Given number of party guests, returns how many cupcakes are needed. """
    return 2 * guests + 13

How many cupcakes do we need for a party with 30 guests?


In [ ]:
cupcake_tally(30)

What about if I'm celebrating my birthday at home with just my husband, Daniel?


In [ ]:
cupcake_tally(guests=1)

Ooh, 15 cupcakes sounds about right for our little party, yeah? ;)

Notice how we explicitly named the guests parameter in that example.

Printing the return value


In [ ]:
cupcakes = cupcake_tally(10)
print("We need to make {0} cupcakes.".format(cupcakes))

We call our function, cupcake_tally(), and pass in 10 as the value of guests.

cupcake_tally(10) returns 33, which gets stored in the cupcakes variable.

Lastly, we print "We need to make 33 cupcakes." using Python string formatting.

Multiple function arguments

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 cupcake_tally(cupcakes, guests):   
    return cupcakes * guests + 13

In [ ]:
cupcake_tally(4, 15)

Note that we have 2 parameters now: cupcakes and guests

Our function is called and passes in two values of 4 and 15. It returns 73.

Summary: What are functions used for?

Program decomposition, or factoring

To break down a problem into parts. It makes the code more readable and is easier to understand.

Code reuse

Functions can be used instead of repeating the same lines of code at different times throughout a program. This reduces duplication of code.

Abstraction or simplification

Using functions allows us to hide all of the details involved and put them into one place and simply call the function to execute the lines of code.

Exercise: Why are functions useful?

First, create a new Python script and type in the following.


In [ ]:
print("On the day of Sept 20, 2014, were you at Intro to Python?")
answer = input("Answer truthfully, yes or no --> ")
if answer == "no":
    answer = input("Are you sure that you weren't? Tell the truth, now --> ")

print("Were you thinking of skipping the workshop to go to Sea World?")
answer = input("Answer truthfully, yes or no --> ")
if answer == "no":
    answer = input("Are you sure that you weren't? Tell the truth, now --> ")

Look up when you're done, so that we know to move on to the next step.

Was this annoying?

Raise your hand if you felt like this exercise was repetitive...TODO

Modules

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 random numbers. Let's import it so we can use some of its functions:


In [8]:
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 [10]:
random.randint(1, 6)


Out[10]:
2

Let's use the random integer function to simulate rolling a die in a game


In [12]:
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))


First die: 3
Second die: 3
You rolled a 6

choice is a function in the random module that returns a random item from a given list or string


In [13]:
random.choice('abcdefghijklmnopqrstuvwxyz')


Out[13]:
'm'

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 [14]:
def random_happy_birthday(names):
    name = random.choice(names)
    happy_birthday(name)

Now let's try it out with a list of names:


In [20]:
random_happy_birthday(["Alain", "Carol", "Rise", "Trey"])


Happy Birthday, dear Carol!

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 [11]:
from IPython.display import YouTubeVideo
# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar
YouTubeVideo('MirG-vJOg04')


Out[11]:

In [ ]:


In [ ]: