Google Colab is amazing and you already have it, if you have a Google or G-mail account, so there is no installation necessary.
Plus; it’s all completely online, tucked away inside your Google Drive, allowing you to save and share documents from anywhere.
Colab is actually very powerful online software that is used by professional Python coders in many high end fields such as Data Science, AI and Machine Learning.
It is also very easy to use, making it perfect for curious enthusiasts such as yourself, looking to explore the vast universe of coding.
Google Colab is actually based on another software called Jupyter Notebook, which serves the same purpose, but that too requires you to install it locally on your computer.
The main difference is that when you install a program such as Jupyter Notebook locally, it uses your computer’s hardware, (especially your graphics card) for simulations.
So; if you have an old or slow computer, your code is also going to run very slowly as well, especially as the code gets more complex. We won’t ever reach that limitation in this course, but I wanted to simply make you aware of this.
With that said; you are more than welcome to install Jupyter notebook if you prefer, but I will be using Google Colab for this course.
The benefit to using Colab is that it’s an online based platform, which runs on Google’s ultra fast cloud servers. This means that their computer will handle all the calculations for you. So, no matter how old or slow your computer is, as long as you have a working internet connection, you have access to these super fast computers to do all the heavy lifting in running your code for you.
And it gets even better.
Because it’s online, linked to your google account, it means that you can use Google Colab on any device; even your smartphone. As this again doesn’t use any of your phone’s hardware, you can write and run code just as fast as someone on a laptop or desktop computer.
Plus; you’re mobile, so you can practice your code and finish this course from anywhere; at any time.
Amazing! Isn’t it!
Well now that I’ve completely sold and convinced you as to why Google Colab is as hot as a California reaper chili, let’s take a look at how to access it.
Access Google Colab
- To access Google Colab, simply open your web browser and go to the the official website at colab.research.google.com
- If you’re not already signed in to your Google account, click the sign in button.
- If you don’t have a Google account, you’ll need to create one.
Create a New Notebook
Once you’re signed in, you should see a welcome popup window appear.
Inside it is a default Colab file created by Google.
If you click and open it, you will see that it contains an introduction to some of the many features available in Colab.
While you don’t have to read this, I encourage you to at least skim over it, as it will help you understand what it is capable of. Some topics covered there are a bit advanced for a complete beginner, so don’t worry about it too much if you start feeling lost and confused.
You however don’t need it to do this, in order to get started with this course.
If you didn’t open the file, you will see that the popup window also gives you the option to create a new notebook file.
Click on that to create a new Colab notebook. If you already closed the popup, don’t worry.
You can easily create a new notebook by clicking on `File` > `New notebook` from the top menu.
A new browser tab will open with your new notebook. At the top, you can click on `Untitled Notebook` to rename it to something else, but that’s not necessary.
Understand the Notebook Interface
Once it opens you will see that the notebook is divided into cells, of which there are two types.
The `Code` cell and the `Text` cell.
Every new notebook comes with a “code” cell by default.
Code cells are where you write and run your code; while Text cells are for writing notes, explanations, or anything else you want to include with your code that isn’t code itself.
To add another cell simply hover under this code cell and you will see two buttons appear.
Clicking the “+ Code” button will create another code cell below the current one, and in the same way, clicking the “+ Text” button will create a new text cell.
You can also add new cells by clicking:
- `Insert` > `Code Cell` or
- `Insert` > `Text Cell` from the top menu.
You can also move cells up and down by clicking on the cell; and then clicking the up or down arrows in the top right corner of the cell.
Write and Run Code
To run any code, we first need to connect this notebook to a dedicated cloud server at Google.
This is easily done with a simple click of the “Connect” button on the top right of the notebook.
After a few seconds you will see it change to an image with a green check mark and the words RAM and DIsk next to it.
- “RAM” stands for Random Access memory, which is simply the memory allocated to this notebook from the server. Your computer and phone also have ram in them as its required for it to function.
- “Disk” is simply a hard drive or disk space allocated to it as well, allowing you to upload files to this notebook.
For interest’s sake; If you click on this “RAM” and “Disk” button, you will see a breakdown of the allocations.
Now that it’s connected we can run some code.
Click on a code cell to start writing code. Google Colab supports Python and its libraries, as I’ve mentioned before.
For illustrative purposes I am going to type out a variable, assign it a data type and create a print statement. Don’t worry; I am going to be teaching you what all that means in this course.
After writing your code, you can run the cell by clicking the play button on the left side of the cell or by pressing “Ctrl + Enter” on your keyboard.
“Control and Enter”
The results of the code will be displayed directly below the cell. That is really how easy it is to create code in Google Co-lab.
You can also connect and run any code by clicking the `Runtime` menu, from the top menu bar.
This gives you more controlled options, such as to run only selected cells, run all the cells, or run cells before and after a selection.
We won’t be using these but I just wanted to let you know that they exist.
We can also disconnect or restart the runtime from this dropdown menu, or by clicking the arrow next to the RAM and Disk button, which gives the same options.
Save and Share Your Notebook
Google Colab also automatically saves your notebook to Google Drive, into a custom created folder called “Colab Notebooks”, as you work.
You can also manually save by clicking `File` > `Save`; or “Ctrl + S” on your keyboard.
If you want to share your notebook, click on the `Share` button in the top right corner of the screen.
You can then enter the email addresses of the people you want to share with, or you can get a shareable link.
There are many more things you can do with Colab, but this is all you need to know, in order to complete this course.
Here is a link to a more in-depth tutorial on my website if you are interested in learning more.
Great. Now that you are set up, let’s start learning some python!
Import and Use Data
1. You can import data into your Google Colab notebook from your Google Drive, your local machine, or from a URL.
2. To import from Google Drive, you can use the `drive` library from `google.colab`. Here’s an example of how to do it:
from google.colab import drive
drive.mount(‘/content/drive’)
3. After running this code, you’ll be given a link to authorize Google Colab to access your Google Drive. Once you’ve authorized it, you can access your files from the `/content/drive/My Drive/` directory.
4. To import a file from your local machine, you can use the `files` library from `google.colab`. Here’s an example:
from google.colab import files
uploaded = files.upload()
5. After running this code, you’ll be prompted to select a file from your local machine. The file will then be uploaded and can be accessed in your notebook.
Use GPU/TPU
1. Google Colab provides free access to GPUs and TPUs. To use them, click on `Runtime` > `Change runtime type`.
2. In the pop-up window, under `Hardware accelerator`, select `GPU` or `TPU`.
3. Click `Save`. Your notebook will then have access to the selected hardware accelerator.
Remember, Google Colab is a powerful tool that can be used for a wide range of tasks, from data analysis to machine learning. This tutorial just scratches the surface of what you can do with it.