How to use Gemini for coding

Learn how to use Gemini for coding: write a prompt, copy the code block it returns, and ask a follow-up for a pytest unit test, step by step.

Quick answer

Open a new chat in Gemini, type a clear coding prompt in the Enter a prompt for Gemini box, and send it. Gemini replies with a code block you can copy using Copy code. Then ask a follow-up, such as adding a pytest test, and copy that code too.

Steps at a glance

  1. Open a new Gemini chat
  2. Type a specific coding prompt
  3. Send the prompt with Send message
  4. Copy the code with Copy code
  5. Ask a follow-up in the same chat
  6. Copy the pytest test Gemini returns

Interactive demo

Loading demo…

Summary

Gemini can write, explain and extend code from a plain-language prompt. Because the chat keeps context, you can ask for changes or tests as follow-ups instead of starting over.

Step-by-step guide

  1. Step 1

    Describe the code you need

    Start a chat from the home screen, where the heading reads Where should we start?. Click the Enter a prompt for Gemini box and describe the code. Name the language, the inputs and the rules, for example removing duplicate emails case-insensitively and keeping the first occurrence.

  2. Step 2

    Send the prompt

    Click Send message to submit your prompt. Gemini starts generating a reply, and a Stop response button shows while it works. Wait until it disappears before you read or copy anything.

  3. Step 3

    Copy the code

    Gemini answers with a Python code block. Click Copy code in the top corner of the block to copy it, and a Copied to clipboard message confirms it. Paste it into your editor and run it.

  4. Step 4

    Ask a follow-up

    Stay in the same chat and click the Enter a prompt for Gemini box again. Type a follow-up such as Add a unit test with pytest. Gemini already has the earlier code as context, so you do not need to paste it back in.

  5. Step 5

    Send the follow-up

    Click Send message to submit the follow-up. Your request appears in the conversation and Gemini starts a new reply below the first one.

  6. Step 6

    Copy the pytest test

    Gemini returns unit tests covering mixed cases, empty lists and lists with no duplicates. Click Copy code on the new code block to copy the tests. Save them next to your function and run pytest.

Why this matters

You need a small utility function today, and you want it tested before it goes into your project. Prompting Gemini, copying the result and asking for a pytest test in the same chat gets you working code and a test in minutes.

Frequently asked questions

  • Do I need to paste my code back in for a follow-up?

    No. In this capture Gemini wrote a pytest test for the function from the first reply without the code being pasted again.

  • Can I check the code before using it?

    Yes, and you should. Gemini shows a note that it can make mistakes, so run the code and its tests yourself before relying on them.

Create interactive demos like this one — free, no coding required.

Start for free →