How to Use Google Colab for Python

journey into python

In this post, we will talk about how to use Google Colab to learn how to code in Python. Google Colab is a fun, fast, and easy way to start coding in Python within seconds. The best part is the coding in Google Colab is free. So, what are you waiting for? Start coding Python in Google Colab now!

Start coding in python in less than 30 seconds

Ok, your time starts now, HURRY!

If you have a Google account click this link: Google Colab

Click “NEW NOTEBOOK” in lower right corner, select one of the cells, and type:
print("Helloworld")

Now hit Shift + Enter.

Yeah, that’s right, you can now code with nothing but a browser. Go on, start coding and come back if you want to learn more about Google Colab.

Why use Google Colab?

  • Easy
  • Fast
  • Keyboard Shortcuts
  • Similar to Jupyter Notebooks
  • Lots of features
  • And best of all… FREE!

What is Google Colab?

Colab stands for Colaboratory, a mash-up of Collaborate and Laboratory. Based on Jupyter, an open-source project that allows the execution of python code within a browser. This means that you can even code on a tablet or a Google Chrome book. Google Colab is cloud-based; it even allows you to save and share your notebooks in Google Drive. Already mentioned, but a big part of Google Colab is that it has most of the same features that Jupyter Notebooks. So what are Jupyter Notebooks?

What is Jupyter

  • Jupyter is an interactive data science and scientific computing across all languages
  • Allows the execution of python within your browser. Sound familiar.
  • Jupyter stores the code in notebooks and each notebook is separated into cells
  • Supports multiple languages
    • Started with Julia, Python, and R. This is where the name Jupyter came from
    • Now it supports nearly every language
  • Supports multiple kernels
  • Built on top of IPython
    • Supports Magic Commands
  • Notebook cells support HTML and Markdown
  • You can convert notebooks to standard outputs
    • Html, LaTex, Pdf, Rst, Markdown, and output the code itself
  • Can run bash commands within cells
  • Supports debugging

JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data. It is flexible: configure and arrange the user interface to support a wide range of workflows in data science, scientific computing, and machine learning. JupyterLab is extensible and modular: write plugins that add new components and integrate with existing ones.

Even though Jupyter was built for data science, it doesn’t mean that it can’t be used to learn python. It is a great tool to learn python! Your code is saved within a “notebook,” and notebooks are separated into cells that can be run independently. Your variables, functions, and classes are executed within the same kernel, meaning that they carry over to all cells below the executed cell.

Since Jupyter cells support HTML, it makes for a great teaching tool. A single notebook can have instructions as well as code. Supporting HTML makes for a very interactive teaching tool. What is also great is that it supports more than just the python language. It initially supported Julia, Python, and R, hence the name Jupyter. There is now support for most languages. Some examples are go, c++, and java

Google Colab Shortcuts

Shortcuts I use the most:

DescriptionShortcut
Run current cell and create/select cell belowShift + Enter
Run current cellCtrl + Enter
Run current cell and create new cellAlt + Enter
Run all cells in notebookCtrl + F9
Create cell aboveCtrl + M A
Create cell belowCtrl + M B
Delete cellCtrl + M D
Make a cell into textCtrl + M M
Command PalleteCtrl + M H

The last command will list all available shortcuts.

Using magic commands within Google Colab and Jupyter Notebooks

Colab uses the Ipython kernel, which supports magic commands. Magic commands use a syntax element not found in the chosen underlying language. This ensures that there is no conflict during execution and allows for an additional set of functionality.

Most useful magic commands

  • Displays and sets environment variables
    • %env
  • Enter debug console
    • %debug
  • Sets an automatic debugger on exceptions
    • %pdb
  • Load code
    • %load
  • Run a python script
    • %run

Using markdown within Google Colab

Colab text cells support HTML and Markdown. You can use the “Ctrl + M M” shortcut to turn a cell into text. Markdown is a simple plain text format converted to HTML when you run the cell. Markdown supports headers, unordered lists, ordered lists, tables, italicized, bold, links, images, and more. Below are the Markdown syntaxes that I use the most. A complete guide found at MarkdownGuide.Org

Markdown headers

# Header 1: Big
## Header 2: Medium
### Header 3: Smaller
#### Header 4: Just keeps getting smaller

Markdown lists

- Unordered Lists
    - One
    - Two
        - Three

1. Ordered Lists
    1.1 One
    1.2 Two
        1.2.1 Three

Markdown bold and italicized

**This is bold**
*This is italicized*

Add charts and graphs in Google Colab and Jupyter Notebooks

Google Colab supports chart and graph visualizations like bar, pie, line, scatter, and more. Some of the popular visualization packages are Matplotlib, Seaborn, and Plotly. Here is an example of how to use Seaborn to display a cell chart.

The code here can be found at the Seaborn website

import seaborn as sns
sns.set_theme()

# Load an example dataset
tips = sns.load_dataset("tips")

# Create a visualization
sns.relplot(
    data=tips,
    x="total_bill", y="tip", col="time",
    hue="smoker", style="smoker", size="size",
)
Visualize Data with Google Colab

How to install additional packages in Google Colab and Jupyter Notebooks

Using Bash Command

Colab runs on top of a Unix operating system. Unix bash commands can be exectuted from within a notebook.

!pip install package1 package2

Using Ipydeps

Note: Ipydeps package must be installed prior to use

We love using Ipydeps; it is a great way to share notebooks without worrying about dependencies. As long as Ipydeps is pre-installed, it can handle all additional packages needed to get a notebook up and running.

import ipydeps
packages = ['package1', 'package2']
ipydeps.pip(packages)

Conclusion

Google Colab is a fun, fast, free, and easy way to start coding in Python. Start practicing now!

TL;DR Then watch a video

How to use Google Colab

Additional References:
Google Colab FAQs: here

Leave a Comment