INTRODUCTION
Your first .py file
Code lives in a file. The terminal runs it.
press Enter ↵
🐍 main.py
Ctrl + Enter runs it too. Esc, then Tab, leaves the code.
Output

Press ▶ Run to see what your program does.

Lesson 29: Your first .py file

Level 4, Python First Steps. Save code in a file and run it from the terminal. The new word: .py file. It follows lesson 28, Programs that listen (input). Next comes lesson 30, Error detective.

What the lesson explains

Your first .py file. Code lives in a file. The terminal runs it.

Save, then run. PlayBuddy saves greet.py in my-project, then runs it. On the card: python greet.py

What you do first

Run it yourself. In the practice terminal, type cat greet.py at the prompt PS C:\Users\you\Documents\my-project> and the computer answers: print("Hi from greet.py!") / print("I am a Python file.").

The code on the screen

Your first .py file. The editor shows 2 lines: print("Hi from greet.py!") / print("I am a Python file."). Run it, and it prints "Hi from greet.py!", "I am a Python file.".

Save, then run. The editor shows 2 lines: print("Hi from greet.py!") / print("I am a Python file."). Run it, and it prints "Hi from greet.py!", "I am a Python file.".

What you practise

Try it, "Run it yourself": In the practice terminal, type cat greet.py at the prompt PS C:\Users\you\Documents\my-project> and the computer answers: print("Hi from greet.py!") / print("I am a Python file."). Then: In the practice terminal, type python greet.py at the prompt PS C:\Users\you\Documents\my-project> and the computer answers: Hi from greet.py! / I am a Python file..

Your turn, "Write a file of your own": Print two lines of a story. One way to write it: print("Once upon a time,") / print("a coder wrote a story."). It prints: Once upon a time, / a coder wrote a story..

What PlayBuddy and PlayType say

PlayBuddy: "Real coders save their code in a file, then run it from the terminal."

PlayType: "A Python file ends in dot p y. That is how the computer knows what it is."

Help in every lesson

Everything happens on a pretend computer in your browser, so nothing touches your real files. PlayBuddy and PlayType stand beside the board, cheer you on and say when it is your turn.

A green ring shows where to click the first time. After that you do it on your own, and if you slip twice the ring comes back.

Stars

The lesson has 3 things to do. Up to one slip in ten gives three stars, up to one in four gives two, and finishing always gives at least one. Stars and progress are saved in this browser only; there is no account.

About DayOneCode

DayOneCode is an independent learning site, not affiliated with or endorsed by Anthropic. Claude and Claude Code are trademarks of Anthropic.