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
- Open a new Gemini chat
- Type a specific coding prompt
- Send the prompt with Send message
- Copy the code with Copy code
- Ask a follow-up in the same chat
- Copy the pytest test Gemini returns
Interactive 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
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.
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.
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.
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.
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.
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.
Related guides
Continue with more step-by-step guides for getting work done.
How to create a Gem in GeminiLearn how to create a custom Gem in Gemini: name it, describe it, write instructions, save it, and start chatting with your new Gem right away.
How to use Gemini for writingLearn how to use Gemini for writing: type a drafting prompt, ask for a shorter and more casual rewrite, then copy the finished text in minutes.
How to delete Gemini chatsDelete a Gemini chat you no longer need: open the conversation menu, choose Delete, and confirm to remove its prompts and responses.
Create interactive demos like this one — free, no coding required.
Start for free →