
Using Google Colaboratory for this Course
The assignments for this course rely on Google Colaboratory (Colab) notebooks. Each homework assignment and exam will have portions that require you to write your own Python code, and this code needs to be submitted in a Colab notebook, through Canvas (the Learning Management System for the course). Furthermore, examples in subsequent lessons are given in Colab notebooks. For these reasons, it is important to become familiar with how Colab notebooks work. Both Google Colab and Canvas are accessed online, so there is nothing you need to install. However, you do need to access course materials and assignments with your PSU Google Workspace account, so that your submissions are properly attributed to you.
Preliminary Task: Make sure your PSU Google Workspace account is setup
- Go to https://google.psu.edu/(link is external)
- Click Launch
- A Google sign-in screen should open. Login with your PSU email (e.g., xyz123@psu.edu(link sends e-mail)), and associated password. You may need to perform two-factor authentication.
- If you encounter a new window asking what sort of account to use, select Organization G Suite Account
- Otherwise, you should be at your Google Account home page, in which case your account is successfully set up.
Overview
The following videos demonstrate:
- How to access Google Colab through Canvas (for Assignments, including their submission)
- How to create your own Google Colab notebooks from scratch (in Google Drive)
- The basics of adding code to a Colab notebook
- How to add Text Cells to a Colab notebook
- What to do if your Colab session freezes or stalls
- The basics of functions in Python
1. How to access Google Colab through Canvas
This first video starts in Canvas and walks through accessing and submitting a mock assignment that uses Colab. For all assignments and exams, a template Colab notebook is given to you for you to add your work. The video also points out how to attach supplemental work. For example, you may find it easier to write the solutions to textbook problems on paper and scan that work. The scan can be attached to your submission as a separate document alongside your Colab notebook. The video also shows how to view feedback and comments on your graded work.
2. How to create your own Google Colab notebooks from scratch
For general-purpose use, outside of this course, you may find it helpful to develop your own Colab notebooks. For example, you may find Python a convenient tool for solving some problems in another course. A Colab notebook is an easy way to develop this Python code and display your results. The video shows how easy it is to make your own, blank Colab notebook through Google Drive. The notebook, ending with the extension ".ipynb
", is treated just like any other file in Google Drive.
3. The basics of adding code to a Colab notebook
Now that you know how to access or create a Colab notebook, let's get to the fun stuff: coding! The following video demonstrates how to add a "Code Cell" to the notebook. In this Code Cell, you will enter your Python code, and execute the code by pressing the "Play" button on the left side of the cell. It is important to bear in mind, especially as we progress to more complicated coding examples and assignments, that:
- The lines of Python code within a Code Cell are executed in order from top to bottom.
- The Code Cells are executed in the order in which you press the "Play" buttons (unless you do "Run All", which will run the cells in order from top to bottom in your notebook).
4. How to add Text Cells to a Colab notebook
Text Cells are very handy for providing an explanation of your work. Besides describing the steps you perform in your neighboring code cell, or interpreting the results of your code, Text Cells can also be used in this course to give your solutions to the textbook problems (if you prefer). You can even insert images into a Text Cell, which may be handy for some assignment problems which ask you to draw or sketch something. The following video gives a brief demonstration of some of these key features of Text Cells, and you are encouraged to explore other functionality of the Text Cells.
5. What to do if your Colab session freezes or stalls
Sometimes, you may unfortunately find that the code in your Colab notebook refuses to run or appears to be running but is taking an exceptionally long time. Often, this is due to an interruption of the connection to Google's servers (upon which your code is running). The following video shows you how to reset this connection, through a few different approaches you can try. In summary, we recommend trying these in order until your code is working again:
- Go to Runtime menu --> Restart Runtime (or Restart and Run All, if you want to also execute all your Code Cells in order)
- Save your Colab notebook and restart your browser (quit and open again)
- Save your Colab notebook and restart your computer
- Save a copy of your Colab notebook, as a separate file, and continue your work in that. Make sure to then attach this new notebook to your assignment submission.
6. The basics of functions in Python
A previous video introduced you to the print
function and did not provide much explanation about how you would know how to use that function. This video provides some more general background on functions in Python (you will be introduced to many throughout this course). The typical format of a Python function follows:
function_name(argument_1, argument_2, ..., argument_n, parameter_1 = default_value_1, parameter_2 = default_value_2, ..., parameter_n = default_value_n) |
Different functions may only take a certain number of arguments, or some can take any number (such as print
). Often, "argument" and "parameter" are conflated and used interchangeably. Either term indicates information that is passed into the function.
Summary
You now have the ability to get started using Google Colab notebooks and developing your own Python code. The Python and Colab Practice assignment in Canvas is a non-graded assignment that gives you the opportunity to practice accessing and submitting an assignment, so you can see for yourself how that works without fear of having a mistake that costs you points. You can also experiment with Python code and Text Cells in this practice assignment.