How to Use Google Colab for Python (With Examples)

Google Colab is a free Jupyter notebook that allows to run Python in the browser without the need for complex configuration. It comes with Python installed and has all the main Python libraries installed. It also comes integrated with free GPUs.

In this tutorial, we will cover everything that you need to get started using Python with Google Colab.

Google Colab is truly the fastest way to start using Python on any computer.


Subscribe to my Newsletter


What is Google Colab?

Google Colab is a browser-based product created by Google Research that allows to write and execute Python code without specific configuration.

How to Install Python in Google Colab?

Python comes pre-installed in Google Colab. You can start using Python in Google Colab straight away.

Useful Keyboard Shortcuts in Google Colab

CommandShortcut
Create cellCommand/Ctrl+M+B
Select all cells⌘/Ctrl+Shift+A
Run all cells⌘/Ctrl+F9
Run the selected cellCommand/Ctrl+Enter
Interrupt executionCommand or Ctrl+M+I
Useful Google Colab shortcuts

How to Run Python Code in Google Colab?

Google Colab is a notebook, meaning that you can run code or markdown instantly in any cell.

You can create a new cell in Google Colab by pressing on + Code at the top of the notebook or below any cell that you hover with your mouse, by clicking on Insert + Code Cell from the menu, or by using the ⌘/Ctrl+M+B keyboard shortcut.

Then, to run Python code, just add code in the cell and press the play button at the left of the cell. This will run the IPython for the selected cell.

Make Your Notebook More Interesting With Markdown

One of the interesting things about Jupyter Notebooks is that they allow to surround your code with relevant documentation in a digestible format.

The way to do that is by using Markdown.

To open a new markdown cell in Google Colab by pressing on + Text at the top of the notebook or below any cell that you hover with your mouse, or by clicking on Insert + Text cell Cell from the menu.

Then use the Markdown syntax to annotate your document.

Which makes your documentation visually compelling.

How to Know What Packages are Already Installed in Colab?

Google Colab comes with pre-installed Python libraries.

You can check what the full list of packages that are already installed in Google Colab by using pip list.

!pip list -v

Or even search through the files using grep.

!pip list -v | grep tensorflow

How to Install Python Packages in Google Colab?

Installing a Python package in Google Colab is simple using the pip command along with the exclamation mark (!).

The exclamation mark at the start of a cell allows to run a shell command, and pip is the Python package installer that allows to install Python libraries.

!pip install advertools
!pip install Google colab
!pip install Google colab

Explore Your Colab Environment

Before we go further, let’s look at how to explore your environment in Google Colab.

On the left panel there are quick links that allow you to view:

  • Table of content: Table of content showing the Markdown headings
  • Find and replace: Find and replace any string or regex from the entire file
  • Variable inspector: Show all variables that are stored
  • File explorer: Files and directories available from Colab. This is where you’ll view the files of a mounted drive
  • Code snippets: Pre-built reusable code snippets
  • Search commands: Search box of the commands available from the menu
  • Terminal: In the pro version you can get access to the runtime’s terminal

Connect to Google Drive into Google Colab?

You can connect to Google Drive from Google Colab so that you can use the files already stored or even store the result of your scripts. To use the files from Google Drive into Google Colab, you need to first mount your drive.

from google.colab import drive
drive.mount('/content/drive')

An overlay will ask you to permit the notebook to access Google Drive files. You will need to click on “Connect to Google Drive” and follow the prompts to give access to your Google Drive.

Your files will be listed in the following directory: “content > drive > MyDrive”.

How to Run Magic Commands?

You can run IPython enhancements in Google Colab by running magic commands.

Examples of things that you can do with the magic commands:

  • Show all magic commands: %lsmagic
  • Run a file: %run filename.py
  • Time the execution of a cell: %%time
  • Display Matplotlib: %matplotlib inline
  • Get and set environment variable: %env
  • Better formatting of Pandas Dataframe: %load_ext google.colab.data_table

Magic commands can run in one of two ways:

  • Single line: using the % prefix
  • Entire cell: using the %% prefix

Google Colab for Machine Learning

Get Free GPUs

Google Colab provides free GPUs to speed up the training of your machine learning models.

GPUs, or Graphics Processing Units, are useful in machine learning. They allow multiple parallel processing of calculations, which is useful when training large machine learning models.

To enable free GPUs in Colab, go to:

Runtime > Change Runtime Type and select the right Hardware accelerator.

GPUs are more expensive than CPUs and Google imposes limit on its use. If you don’t need GPUs, it is probably best to set the hardware accelerator to “None”.

How to Make Drive Directory on Google Colab

To create new Google Drive folder in the ‘Files’ section, mount Google drive and use !mkdir in the code shell:

!mkdir new_drive_folder

To create a drive folder at a specific location in your Google Drive, use the os module.

import os

if not os.path.exists('/path/to/folder'):
  os.mkdir('/path/to/folder')

Import .py Module in Google Colab from GitHub URLs

To import a single .py file from GitHub into Google Colab, use the python requests library and fetch the raw URL from Git. Then write the file to your computer and import it.

import requests

r = requests.get('https://gist.githubusercontent.com/canfixit/1662664/raw/f5f9db4f1b287804fd07ffb3296ed0036292bc7a/countryinfo.py')

with open('country.py', 'w') as f:
    f.write(r.text)

from country import countries

countries

Google Colab FAQs

What’s the Meaning of the Exclamation Mark in a Jupyter Notebook?

The exclamation mark (!) before a statement in Jupyter notebook is used to run shell commands from the underlying operating system.

What is Google Colab Used For?

Google Colab is used to run Python in a browser without the need of a complex implementation

Why use Google Colab?

Google Colab is a free and easy to use Jupyter Notebook that provide free GPUs, a valuable resource for machine learning tasks.

How do I use Google Colab Python Notebook?

Simply register for a Google account and get full access to Google Colab where Python is already preinstalled

Conclusion

We introduced Google Colab to help your get started with Python. If you would like to learn python, make sure that you see my Python SEO Tutorials.

5/5 - (6 votes)