Skip to main content
This lesson demonstrates basic code completion workflows using GitHub Copilot. You’ll see concise, real-world Python examples where Copilot suggests small snippets, fills in common patterns (factorial, list comprehensions), helps with exception handling, and assists when using external libraries such as requests. Key search-friendly topics covered: GitHub Copilot, Python code completion, docstring-driven completion, exception handling best practices, and using the requests library.

Create the file

Create a new Python file for the examples:
Open main.py in your editor and trigger Copilot. It will often offer inline suggestions you can accept, discard, or regenerate.

Hello World

A minimal example — accept Copilot’s inline suggestion to generate a simple main function:
Run it:
Why this is useful: Copilot excels at trivial, well-known patterns. Use these small completions to bootstrap files and test your editor setup.

Generating a factorial function

Copilot often infers intent from function names, signatures, and docstrings. Provide a clear signature or docstring to guide the model. Example: a recursive factorial implementation:
Run to confirm:
Tip: If Copilot doesn’t complete the implementation, add a docstring or a comment describing expected behavior — this often yields a correct solution.

Extracting usernames from a list of dictionaries

Copilot suggests common Python idioms such as list comprehensions when it detects the pattern.
Run:
Note: If Copilot fills the comprehension but you forget to print results, a comment like # Print each username can prompt suggestions for the loop.

Gracefully handling missing files

When working with file I/O, Copilot commonly suggests try-except blocks. Start simple and refine to specific exceptions. Example handling a missing file explicitly:
If you prefer a broader catch to log any error before falling back:
Run example (when the file is missing):
Prefer catching specific exceptions (like FileNotFoundError) over Exception to avoid hiding unexpected bugs.
Copilot’s suggestions are helpful for common patterns, but always review and test generated code — especially exception handling — to ensure it fits your use case.

Using the requests library (install and fix errors)

If Copilot inserts imports for external libraries that are not installed, your editor or Copilot may offer quick fixes (for example, pip install). Install requests in your virtual environment or globally as appropriate:
A typical requests usage pattern:
If you intentionally introduce an incorrect call (e.g., requests.test), Python will raise an AttributeError:
Running the incorrect code yields:
Copilot Chat or inline suggestions can diagnose this and propose replacing test with get, or add a try-except wrapper as shown above. When applying Copilot edits you will usually see a diff preview — review it before accepting.

Copilot editing workflows

Copilot offers multiple ways to apply suggestions — choose the workflow that fits your review needs:
  • Insert at Cursor — inserts a short suggestion directly at the cursor.
  • Apply in Editor — replaces or modifies larger sections and shows a diff.
  • Copy — copy suggested code into your clipboard for manual review and paste.
Pros and cons:
  • Fast insertion accelerates routine edits but requires careful review.
  • Diff-based application is safer for larger refactors because you can inspect changes before accepting.
  • Copy/paste gives the most control for auditing generated code.

Quick reference table of examples

Summary

This article covered basic Copilot code-completion scenarios in Python:
  • Accepting inline completions for simple functions (Hello World).
  • Generating implementations from function names and docstrings (factorial).
  • Completing common Python idioms (list comprehensions).
  • Suggesting exception handling for file operations.
  • Helping with external libraries (install prompts and fixing incorrect API usage).
  • Using Copilot Chat or editor workflows to diagnose and apply fixes, with options to copy, insert, or apply edits.
When used thoughtfully, GitHub Copilot can automate repetitive work, produce syntactically correct suggestions, and speed up development — but always review and test generated code before committing.

Watch Video