How to Use Google Colab for Python (With Examples) | JC Chouinard

How to Use Google Colab for Python (With Examples) | JC Chouinard

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.

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

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

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

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

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 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 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.

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 at the top of the notebook or below any cell that you hover with your mouse, or by clicking on Cell from the menu.

Then use the Markdown syntax to annotate your document.

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

Or even search through the files using .

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 is the Python package installer that allows to install Python libraries.

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:

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.

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”.

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

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

Magic commands can run in one of two ways:

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”.

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.

Images Powered by Shutterstock