Activity ‐ Generating Python Code - accentient/github-copilot-devs GitHub Wiki

Generating Python Code with GitHub Copilot

Objective

This activity introduces Python developers to GitHub Copilot while building a Unit Converter application. The activity demonstrates how Copilot assists in scaffolding projects, generating code, improving quality, and enhancing maintainability. By the end of the activity, students will:

  • Understand how to integrate and effectively use GitHub Copilot in VS Code
  • Experience generating and refining Python code snippets using Copilot prompts
  • Build, test, and debug a Unit Converter application with Copilot-assisted code
  • Learn how to enhance code quality through error handling, refactoring, and best practices with Copilot

Project

Use GitHub Copilot to scaffold and write a simple Unit Converter console app in Python that converts length (meters ↔ feet), temperature (Celsius ↔ Fahrenheit), and weight (kilograms ↔ pounds).

Prerequisites

  • Python installed
  • Visual Studio Code with GitHub Copilot enabled
  • Active GitHub Copilot subscription

Steps

Step 1: Project setup

  • Create a new Python project
Scaffold a new Python console app named PythonDemo

Step 2: Create a UnitConverter class

  • Open Main.py and add a comment at the top to prompt GitHub Copilot
# Create an empty private UnitConverter class
  • Press Tab and Enter to accept the generated code

Step 3: Implement Length Conversion

  • Within the UnitConverter class, add a comment to prompt GitHub Copilot
# Implement meters to feet conversion
  • Press Tab and Enter to accept the generated code, method by method; or, press Alt + ] or Alt + [ to see the next or previous suggestion. You can also press Ctrl + Enter to open a new tab showing multiple suggestions. To accept a suggestion, below the suggestion, click Accept suggestion NUMBER. To reject all suggestions, close the tab.

Step 4: Implement Temperature Conversion

  • Within the UnitConverter class, add a comment to prompt GitHub Copilot
# Implement Celsius to Fahrenheit conversion

Step 5: Implement Weight Conversion

  • Within the UnitConverter class, add a comment to prompt GitHub Copilot
# Implement kilograms to pounds conversion
  • Review the code

Step 6: Implement a Client UI

  • Within the Main class, add a comment to prompt GitHub Copilot
# Call conversion methods
  • Review the code
  • Build/Run the app

Step 7: Ask GitHub Copilot to Explain Code

  • Within the UnitConverter class, highlight a method, right-click and select Copilot > Explain
  • In the GitHub Copilot Chat window, ask some additional questions about the code
Review all of the code. Is it secure?
Is the code threadsafe?
Do you see any open source code?
Is this code easy to understand and maintainable?
Are there any edge cases this code might fail to handle?

Step 8: Add Error Handling

  • Within the UnitConverter class, highlight one of the conversion methods, right-click and select Copilot > Inline Chat
Add error handling
  • Accept/insert the code if you want
  • Do this for other methods if you want, or for multiple methods at once
  • Review the code
  • Build/Run the app

Step 9: Improve the Code

  • Highlight all the code and bring up Inline Chat
/fix Improve the code to use common Python coding conventions. As part of the refactoring, configure constants, refactor code into methods, and use meaningful variable names.
  • Accept/insert the code if you want
  • Review the code
  • Build/Run the app

Summary

In this hands-on activity, you built a Unit Converter application in Python using GitHub Copilot as an AI coding assistant. Through guided prompts, you integrated Copilot into your development workflow, generated code for length, temperature, and weight conversions, implemented error handling, and refined AI-generated suggestions. This experience provided you with practical insights into accelerating software development with AI tools.

Resources