Get AI summaries of any video or article — Sign up free
Using ChatGpt in Google Colab | Learn python with chatgpt and Google Colab thumbnail

Using ChatGpt in Google Colab | Learn python with chatgpt and Google Colab

5 min read

Based on SABIYA'S PROGRAMMING SCHOOL's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Install the “ChatGPT for Google Colab” Chrome extension to add ChatGPT controls directly inside Google Colab notebooks.

Briefing

Learning Python often turns into a loop of trial-and-error—until errors pile up and the “why” behind them stays unclear. This walkthrough shows a practical way to pair ChatGPT with Google Colab so coding help happens inside the notebook: a browser extension adds ChatGPT controls to Colab, then ChatGPT can (1) explain working code, (2) diagnose and fix bugs, and (3) refactor code into a cleaner, more maintainable form.

The process starts with installing a “ChatGPT for Google Colab” extension from the Chrome Web Store. After that, the user opens Google Colab, signs in, and creates a new notebook. The notebook then displays a ChatGPT button, enabling prompts to be sent from within the Colab environment. A key operational detail follows: ChatGPT requires an active login at chat.openai.com. If that session is closed, the Colab integration reports a “session expired” error, so the ChatGPT tab must remain available while working.

To demonstrate the workflow, the notebook runs a factorial program for a non-negative integer. The code initializes a number (example given: 5), defines factorial accumulation starting from 1, checks whether the input is negative, zero, or positive, and uses a loop to multiply values from 1 through n. When the “Explain” task is selected, ChatGPT returns a markdown-formatted breakdown: factorial is described as the product of all positive integers up to n; the conditional logic is mapped to negative/zero/positive cases; and the loop’s role is clarified through the example that 5! becomes 5×4×3×2×1 = 120. ChatGPT also points out how the commented input line can be uncommented to accept user input rather than relying on a hardcoded value.

Next comes error handling. The factorial code is intentionally broken by removing the definition of the number variable and introducing an indentation mistake. Using the “Fixed” (diagnose and fix) task, ChatGPT identifies two issues: a missing input/initialization for the variable num (so the program has no value to compute with) and incorrect indentation inside Python’s if blocks (Python requires indentation to define code blocks). ChatGPT provides an updated version that restores the input mechanism (either by uncommenting the input line or initializing num) and corrects indentation. Running the repaired code with an example input of 5 produces the expected output: factorial of 5 is 120.

Finally, the notebook tests additional ChatGPT tasks: “Summarize” generates a markdown summary of the code’s purpose and structure, while “Refactor” rewrites the program for better readability and maintainability. In the refactored version, factorial computation is moved into a dedicated compute factorial function, and the main flow is organized into a main function. Instead of printing errors for negative inputs, compute factorial returns a value (described as None for negative numbers), allowing the caller to handle error conditions more cleanly. The walkthrough ends by using an “Add” task to insert descriptive comments explaining each code section’s purpose and importance, reinforcing the idea that ChatGPT can improve both correctness and clarity while learning Python in Colab.

Cornell Notes

Pairing ChatGPT with Google Colab turns notebook coding into an interactive help loop. After installing the “ChatGPT for Google Colab” extension and keeping a chat.openai.com session active, the notebook can send tasks like Explain, Fixed (diagnose and fix), Summarize, Refactor, and Add comments. A factorial example demonstrates how ChatGPT can describe conditional logic (negative/zero/positive), loop-based multiplication, and how to switch from hardcoded input to user input. When the code is intentionally broken, ChatGPT identifies missing variable initialization and indentation errors, then supplies corrected code that runs successfully. Refactoring moves computation into a reusable function and improves structure via a main function, making the program easier to maintain.

How does the factorial program handle different inputs, and what does ChatGPT highlight in its explanation?

The program treats factorial as defined only for non-negative integers. It initializes factorial to 1, then uses an if statement to branch: if num is less than 0, it reports that factorial does not exist for negative numbers; if num equals 0, it states factorial of 0 is 1; otherwise, for positive num, it loops from 1 to num and multiplies factorial by each i. With num = 5, the loop multiplies 1×1×2×3×4×5 to reach 120, and ChatGPT’s explanation maps each step to these branches and the accumulation logic.

What two bugs appear after the code is deliberately broken, and how does ChatGPT fix them?

Two issues are introduced: (1) “number is not defined” because the input/initialization line is removed or commented out, leaving num without a value; (2) an indentation error because the print statement inside the if/else blocks is not aligned correctly. ChatGPT’s fix restores the input mechanism (by uncommenting the input line or initializing num) and corrects indentation so Python recognizes the intended code blocks. After applying the updated code, running with input 5 yields factorial of 5 is 120.

Why does keeping the chat.openai.com session open matter when using ChatGPT inside Colab?

The integration depends on an active ChatGPT session. If the chat.openai.com window is closed, Colab’s ChatGPT integration reports a “session expired” error. Keeping the login session available prevents that interruption and allows prompts from the notebook to work reliably.

What changes does “Refactor” make to improve the factorial code’s structure?

Refactoring reorganizes logic into clearer units. The factorial computation is moved into a separate compute factorial function, making it reusable. The main execution flow is placed into a main function for better organization. For negative inputs, compute factorial returns None rather than printing an error directly, so the calling code can handle the error condition more flexibly.

How does “Add” (adding descriptive commands/comments) support learning beyond fixing code?

Instead of only correcting syntax or logic, the Add task inserts comments that explain what each section does and why it matters. This turns the notebook into a more readable reference: learners can connect variables, conditionals, loops, and output statements to their intent, not just their mechanics.

Review Questions

  1. When using ChatGPT tasks in Colab, what happens if the chat.openai.com session is closed, and why?
  2. In the factorial example, how do the conditional branches for num < 0, num == 0, and num > 0 differ in behavior?
  3. What structural improvements does refactoring introduce (function extraction and main encapsulation), and how does returning None for negative inputs change error handling?

Key Points

  1. 1

    Install the “ChatGPT for Google Colab” Chrome extension to add ChatGPT controls directly inside Google Colab notebooks.

  2. 2

    Keep chat.openai.com logged in while working in Colab; closing it can trigger a “session expired” error in the integration.

  3. 3

    Use the Explain task to get markdown-formatted, step-by-step descriptions of code structure, logic, and examples.

  4. 4

    Use the Fixed (diagnose and fix) task to correct common Python issues like undefined variables and indentation errors.

  5. 5

    Switch from hardcoded values to user input by uncommenting or initializing the input variable (e.g., num) before running logic.

  6. 6

    Use Refactor to improve readability and maintainability by extracting computation into functions and organizing execution into a main function.

  7. 7

    Use Summarize and Add to generate learning-friendly markdown summaries and descriptive comments for each code section.

Highlights

A working workflow emerges: install the Colab extension, keep chat.openai.com active, then run Explain/Fixed/Refactor tasks from inside the notebook.
The factorial demo shows ChatGPT mapping Python conditionals and loops to concrete outcomes like 5! = 120.
When the code breaks, ChatGPT pinpoints two classic Python problems—missing variable initialization and indentation mistakes—and supplies corrected code.
Refactoring turns a script into a more modular design by introducing compute factorial and a main function, improving reuse and clarity.

Topics

  • ChatGPT Extension
  • Google Colab Integration
  • Python Factorial
  • Debugging
  • Code Refactoring