Skip to content
VLSI Mentor

Python Part I · Module 1 · Introduction to Python

Writing and Running Your First .py File

This is the chapter the previous six have been preparing for. You will create a file, put two lines of Python in it, save it, run it from your terminal, and see output appear. It takes about five minutes and it is the single most important five minutes of Part I, because every chapter from here on assumes you can do it without thinking. The chapter also spends real time on the part that actually defeats people on their first attempt, which is not the Python at all — it is being in the wrong directory when you run the command. Getting that straight now means the next fifteen modules are about the language rather than about your shell.

Foundation10 min readPythonPart IFundamentalsFirst ProgramTerminal

Part I · Module 1 · Chapter 1.7 · Writing and Running Your First .py File

1. What You Are About to Do

Four steps, and they are the loop you will repeat for the rest of your Python life:

Four steps: write Python into a text file, save it with a .py name, run it from the terminal with python3, and read the output or the error that comes back.Write → save → run → read01WRITEtype Python into a text editor02SAVEgive the file a name ending in .py03RUNpython3 hello.py, at the terminal04READoutput — or an error telling you what to fix
Figure 1 — the edit-save-run loop. You will do this thousands of times. Every later chapter in Part I is a variation on it: change what is in the file, run it again, read what came back.

2. Write the File

Open any text editor — VS Code, Vim, Emacs, Notepad, whatever you already use. A code editor is more comfortable, but nothing here requires one.

Type exactly this:

Azvya Education Pvt. Ltd.VLSI Mentor
hello.py
print("Hello, Python!")

Save it with the name hello.py. The .py on the end matters: it is how you and your editor know what the file is, and it is the name you will type in a moment.

3. Go to Where the File Is

This step is skipped by most tutorials and is where most first attempts fail.

Your terminal is always "in" some directory. When you ask it to run hello.py, it looks for that file in the directory it is currently in — not wherever you happened to save it.

Ask where you are:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
pwd
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
/home/asaifi

List what is there, and check hello.py is among them:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
ls
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Documents  Downloads  hello.py  projects

If hello.py is not listed, you are in the wrong place. Move to the directory you saved it in using cd, then run ls again to confirm:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
cd Documents
ls

4. Run It

With hello.py in the directory you are in, type:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
python3 hello.py

And there it is:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Hello, Python!

That is a working Python program. The interpreter read your file, carried out the single instruction in it, and printed the result.

5. When It Does Not Work

Three failures account for nearly everything on a first run. None is a Python problem.

python3: can't open file 'hello.py': [Errno 2] No such file or directory

The interpreter started fine but could not find your file. You are in the wrong directory, or the file is named something else — hello.py.txt is a classic, because some editors add .txt silently. Run ls and read the name that is actually there.

python3: command not found

The interpreter itself was not found. That is Chapter 1.6 — go back, work out which command your machine uses, and come back.

SyntaxError

Python found the file, started reading it, and could not make sense of what you typed. Compare your line against §2 character by character. A missing quote or bracket is the usual cause, and Chapter 1.11 is entirely about reading these messages.

6. Change One Thing

Now the part that turns this from a recipe into learning.

Predict. You are going to change the text between the quotes. Before doing it, write down what you expect to see:

Azvya Education Pvt. Ltd.VLSI Mentor
hello.py — edited
print("Simulation complete")

Run. Save the file and run the same command again:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
python3 hello.py

Observe.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Simulation complete

The text between the quotes is yours. Python displays whatever you put there. Nothing else about the line changed.

Notice also that you did not rebuild anything, and there was no separate step between saving and running. That short loop is what Chapter 1.2 meant about Python handling the translation step for you.

7. Two Lines

Add a second line so the file reads:

Azvya Education Pvt. Ltd.VLSI Mentor
hello.py — two instructions
print("Simulation complete")
print("Errors: 0")

Predict which message appears first. Then run it:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Simulation complete
Errors: 0

The first line ran, finished, and then the second ran. This is Chapter 1.1 §5 in action — one thing at a time, in the order written. Swap the two lines and the output order swaps with them.

8. Naming Files

Two rules now, so you do not build a habit you have to break:

  • End the name in .py. Everything assumes it.
  • Avoid naming a file after something Python already provides. A file called random.py or json.py can confuse Python later, when your file is found instead of the one that ships with Python. This will not bite you today, but a file named test.py or string.py is worth renaming now rather than debugging in Module 13.

Short, descriptive, lowercase, with underscores instead of spaces — check_log.py, hello.py — is the normal convention.

9. Exercise

Exercise 1 — three lines, predicted first. Edit hello.py to print three different messages. Before running it, write down the exact three lines you expect. Then run it and compare. Did anything surprise you?

Exercise 2 — cause the directory error deliberately. Move to a different directory:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
cd ..
python3 hello.py

Read the error. Which part of the message tells you it is a file problem rather than a Python problem? Then cd back and confirm it works again.

Exercise 3 — reorder. Swap your first and last print lines. Predict the new output before running. This is worth doing even though you are sure, because it is the last cheap opportunity to confirm that order means exactly what it appears to mean.

Exercise 4 — break it on purpose. Delete the closing ) from one line and run it. You will get a SyntaxError. Do not try to understand the whole message yet — just note which line number it mentions. Then put the bracket back.

10. Summary

  • The loop is write → save → run → read. Everything in Part I is a variation on it.
  • A .py file is an ordinary text file written in any editor. Name it ending in .py.
  • python3 hello.py is a terminal command, not something that goes inside the file.
  • Your terminal has a current directory, and it looks for your file there. pwd says where you are, ls says what is there, cd moves you. Most first-run failures are this, not Python.
  • Lines run in the order written — the first print always appears first.
  • Change one thing, predict, run, observe. That loop is how the rest of this course is meant to be read.

You have written and run a Python program. The next chapter goes back to the one line you have been using on trust and takes apart every character of it.

Where this fits

Part of the Python curriculum.