

Everything you need to know to get started with Google Colab. No setup required, get coding from your phone.
Google Colab is a tool that allows you to write, run and share Python code in your browser. Colab also has multiple features that make it a popular tool for data science, machine learning and artificial intelligence. Many of the machine learning tutorials you may see online, are written in Google Colab, and this is for a good reason. When you write your machine learning code in Google Colab, you can share a link to it and anyone can have access to that code, run that code on their own browser, without the need for any setup.
If you are familiar with the Jupyter Notebook, you can think of Google Colab as a supercharged version of the Jupyter Notebook, hosted on Google’s cloud servers, with multiple convenient features. And if you are not familiar with it, do not worry, because this tutorial does not require any prior knowledge on Jupyter Notebook.
To get started with Google Colab, you can either google it or, for your convenience, you can also click the link below to get started:
https://colab.research.google.com/
Once you head into the website, it will give you an option to create a new notebook, along with displaying a “Welcome to Colaboratory” notebook. Before we go any further, one thing you should know is that a “notebook” is an integral part of Google Colab. We will write and execute our Python code inside notebooks. All the notebooks you create will be saved to your Google Drive, therefore you can access and share them from your Google Drive, just like you would share other documents. As a result, you can start coding in your browser, from your laptop, tablet or phone, without any setup and all of your work is shareable with a simple link, and is secure and private on your Google Drive with the rest of your files.
Let’s first start by clicking on “new notebook”. This will create a new notebook for you with a name such as “Untitled0.ipynb”. You can go ahead and change that name to anything you like, such as “ColabPractice”. You don’t have to change the name, and by default, Colab will name all your notebooks. But it can be helpful for you to name your notebooks, since more often than not, you may be using multiple notebooks at a time. Now you should be seeing something like this:
In Colab, all the code you write will be in “code cells”. There are two types of cells in Colab, one is code cells, and the other is called “text cells”. You can use the text cells to add comments to your code as well as to display images, videos, headlines, lists and more. The language used in the text cells is called “markdown”. If you are interested, you can learn how to write markdown text, but it is not required as you are just getting started with Google Colab.
Now, you should be seeing a single code cell in your notebook, which has a play button icon on its left hand side. Let’s go ahead and click on that code cell and type a simple python code. It can be something like this:
The most straightforward way to run this code is to click on the play button on the left side of the code cell. Let’s go ahead and click on that. The outcome should look like this:
Notice that the first time you run your code in Google Colab, it will take couple of seconds. This is because your notebook is being connected to a Google server which will run your code.
Once you run your first code cell, the rest of your code will run much smoother as long as you are connected to a Google server. You can see your connection status on the right-hand side of the notebook. The very first time you open a notebook it will say “connect”. As you are running the code, it will turn into “allocating”, “connecting” and “initializing” within a couple of seconds and it will display the “RAM” and “Disk” space you have, once you are connected.
What happens behind the scenes is that, basically Google allocates a computer for you, at least a part of it, and as long as you are connected, all your code will run in that server. When you leave your notebooks unused for hours, you might notice that you will be disconnected. Don’t worry, your code will remain unchanged, however, your variables and states in the current runtime will be lost, meaning that to continue from where you left off, you can run your code cells again, either one by one or by going to “Runtime” on the top left side of your notebook and click on “Run all”.
In Colab, there are 3 types of runtimes you can connect to, meaning that the code you write on your browser will be executed on the Google cloud servers. Some of these servers are better at running machine learning code, due to the hardware they have. That is why, according to our needs, we may want to connect to a specific kind of server.
The first one and the default one is a CPU runtime. CPU stands for Central Processing Unit. This is a go to one if you are working with Python or if you are working with a small machine learning model. But as you start building more complex or larger machine learning models, the computation time for your models will increase and you will probably want to use some kind of “accelerator” for your code to run faster. That’s where the second and the third runtime types step in.
The second type of runtime uses GPUs. GPU stands for Graphics Processing Unit. Although the GPUs were initially designed for better graphics performance in videos and video games, they became the default way to run machine learning code due to their efficiency and effectiveness. This basically means that your machine learning code will run faster when you use GPUs, because the type of calculations you perform in your machine learning code can be done much faster with GPUs.
The third type of runtime option you can select to run your code is TPUs. TPU stands for Tensor Processing Unit. These processing chips are developed by Google for the purposes of dramatically accelerating machine learning code.
To change the runtime type of your code, you can click on “Runtime” which is on the top left corner, then click on “Change runtime type” and from there, you can change your runtime type to GPU or TPU depending on your computational needs.
Notice that, when you change the runtime type from the Default to a GPU or TPU, there is a note that basically says: only use a hardware accelerator if you need a harware accelerator, to get the most out of colab. This way, Google can distribute its resources more efficiently for those who need more computational power.
To create another code cell, you can hover your mouse on the bottom of a code cell, and it will give you two options, “+ Code” and “+ Text”. If you click on the “+ Code” it will create another code cell for you below the current cell. Chances are, however, most of the time you will want to use the keyboard shortcuts. To run the current code cell, you can use “Command + Enter” on Mac or “Control + Enter” on Windows. To run the current code cell and move to the next cell, you can use “Shift + Enter”. More often than not, you can find yourself running your codes with “Shift + Enter”, because quite often you will want to not just run a single cell, but run the code cells sequentially, from top to bottom.
When you click on a code cell, you will notice multiple symbols appearing on its right hand side. With the delete icon, you can delete the code cell, and you can move the code cells up or down with the up and down arrows.
Whenever you are learning from a Colab tutorial, you will often want to get a copy of that notebook, so you can experiment with that code yourself. To do that, you can click on the “File”, and select “Save a copy in Drive”. This will create a copy and save that notebook to your Google Drive, and you will be able modify and run that code and make it your own.
If you want to use GitHub, you can also do that from “File” and you will have options like “Save a copy in Github”. This will take you to a Github sign in page and once you are signed in, you will be able to use GitHub right from Google Colab.
If for some reason, you want to download your notebook from Colab and work with it locally on your computer, you can do that as well. You can go to “File” and select “Download .ipynb” or “Download .py”. One thing you should know is that when you are writing Python code in Colab, you are working with an “.ipynb file”, which is a ipython notebook file.
An “.ipynb” file is like a Python file with additional functionalities that makes it more convenient to work with data science and machine learning code. Some of the major differences of an “.ipynb” file is that it allows you to use code cells that you can move around and you can save the outputs of your code cells. This can be very helpful with visualization of your data in data science and machine learning.
An “.ipynb” file is the same file type you would use in a Jupyter notebook. So if you want to work with your code locally in your machine, you can do so using Jupyter Notebook environment and the “.ipynb” file you downloaded from Colab.
If you are using Colab from a chrome browser, you can also connect to a local runtime by clicking the downward arrow right next to the “RAM” and “Disk” usage bars on top right side of your notebook.
All the major machine learning and data science libraries are pre-installed in Colab. Therefore, when you want to use major libraries like TensorFlow, PyTorch, Numpy, Matplotlib, Pandas, Seaborn, you can start using them with import statements such as the ones below:
import tensorflow as tfimport torchimport numpy as npimport matplotlib.pyplot as pltimport pandas as pdimport seaborn
Every now and then, you may also need to install different libraries to Colab. You can do this by prefixing your pip install statement with an exclamation mark such as:
!pip install gym
Once you install the libraries you want, you can now import them and start using them with an import statement as below:
import gym
If you click on the Colab icon on the top left corner of your notebook, it will take you to your Google Drive, since this is where all your Colab notebooks are saved. You can find all your Colab notebooks under My Drive -> Colab Notebooks. To open any previous notebook, you can right click on them and choose Open with -> Google Colaboratory.
These are all the basics you need to get started with coding in Colab. Since it requires no setup, you can start creating your machine learning models from your phone, tablet or laptop. As long as you have an internet connection, you are good to go.
I hope you got some value out of this article. If you have any questions, comment them below and I will get back to you as fast as I can. If your question requires a more extensive explanation, I can explain that in another article or a video, addressing your question. If you are a visual learner by the way, I also have this article in the video format here: