Writing and Running Your First Program in Python
A Python program is just a plain text file saved with a .py extension. There's no special "create project" step and no compiler to run first β you write code in an editor, save the file, and hand it to the python command from a terminal.
This is called running Python in "script mode": the interpreter reads your file from top to bottom and executes every line in order, exactly once, then exits. It's the mode you'll use for almost every real program, as opposed to the interactive REPL, which runs one line at a time and suits quick experiments better.
Create a new file
In your editor, create a new file and save it with a .py extension β for example, hello.py. The name before .py can be anything valid for a filename.
Write some code
Type a line or two of Python β a print() call is the traditional first line, since it produces visible proof the program ran.
Save the file
Save it somewhere you can find again, ideally a dedicated folder for your Python practice files.
Open a terminal in that folder
Navigate your terminal to the folder containing the file, using cd (change directory).
Run it
Type python hello.py (or python3 hello.py) and press Enter. Python reads the whole file and executes it top to bottom.
# hello.pyprint("Hello, World!")print("This is my first Python program.")Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself β building beats watching.