Tutorials / Prompt Engineering with ChatGPT / Using ChatGPT for coding and technical work

Using ChatGPT for coding and technical work

15 min read text
Course contents (19 lessons)
  1. Understanding the tool
  2. What a language model is, in plain words
  3. How ChatGPT works from a user's point of view
  4. Building a good prompt
  5. The anatomy of a good prompt
  6. Being specific: how to remove guesswork
  7. Showing examples: few-shot prompting
  8. Asking for step-by-step reasoning
  9. Controlling and improving answers
  10. Controlling length, tone and format
  11. Iterating: how to improve an answer with follow-ups
  12. Custom instructions and memory
  13. Working with files and images
  14. ChatGPT for real tasks
  15. Using ChatGPT for writing: emails, reports and rewrites
  16. Using ChatGPT for studying and learning
  17. Using ChatGPT for coding and technical work
  18. Using ChatGPT for data, spreadsheets and business tasks
  19. Prompt templates
  20. A library of reusable prompt templates
  21. Reliable and safe use
  22. Avoiding hallucinations and checking facts
  23. Privacy and safe use
  24. Workflows and final project
  25. Building repeatable workflows and custom assistants
  26. Final project: build your own prompt toolkit

A fast pair programmer with blind spots

I write code for a living, and I use assistants like this one most days. They save real time on boilerplate, explanations, and first drafts. They also produce code that looks right and is wrong, uses functions that do not exist, or quietly ignores an edge case. So the working method is: ask well, read carefully, run the code, test it. The model drafts, you decide.

Give the model what a colleague would need

Weak prompt:

Write a function to process orders.

Stronger prompt:

Write a Python function that takes a list of orders and returns the
total revenue per customer.

Each order is a dictionary like:
{"customer": "ali", "amount": 25.50, "status": "paid"}

Rules:
- Only count orders whose status is "paid".
- Customer names are case-insensitive ("Ali" and "ali" are the same).
- Return a dictionary of customer to total, rounded to 2 decimals.

Use Python 3.11 and only the standard library. Add short comments,
and finish with three example calls that I can run, with their expected
output.

The model now has the language and version, the input shape, the rules (including an edge case: case-insensitive names), the output shape, and a request for runnable examples. The examples are valuable: you can run them to check that the code does what the model says it does.

Explaining code you do not understand

Explain what this code does, line by line, for a developer who knows
JavaScript but has never used Python.

Then tell me one thing in it that could cause a bug.

[paste code]

Naming your background is what makes the explanation useful. The request for a possible bug gets a free quick review at the same time. Remember the model explains what the code appears to do. If an explanation sounds odd, run the code with a small input and check.

Debugging

The best debugging prompts include the error message, the code, and what you expected. Without those three things you are asking the model to guess.

My code should print the average of the numbers, but it prints 0.

Language: Python 3.
Code:
[paste the smallest code that shows the problem]

Input I used: [1, 2, 3, 4]
Expected output: 2.5
Actual output: 0

Explain the cause in plain words first, then show the fix. Tell me
how I could have found this myself with a print statement.

The final line is the one that makes you a better programmer. The aim is not only a fixed program, but a better method next time. If you get an error message with a stack trace, paste the whole thing, not a paraphrase; the exact words matter.

Make the example small. If you paste 600 lines, the model has to guess where the problem is. Cut it down to the smallest code that still shows the bug. Often, in the process of cutting it down, you find the bug yourself.

Code review

Review the function below as a careful senior developer.
Look for: bugs, unhandled edge cases (empty input, null values, very
large input), security problems, and unclear naming.

List problems in order of importance. For each, show a short fix.
Do not rewrite the whole function and do not change what it does.

[paste code]

"Do not rewrite the whole function" keeps the answer focused. The ordering by importance stops a long list of style nitpicks from hiding a real bug.

Writing tests

Here is a function and its description. Write unit tests for it using
pytest. Include: normal cases, empty input, a single item, and at
least two edge cases you think the author may have missed.
Explain each edge case in a comment.

[paste function]

Tests are a good use because you can run them. Failing tests may point to a bug in the function or in the test itself, and either is a useful finding.

Check before you trust

  1. Run it. Code that has not been run has not been checked.
  2. Try empty input, very large input and wrong types.
  3. Check that every library function exists in the version you use. Models sometimes invent a method that sounds right. The error message will tell you quickly.
  4. For anything touching security, money or personal data, review it with extra care, or have a person review it. Generated code can include weak practices such as building SQL by joining strings.
  5. Understand it before you commit it. If you cannot explain a line, ask the model to explain it first.

Keep secrets out

Never paste passwords, API keys, private tokens, customer data or confidential company code into a chat. Replace them with placeholders such as API_KEY_HERE. If you work for a company, find out what is allowed before you paste its code anywhere. This is a rule of the workplace, not just of good practice.

The common mistake

Pasting code in and asking "fix this" without a description of what it should do. The model guesses your intent, and "fixes" a thing that was not wrong. Say what you expected. The other classic is copying a large generated block into your project without understanding it, then being unable to maintain it. Ask for smaller pieces that you can read and test one at a time.

Try it yourself

  1. Think of a small script you wish you had, such as renaming files by date. Write a prompt with language, input, rules and examples, run the result, and test it with odd input.
  2. Take an old piece of code of yours and use the review prompt. Decide which of its points are real.
  3. Next time you hit an error, use the debugging layout with expected and actual results before searching anywhere else.
text
Write a Python function that takes a list of orders and returns the
total revenue per customer.

Each order is a dictionary like:
{"customer": "ali", "amount": 25.50, "status": "paid"}

Rules:
- Only count orders whose status is "paid".
- Customer names are case-insensitive ("Ali" and "ali" are the same).
- Return a dictionary of customer to total, rounded to 2 decimals.

Use Python 3.11 and only the standard library. Add short comments,
and finish with three example calls that I can run, with their expected
output.

This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.

Sign up free I already have an account