Google Colab for Python: Setup, Files, Drive and Projects

Google Colab features for running Python notebooks

Google Colab is a browser-based notebook environment for running Python code without installing Python on your local computer.

A Colab notebook can contain Python code, text, tables, charts and output in the same .ipynb file. Notebooks can also be stored in Google Drive and shared with other users.

Colab is useful for Python learning, Pandas and NumPy exercises, data analysis, machine learning experiments, API projects, charts, database work and other notebook-based applications.


What Is Google Colab? 🔝

Google Colab, short for Google Colaboratory, provides a notebook environment that runs Python code in a cloud-based runtime.

Instead of creating a local Python environment first, a beginner can open a notebook in a browser, add a code cell and start running Python.

A notebook can contain:

  • Python code;
  • formatted explanations;
  • Pandas DataFrames;
  • NumPy arrays;
  • Matplotlib charts;
  • images;
  • uploaded files;
  • database queries;
  • interactive widgets;
  • API requests and JSON data.

Popular Python libraries such as Pandas and NumPy are commonly available in the Colab environment and can be imported directly.

Advantages of Google Colab for Python 🔝

  • No local Python installation: Python code runs inside the Colab runtime through the browser.
  • Notebook format: Code, output and explanations can remain together in one .ipynb file.
  • Google Drive integration: Files stored in Drive can be accessed from notebooks after mounting the drive.
  • Sharing: Notebooks can be shared with other users.
  • Python libraries: Many commonly used packages are already present, and additional packages can be installed when required.
  • Hardware runtimes: Depending on runtime availability and account settings, Colab can provide different compute options such as CPU, GPU or TPU.
  • Useful for learning: Beginners can focus on Python code before setting up a complete local development environment.
  • Useful for data projects: Pandas, NumPy, Matplotlib, SQLite, APIs and notebook widgets can be combined in one project.

Start a Google Colab Notebook 🔝

A Colab notebook can be created from Google Drive.

New > More > Google Colaboratory

If Google Colaboratory is not listed, use:

New > More > Connect more apps

Search for Google Colaboratory and connect it to Drive.

Create Google Colab notebook from Google Drive

Existing notebooks can also be opened or uploaded through Colab.

Open upload or import notebook in Google Colab

Google Colab Python Tutorial for Beginners

Run Your First Python Code 🔝

Create a code cell and enter:

print('Hello from Google Colab')
Output
Hello from Google Colab

Variables, loops, functions and other Python code work in the same way as in a normal Python environment.

name='Alex'

print(
    f'Welcome {name}'
)

Check the Python Version in Colab 🔝

Use this shell command inside a code cell:

!python --version

The displayed Python version depends on the current Colab runtime.

Install and Check Python Packages in Colab 🔝

Many libraries are already installed.

To view installed packages:

%pip list

To inspect one package:

%pip show pandas

To install a package:

%pip install package_name

For example:

%pip install pytrends

Check Whether a Library Can Be Imported

try:
    import reportlab
    found=True

except ImportError:
    found=False

print(found)

The try and except statement allows the program to handle a missing package without immediately stopping.

Understanding Files in a Colab Runtime 🔝

Files stored directly in the Colab runtime are temporary.

For example, a downloaded file may appear under:

/content/

These runtime files can disappear when the runtime is reset, disconnected or replaced.

Upload and Download Files Between Your Computer and Colab 🔝

Upload a File

from google.colab import files

uploaded=files.upload()

The returned uploaded object contains the uploaded file data.

Download a File

from google.colab import files

files.download(
    'fish-market.jpg'
)

Create a Text File and Download It

from google.colab import files

with open(
    'data1.txt',
    'w'
) as file:

    file.write(
        'This text was created in Google Colab.'
    )

files.download(
    'data1.txt'
)

Mount Google Drive in Colab 🔝

Files that must remain available after the current runtime ends can be stored in Google Drive.

Mount Drive using:

from google.colab import drive

drive.mount(
    '/content/drive'
)

Colab will request permission to access the selected Google Drive account.

After authorization, Drive files are normally available below:

/content/drive/MyDrive/
Mounted Google Drive files inside Google Colab

The Files panel can be used to browse folders and copy the path of a required file.

Read CSV and Excel Files from Google Drive 🔝

After mounting Drive, Pandas can read CSV and Excel files directly.

Read an Excel File

import pandas as pd

path='/content/drive/MyDrive/Colab Notebooks/downloads/my_file.xlsx'

my_data=pd.read_excel(
    path
)

print(
    my_data.head()
)

See the detailed Pandas read_excel() tutorial.

Read a CSV File

import pandas as pd

path='/content/drive/MyDrive/Colab Notebooks/downloads/student.csv'

my_data=pd.read_csv(
    path
)

print(
    my_data.head()
)

See the Pandas read_csv() tutorial.

Upload Excel or CSV Files to Google Drive and Read Them from Colab

Download Files from a URL Using wget 🔝

A file available through a direct URL can be downloaded to the current Colab runtime with wget.

!wget https://www.plus2net.com/python/download/my_db.db

Other examples:

!wget https://www.plus2net.com/python/download/student.xlsx

!wget https://www.plus2net.com/python/download/student.csv

These downloaded files belong to the current runtime unless you copy them to persistent storage such as Google Drive.

Useful Google Colab Commands 🔝

Colab supports normal Python together with shell commands and notebook magic commands.

Command Purpose
%pip install package Install a Python package in the notebook environment
%pip list List installed Python packages
%pip show package Display information about a package
!python --version Display the Python version
!pwd Display the current working directory
!ls List files and directories
%cd path Change the notebook working directory
!mkdir folder Create a directory
!cp source destination Copy a file
!mv old new Move or rename a file
!rm filename Remove a file
!wget URL Download a file from a URL
!curl URL Retrieve content using curl
!nvidia-smi Display NVIDIA GPU information when a compatible GPU runtime is active
%%time Measure the execution time of a cell
%%capture Capture or suppress cell output
!python script.py Run a Python script file

Interactive Widgets in Google Colab 🔝

The ipywidgets library can add sliders, buttons, dropdown lists, checkboxes, text inputs, file uploads, progress bars and other interactive controls to notebook interfaces.

For example:

import ipywidgets as widgets
from IPython.display import display

slider=widgets.IntSlider(
    value=50,
    min=0,
    max=100,
    description='Value:'
)

display(slider)

The dedicated tutorial covers interact(), observe(), on_click(), layout containers, file uploads and linked widgets.

Python ipywidgets in Google Colab

Python Projects You Can Run in Google Colab 🔝

Once the basic Colab workflow is understood, the same notebook environment can be used for larger Python projects.

Mutual Fund NAV Tracking with Python and MFAPI

This project uses Python Requests to retrieve the latest available mutual fund NAV through MFAPI. It then expands the same workflow to multiple schemes, historical NAV charts, portfolio valuation, Excel input and scheduled automation.

Mutual Fund NAV Tracking with Python and MFAPI

Interactive Data Entry with ipywidgets

Build notebook forms with text boxes, dropdowns, buttons and other interactive controls.

Data Entry Form with Colab and ipywidgets

SQLite Database Projects

Colab can connect to SQLite database files and use them together with Pandas.

SQLite Database Using Google Colab

Create PDF Files

Python libraries can generate PDF files from notebook data and database content.

Generate PDF Files in Google Colab

Gemini API Projects

Secrets and API calls can be combined to create AI-based Python notebook projects.

Using Gemini API in Google Colab

AI Features in Google Colab 🔝

Supported Colab environments can include AI-assisted features for working with code and data.

Depending on the available interface and account features, these tools can assist with tasks such as:

  • generating Python code from natural-language instructions;
  • suggesting code completions;
  • explaining code;
  • helping interpret errors;
  • generating data analysis steps;
  • creating charts from data;
  • suggesting missing imports or code corrections.

Because Colab features can change over time, the exact buttons and interface available in a notebook may differ.

Google Colab AI suggesting Python code changes

Using the Colab Data Science Agent for Data Analysis

Ask Questions About Data

The Plus2net example notebook demonstrates using an AI data-analysis workflow with the student database exercise.

Open the Data Analysis Notebook

Using SQLite in Google Colab 🔝

SQLite is a file-based database, so a database file can be stored in Google Drive or uploaded to the current Colab runtime.

Python can then connect to the SQLite database, run queries and transfer data between SQLite, Pandas, Excel and other formats.

Use SQLite Database from Google Drive with Colab

Pandas DataFrame to SQLite Table

Display Images from SQLite in Colab

Transfer Data Between SQLite and Excel in Colab

Create SQLite Tables Using Dates

Creating PDF Files in Google Colab 🔝

Python libraries can create PDF documents in a Colab notebook using text, tables, images and dynamic data.

The PDF can then be downloaded to the local computer or saved to Google Drive.

Generate PDF Files in Google Colab

Introduction to Google Colab for Python

Using Secrets in Google Colab 🔝

API keys, passwords and other credentials should not be written directly into notebook code that may be shared.

Google Colab provides a Secrets area where sensitive values can be stored separately from the Python cells.

Google Colab Secrets feature

Add a secret name, such as:

OPENAI_API_KEY

and store its value in the Secrets panel.

Adding API key to Google Colab Secrets

Read a Secret in Python

from google.colab import userdata

api_key=userdata.get(
    'OPENAI_API_KEY'
)

Store the Secret in an Environment Variable

import os
from google.colab import userdata

os.environ['OPENAI_API_KEY']=userdata.get(
    'OPENAI_API_KEY'
)

Notebook access to a stored secret should only be enabled for notebooks that need it.

How to Store API Keys and Passwords Securely in Google Colab

Using Gemini API in Colab Gemini Image Projects Using Kaggle API in Colab

Google Colab Summary 🔝

  • Google Colab runs Python notebooks through a browser-based runtime.
  • A notebook can contain Python code, explanations and output in the same .ipynb file.
  • No local Python installation is required to start using Colab.
  • Use !python --version to check the active Python version.
  • Use %pip install to install additional Python packages.
  • Files stored directly in the runtime are temporary.
  • Use files.upload() and files.download() for local file transfer.
  • Mount Google Drive when files need persistent storage.
  • Pandas can read Excel and CSV files stored in Drive.
  • wget can download files directly into the runtime.
  • ipywidgets can add interactive controls to notebooks.
  • SQLite databases can be used directly from Colab.
  • Python can create PDF files inside the notebook environment.
  • Secrets can keep API credentials outside notebook source code.
  • Colab can be used for larger projects such as API automation, data visualization and mutual fund NAV tracking.
ipywidgets Mutual Fund NAV Project SQLite in Colab PDF in Colab

Using .ipynb Files as Modules and Docstrings Download and Install Python Python IDEs Learn Python Basics




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer