In-Class Activity: Introduction to Google Colab and Writing Functions

 


Introduction

In this activity you will learn how to use Google Colab to write Python code. You will experiment with writing a few functions of your own.



Exercises

Getting Started with Google Colab

In this set of exercises, we will learn the basics of Google Colab, a tool for writing and executing code in your browser, with easy sharing among collaborators. To use this tool, you must have a Google account.

  1. To begin, go to the following website: Cogab.research.google.com/. You will need to sign in to your Google account if you have not already done so. In the window that pops up, click on the "Welcome to Colaborator" link. Read through the "Getting started" and "Data Science" sections.
  2. Work through Sections 1-5 of this Google Colab tutorial. At the end of these sections, you should have created a new notebook, added some code and text to it, and saved it to your Google drive.

Writing Functions

In this section we will explore how to define/write and call our own functions. You will add your functions to new code cells in the same notebook you created for the previous section of this activity.

  1. In a new Code cell, define a function called computeAvg that will ask the user for two numbers and then print out the average of those two numbers. In order to ask the user for two numbers, you should have two input statements, with the values that the user enters being stored in variables. You should store the result of the average calculation in another variable. For example, you may have something like the following as the first statement in your function:

    num1 = float(input("Please enter a number: "))
    to get a value from the user.

  2. Add another Code cell and put the call to your function in it. This should look something like computeAvg().

  3. Run these two Code cells to test that your function does what you expect.

  4. Returning a value: In the computeAvg function that you just wrote, the function printed the average. To make this function more useful, we will have it return the average instead of printing it. Comment out your print statement by putting a # symbol in front of it. Then add the following statement at the end of your function:

    return avg
    where avg is the name of the variable that holds the result of the average computation.

  5. Run the two Code cells again. Did you get the output you were expecting? Why or why not? (Hint: unless you already changed how you call the function, you won't see any ouptut.)

  6. In the second Code cell, modify the call to the function so that it stores the result in a variable and then prints it, such as:

    average = computeAvg()
    print("The average was: ",average)

  7. Using parameters(arguments): We will now generalize the function you just wrote so that it computes the average of any two numbers that get passed to it as parameters and will return the average of those two numbers. Add two parameter names, such as num1 and num2, separated by commas between the parentheses in the first line of the function definition. Modify your function by commenting out the user input statements. Print out the two numbers and then return the average.

  8. Modify the Code cell that tests the function by adding two actual numbers separated by commas in the call to the function, such as

    average = computeAvg(12, 15)

  9. Add two more lines in that Code cell to get user input for the two numbers and store those values in variables. Then call the function as you have done before, but pass in the user's values. This should look something like:

    val1 = float(input("Enter a value: "))
    val2 = float(input("Enter another value: "))
    average = computeAvg(val1, val2)
    print("Average is: ",average)

  10. Mad Libs: A Mad Lib is a fun word game, in which you construct a nonsensical short story - or in our case, just a sentence or two - by filling in random words in certain spots. See the Wikipedia article for more information.

    In a new Code cell, write a new function that takes three words, a noun, a verb, and an adjective, as parameters and displays a short story constructed using the words passed in to the function. Then in another Code cell, test your function by calling it with three random words. A sample session might look something like the following:

    Function call:
    madlib("monkey", "jump", "happy")

    Results:
    When I was walking to work today I saw a monkey!
    It was very happy and it made me want to jump.

    OR
    It's your turn to jump over the happy monkey.

  11. Test your function several more times by calling it with different words.

  12. Add a Text Cell at the top of your file with your name, the date, and a short description of this in-class activity.

  13. Enhancements/Challenges: (For fun, not required) Think about how you might write another function that solicits the three words from the user. Then write a third function that uses these two functions, getting the input from one function and passing it into the other function. What other ways could you improve on your mad lib?


Submit