In-Class Activity: Getting Started with Google Colab and Python

 


Introduction

The objective of this activity is to become familiar with the Google Colab programming environment and the language Python, which will both be used in this course.



Becoming familiar 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. Create a new folder called CS103Labs on your Google Drive where you will store your work for this class. Create an additional folder on your machine called CS103Labs. You will be saving work in both of these folders.
  2. Navigate to the following website: Colab.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.
  3. 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.
  4. We are now ready to write some new code in your file. Add a Text cell at the bottom of your file with this Exercise number. (The text in the cell should look something like "Exercise #4".) Then type the following command into a new Code cell at the bottom of your notebook:
    print(3 + 4)
  5. Now let's see what this does. Click on the triangle/arrow to the left of your Code cell. You should see the number 7 get printed out below the Code cell. If this does not happen for you, ask an instructor to see what happened.
  6. Experiment with some of the other arithmetic functions. Add the following commands into the same Code cell that you created in the previous exercise.
    print(3 - 4)
    print(3 * 4)
    print(3 ** 4)
    print(3 / 4)
    print(3 // 4)
    Include the results in your submission. Add a Text cell below your Code cell to detail whether the operations were expected. For those which were not, provide an explanation for what is occuring. Update the Text cell to include this exercise number.
  7. We can now add more commands to our program. Add a new Text Cell with this exercise number. In a new Code cell, type in the following commands from the Exercise at the bottom of page 1 of the reading: Using Google Colab To Program with Python.
  8. print(Hello class)
    print(Hello + class)
    print(34.1/46.5)
    print(1/2)
    print(1//2)
    Note: In Python you can use single or double quotes around strings.
  9. Run your program. Do you get the results that you expected? If not, please ask an instructor or TA for an explanation.
  10. Add a Text cell after this Code cell to explain what happened with this block of statements.
  11. When you get longer blocks of code, it is sometimes useful to include line numbers in Colab to make it easier to refer to particular statements. In Colab, go to Tools, then to Settings. In the Window that pops up, select Editor and then check the box to include line numbers. Then click Save. Do you see line numbers appearing in your file? If not, check with the instructor or a TA.

Working with image files

Some of the following exercises will have questions to be answered and turned in with this activity. You should include your answers in a Text cell as part of your program.
  1. In order to work with image files, you will need to have some images stored in the folder for this class on Google drive. Take a few minutes now to put some images in your folder. These could be your own images, images you download from the internet (without violating any copyright policies), or images you download from this MediaSources directory. (You are free to use these, and they are a nice (i.e., small) size for working with.)
  2. Now that you have some images you'd like to use, we need to give Google Colab permission to access them on your drive. Add the following two lines in a new Code cell at the beginning of your notebook to give your notebook access to files on your Google drive:
    from google.colab import drive
    drive.mount('/drive')
    Run this cell. Follow the prompts to permit your notebook to access your Google drive files.
  3. In order to work with images, we are going to use the PIL Python library. We will tell our program to use this library by adding the following statement in the same Code cell where you mounted your drive in the previous exercise.
    from PIL import Image
  4. To select an image to work with, we need to open it. Add a new Text cell at the bottom of your file with this exercise number. Then add a new Code cell at the bottom of your file, and put the following statement:
    myImage = Image.open("/drive/My Drive/CS103Labs/someImage.jpg")
    This statement would open an image named someImage.jpg that is located in a folder named CS103Labs on Google drive and stores it in a variable names myImage. Replace someImage.jpg with the name of one of your image files and replace CS103Labs with the name of the folder that contains your image. Click on the triangle to run the Code cell.
  5. At this point, you have not actually seen the picture you've chosen to work with. We have two ways to see the image. Try them both:
    • Add the statement myImage.show() in the Code cell after the statement that opens the image file. Run the cell again.
    • Add the statement myImage as the next line of code. Run the cell again. What do you see happening?
  6. Add a Text cell immediately following this Code cell and write a sentence or two in it to describe how to open an image file and show the image.
  7. Finally, let's practice saving the picture. In the Code cell where you opened and showed the image, add the following statement at the end:
    myImage.save("/drive/My Drive/ColabNotebooks/newPhoto.jpg")
    Replace myImage with the name of your image (if it's something different than this), replace ColabNotebooks with your folder name on drive, and replace newPhoto.jpg with whatever name you would like for your new image.
  8. Check that this new file is in your folder on your Google drive. If it is not there or you cannot find it, ask your instructor or a TA for help.

Submit

You will submit your program as a .ipynb file on Kit. When you are finished with this activity, add a Text cell at the very top of your program. In this Text cell add your name, the date, and a short (1 - 2 sentence) description of this activity. Now make sure all of your Code cells have been run. To do this, go to Runtime -> Run all. Then go to File -> Download and select Download .ipynb. Check to see that this file is now saved in your Downloads folder. If it is there, move it to your CS103Labs folder. (It is highly recommended that you create a separate folder on your machine for your work in this class.) You will then submit this .ipynb file on Kit.