How to run Python code on Google Colaboratory?

Google Colaboratory (Colab) is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud. It is hosted in Google Cloud and maintained by Google, allowing Python developers to write, run, and share code using a web browser. In this article, we will learn how to set up and use Google Colab for Python programming.

Accessing Google Colab

Navigate to the Google Colab website at https://colab.research.google.com/. You'll see the welcome screen with options to create a new notebook or open existing ones from various sources like GitHub, Google Drive, or upload from your computer.

Google Colaboratory NEW NOTEBOOK Open Upload Click "NEW NOTEBOOK" to create a Python notebook

Creating a New Python Notebook

Click on "NEW NOTEBOOK" or the "File" menu and select "New notebook". This creates a new Colab notebook with Python 3 runtime by default. The notebook interface includes code cells where you can write and execute Python code, and text cells for documentation.

Example: Basic Python Code

Let's start with a simple Python example. In a code cell, you can write basic Python statements ?

# Simple Python operations
print("Hello, Google Colab!")
x = 10
y = 20
result = x + y
print(f"The sum of {x} and {y} is {result}")
Hello, Google Colab!
The sum of 10 and 20 is 30

Using Python Libraries

Google Colab comes with many popular Python libraries pre-installed. You can import and use libraries like NumPy, Pandas, Matplotlib, and more ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create a simple plot
plt.figure(figsize=(8, 4))
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.grid(True)
plt.show()

Installing Additional Packages

If you need packages that aren't pre-installed, use the ! prefix to run shell commands ?

# Install a package
!pip install requests

# Import and use the package
import requests

response = requests.get("https://api.github.com/users/octocat")
print(f"Status Code: {response.status_code}")
print(f"User: {response.json()['login']}")

Running Code Cells

To execute code in a cell, you can ?

  • Click the play button (?) on the left side of the cell
  • Press Ctrl + Enter to run the current cell
  • Press Shift + Enter to run the cell and move to the next one
print("Hello World") Click play button or press Ctrl+Enter to run

Key Features of Google Colab

Feature Description Benefit
Free GPU/TPU Access to hardware acceleration Faster machine learning training
Cloud Storage Automatic saving to Google Drive Access from anywhere
Pre-installed Libraries Popular packages ready to use No setup required
Collaboration Share notebooks with others Real-time collaboration

Conclusion

Google Colab provides an excellent platform for Python development without any local setup requirements. With pre-installed libraries, free GPU access, and seamless Google Drive integration, it's perfect for learning, prototyping, and sharing Python projects.

Updated on: 2026-03-15T17:08:19+05:30

920 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements