Skip to main content
In this lesson we’ll explore basic code completion with GitHub Copilot inside an editor and Copilot Chat. Using a small Python project, you’ll see how Copilot suggests completions, responds to docstrings and type hints, and helps diagnose and fix common runtime errors.
This tutorial covers practical Copilot usage patterns for Python: completing boilerplate, implementing functions from docstrings, generating list comprehensions, handling file errors, and using Copilot Chat to debug exceptions. Follow along by creating a simple main.py.
Relevant links:

1) Create a Python file

Create a new file for this demo:
This minimal step prepares the editor for Copilot to begin suggesting completions.

2) Hello World

Start with a simple main entry point. Copilot often recognizes this pattern and suggests the complete implementation immediately:
Run it to confirm everything works:
Tip: For trivial patterns like this, accept the suggestion or regenerate if you want stylistic variations.

3) Generate a factorial function from a docstring

Copilot leverages function names, type hints, and docstrings to infer implementations. For example, provide a brief signature and docstring:
Copilot commonly completes it as a recursive solution:
If you prefer an iterative approach, Copilot may instead generate a loop-based implementation depending on surrounding code and your editing history.

4) Extract usernames from a list of dictionaries

Copilot recognizes common Python idioms such as list comprehensions. When you start typing, it often suggests the full comprehension:
Run it to verify output:
Copilot speeds up repetitive patterns by completing common idioms like this.

5) Gracefully handle missing files with try/except

When interacting with files, Copilot often suggests handling FileNotFoundError explicitly. Example:
If you prefer broader error coverage, Copilot can also suggest a general exception handler:
Recommendation: choose the more specific exception when you know which errors to expect; use general handlers only when necessary.

6) Using requests and fixing an AttributeError

If you import requests but forget to install it, your editor may offer a quick fix to install the package. Example pip installation:
A common mistake is calling a non-existent method (e.g., requests.test), which raises an AttributeError:
Traceback example:
Copilot typically suggests replacing the incorrect call with requests.get and adding a safe network exception handler:
This both corrects the method and adds robust handling for network-related errors.
Screenshot of a GitHub Copilot menu overlaid on a code editor, with "Status: Ready" highlighted and a hand cursor pointing at "GitHub Copilot Chat." The menu shows options like Open Completion Panel, Disable Completions, Edit Settings, and View Copilot Documentation.

7) Using Copilot Chat to diagnose issues

Open Copilot Chat, paste the error message and the relevant code, and Copilot Chat will usually return a diagnosis and suggested edits. When applying suggestions, you typically have three options:
  • Apply in Editor — Copilot modifies the file directly.
  • Insert at Cursor — Copilot inserts the suggestion at the current cursor location.
  • Copy — copy the suggestion and paste it manually.
Be cautious when using “Apply in Editor” — in some cases Copilot may change more than you expect. Review diffs before accepting changes.
For the requests.test error above, Copilot Chat commonly suggests switching to requests.get and wrapping the call in a try/except for requests.exceptions.RequestException, as shown earlier.

Reference table: patterns and Copilot behavior

Summary

  • GitHub Copilot quickly completes common code patterns (Hello World, factorial, comprehensions).
  • Docstrings and clear type hints improve suggestion accuracy.
  • Copilot recommends specific exceptions (e.g., FileNotFoundError) and practical error-handling idioms.
  • Use Copilot Chat for diagnosing runtime errors, but always review suggested edits before applying them.
  • When an edit is overly broad, copy and paste the suggested change manually to retain control.
By practicing with small examples like these, you’ll learn how Copilot streamlines repetitive tasks and helps diagnose issues without relinquishing control of your code.

Watch Video