Skip to main content
In this lesson we’ll explore basic code completion with GitHub Copilot and how it speeds up common Python tasks. You’ll see examples where Copilot completes functions from short prompts, interprets docstrings, recognizes idiomatic patterns, and suggests robust error handling.
This lesson demonstrates quick patterns Copilot uses to complete code from prompts, docstrings, or common idioms. Use these examples as a starting point and always review suggestions for correctness and security before accepting them.

Create a Python file

Create the demo file:
Open the file in your editor. Depending on your setup, Copilot suggestions may appear in-line or via a suggestions pane (for example, pressing ⌘I in some editors opens Copilot suggestions).

Hello World

Start with a short prompt and let Copilot finish the function. Typing something like write hel often triggers a full implementation:
Run it:

Factorial: guide completion with a docstring

Copilot can implement functions from a signature and docstring. Example starting point:
Copilot commonly expands this into a recursive or iterative implementation, for example:
Call and test this function inside main() or a small test harness.

List comprehension example

Copilot recognizes common idioms like extracting fields from lists of dictionaries. Given this data:
Copilot often suggests the list comprehension:
Run the file:

File handling with exceptions

When reading files, Copilot frequently suggests try/except blocks to handle missing files or other I/O errors:
If data.txt does not exist, the program prints the fallback string:
To catch a broader set of errors:

Making an HTTP request with requests

If you add import requests, Copilot often fills in a common usage pattern. If the package is missing, install it:
A typical suggestion is a simple GET and printing the status code:
If you accidentally call a non-existent method (for example requests.test), Python raises an AttributeError:
Console output:
Copilot (or Copilot Chat) will typically suggest replacing requests.test with requests.get. To make network calls robust, wrap them in exception handling for network-related errors:
This try/except pattern is commonly suggested when Copilot recognizes network calls.
A screenshot of a GitHub Copilot menu inside a code editor, showing options like "Status: Ready" and "GitHub Copilot Chat." A hand-shaped cursor hovers over one of the menu items.

Using Copilot Chat to debug or improve code

Copilot Chat can analyze pasted snippets and recommend fixes (for example, replace requests.test with requests.get) or suggest adding exception handling. When a chat suggestion appears you typically have three actions:
  • Apply in Editor — inserts the change directly into your file (you can review diffs first).
  • Insert at Cursor — places the suggestion where your caret is.
  • Copy — copies the snippet to your clipboard.
Always review changes before applying automated edits. Use “Show Changes” or the diff view to confirm the modification preserves intended behavior and doesn’t inadvertently remove or reorder important code.
Be mindful: automated edits may move or delete code unexpectedly. Review and run tests after applying suggestions.

Quick reference table

Summary

  • GitHub Copilot excels at filling common Python patterns: Hello World, recursion, list comprehensions, file I/O, and HTTP requests.
  • Guide Copilot using descriptive names, type hints, and docstrings to improve suggestion relevance.
  • Use Copilot Chat to diagnose errors and propose fixes, but always inspect diffs and run tests after applying changes.
  • Treat Copilot as a productivity assistant for boilerplate and repetitive code so you can focus more on design and logic.
Further reading and references:

Watch Video