Learn Python
>>> getting_started.first_program

Writing and Running Your First Program in Python

#concept

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.

#how it works
1

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.

2

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.

3

Save the file

Save it somewhere you can find again, ideally a dedicated folder for your Python practice files.

4

Open a terminal in that folder

Navigate your terminal to the folder containing the file, using cd (change directory).

5

Run it

Type python hello.py (or python3 hello.py) and press Enter. Python reads the whole file and executes it top to bottom.

#example
# hello.py
print("Hello, World!")
print("This is my first Python program.")
Output
Hello, World!
This is my first Python program.
1Line 1 is a comment β€” Python ignores anything after a # on a line, so this exists purely for human readers.
2Line 2 calls the built-in print() function with a string, which writes Hello, World! to the screen.
3Line 3 does the same with a second message, printed on its own line.
4Running python hello.py in a terminal executes this file from top to bottom, producing both lines of output in order β€” nothing happens until you explicitly run the file.
#use cases
The classic "Hello, World!" sanity check when setting up a new machine or environment
A throwaway sandbox file for testing a snippet of syntax before using it in a real project
Small automation scripts β€” rename files, scrape a webpage, reformat a spreadsheet β€” that start exactly this way
The first exercise in almost every programming course, in any language, for the same reason: it proves the whole toolchain works end to end

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself β€” building beats watching.