Get AI summaries of any video or article — Sign up free
How to Use ChatGPT as a Powerful Tool for Programming thumbnail

How to Use ChatGPT as a Powerful Tool for Programming

Corey Schafer·
5 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

ChatGPT can generate runnable code from plain-English prompts, often with explanations that support learning.

Briefing

ChatGPT can function as a practical programming copilot: it can generate working code from plain-English prompts, improve existing code, and accelerate “maintenance” work like testing and documentation—so long as developers review outputs and verify correctness. The biggest value comes from the back-and-forth workflow: when something is wrong or incomplete, the model can regenerate code with targeted fixes, which turns common roadblocks into iterative prompts rather than dead ends.

The walkthrough starts with basic code generation. A simple request—looping from 1 to 10 and printing each number—produces a Python script and even includes learning-oriented commentary about how Python’s `range` works (notably that the stop value is exclusive). From there, the examples move into more realistic tasks. One prompt asks for a password-hashing script using a salt and hashing via Python’s standard `hashlib` module. The generated solution uses `os.urandom` to create a salt, hashes the password, prints the result, and includes notes about production requirements like storing salts per user.

A key usability improvement is handling security-sensitive input. The initial script uses `input()`, which echoes typed characters to the screen. When prompted to hide password characters, the model switches to Python’s built-in `getpass` module so the password isn’t displayed—showing how conversational iteration can quickly correct practical issues.

The tutorial then shifts from writing code to refining it. A small snippet that manually tracks an index in a `for` loop is rewritten using Python’s `enumerate`, reducing error risk and making the code more “Pythonic.” The same theme—review and validation—returns repeatedly: outputs shouldn’t be treated as gospel, especially for larger projects.

Next comes explanation and translation. A complex regular expression is fed into ChatGPT with a request to explain what it matches; the model breaks the pattern down piece by piece and flags that the expression may be permissive and accept some invalid emails. The same regex is then converted into Python code using `re.findall`, and later translated into JavaScript, complete with example test strings and usage patterns.

For quality assurance, ChatGPT can generate unit tests for the Python regex-based function. It produces test cases for multiple scenarios (multiple matches, no matches, single match) and can restructure tests so each test targets one behavior, making failures easier to diagnose. It can also add docstrings and comments to previously undocumented code.

Finally, the tutorial highlights a “blank page” use case: generating a starting project scaffold. Using the League of Legends API as an example, it produces a Python script outline that calculates win percentages for a player, including guidance on API keys, request structure, and how to fetch match history.

Concerns about job replacement are addressed directly: the workflow still requires developer judgment to confirm correctness and integrate code into real systems. The practical takeaway is that developers who use AI effectively will likely outpace those who don’t, because the tool speeds up generation, refactoring, testing, and documentation while leaving final responsibility to humans.

Cornell Notes

ChatGPT is presented as a programming tool that speeds up the full development loop: generating code from plain-English prompts, improving existing code, explaining confusing logic, converting between languages, and producing unit tests and documentation. Examples include Python scripts for looping, salted password hashing with `hashlib`, hiding password input with `getpass`, and refactoring index-tracking loops into `enumerate`. Regular expressions are explained step-by-step, then translated into Python (`re.findall`) and JavaScript. The tutorial stresses that outputs must be reviewed and validated, especially for complex projects, and argues that developers remain essential for correctness and integration. The model is also positioned as a way to start new projects without staring at a blank page, such as an outline using the League of Legends API to compute win percentages.

How does ChatGPT help when writing code from scratch, and what learning benefit comes with the generated code?

It can turn a plain-English request into a working script and often adds explanations alongside the code. In the looping example, a prompt to loop from 1 to 10 and print numbers yields a Python `for` loop plus an explanation of why `range` stops before the end value (the stop is exclusive). That combination of code + rationale makes it easier to learn the underlying mechanics while still getting something runnable.

What security-related improvements can be made through iterative prompting?

When a password input is first implemented with Python’s `input()`, typed characters appear on screen. After a follow-up prompt asking to hide what the user types, the solution switches to Python’s built-in `getpass` module, which prevents shoulder-surfing by not echoing the password characters. The key is that the conversational workflow enables targeted fixes after spotting a practical flaw.

Why is refactoring with `enumerate` safer than manually tracking an index?

Manually maintaining an index outside a loop can drift out of sync or go out of bounds, creating debugging headaches. Using `enumerate` lets Python produce index-item pairs directly, so the index stays aligned with the list iteration. The tutorial’s example rewrites a names loop that tracked `index` into a more robust, Pythonic `enumerate` pattern.

How can ChatGPT turn a hard-to-read regular expression into something usable?

It can explain the regex by breaking it into components and describing what each part matches, along with caveats about permissiveness (e.g., matching some invalid emails). Then it can convert the regex into executable code—such as Python using `re.findall` to extract matches from text—and provide example inputs to verify behavior.

What does ChatGPT add to unit testing and why does that matter for maintenance?

It can generate unit tests for the provided function, including multiple scenarios like multiple matches, no matches, and single matches. It may also suggest restructuring so each test targets one behavior, which makes failures easier to pinpoint. The maintenance value is faster error discovery and confidence when changing code, especially in larger projects where regressions are costly.

How does ChatGPT help with the “blank page” problem in new projects?

Instead of starting from documentation alone, it can produce an initial script scaffold. The example uses the League of Legends API to outline a Python program that calculates win percentages for a specific player, including steps like obtaining an API key, specifying parameters (region and summoner name), and fetching match history to compute wins. Even if accuracy must be verified against official docs, it provides a concrete starting point to iterate from.

Review Questions

  1. When would you prefer `enumerate` over manually tracking an index, and what kinds of bugs does that prevent?
  2. What checks should a developer perform before trusting ChatGPT-generated code for production use?
  3. How would you design unit tests for a function that parses or validates input to ensure edge cases are covered?

Key Points

  1. 1

    ChatGPT can generate runnable code from plain-English prompts, often with explanations that support learning.

  2. 2

    Conversational iteration helps fix practical issues quickly, such as switching from `input()` to `getpass` for hidden password entry.

  3. 3

    Refactoring suggestions like replacing manual index tracking with `enumerate` can reduce error risk and improve code clarity.

  4. 4

    Regular expressions can be made understandable through step-by-step breakdowns, then translated into working code in Python or JavaScript.

  5. 5

    Unit tests, including edge-case scenarios and clearer test separation, can be drafted quickly to improve regression safety.

  6. 6

    Documentation and comments can be added automatically using docstrings and inline comments to make future maintenance easier.

  7. 7

    AI assistance doesn’t remove developer responsibility; code must be reviewed and validated, especially for complex systems.

Highlights

ChatGPT can correct a security flaw in an interactive workflow: after noticing password characters were echoed, it replaces `input()` with `getpass` to hide typed text.
A manual index loop is refactored into a safer, more Pythonic pattern using `enumerate`, reducing the chance of index-related bugs.
A complex regular expression is transformed from an opaque string into: (1) a step-by-step explanation, (2) Python extraction code with `re.findall`, and (3) a JavaScript equivalent with test inputs.
Unit tests are generated and then reorganized so each test targets one behavior, making failures easier to diagnose.
For new projects, ChatGPT can produce an initial API-based script scaffold (League of Legends win percentage example) that eliminates the blank-page start.

Topics

Mentioned