Running Python Programs

You just installed Python, so it is working on your computer now. Time for the fun part. Let’s actually run some Python code. There is more than one way to do it, and each one is useful at a different moment. Let’s walk through them.

🤔 Why are there different ways to run code?

You might wonder why we even need more than one way. Here is the thing. The job is not always the same, right? Sometimes you just want to test a tiny idea. Other times you want to run a full program you saved.

  • Sometimes you want to test a quick idea, like “what does 5 / 2 give me?”. For that the interactive shell is great, because it answers one line at a time.
  • Sometimes you have a real program with many lines, saved in a file. For that you run a .py file from the terminal.
  • And sometimes you just want a one-line check without opening anything. A one-line command does that.

So you do not pick one and forget the rest. You use whichever fits what you are doing right now. Let’s see each one.

💬 Way 1: The interactive shell (REPL)

The interactive shell lets you type one line of Python and get the answer back immediately. People also call it the REPL. That word looks scary but it is simple. It stands for Read, Eval, Print, Loop, and that is literally the cycle it runs in.

  • Read — it reads the line you typed.
  • Eval — it works out what that line means (Eval is short for “evaluate”, which just means “figure out the value”).
  • Print — it prints the result back to you.
  • Loop — then it loops, so it waits for your next line and does the whole thing again.

To start it, open your terminal and type python, then press Enter.

Terminal window
python

You will see something like this, with >>> sitting there waiting for you.

Python 3.13.1
>>>

That >>> is the prompt. It means the shell is ready and listening. Now type some Python right after it and press Enter. Try a little math and a print.

>>> 2 + 3
5
>>> print("Hello from the shell")
Hello from the shell
>>> name = "Riya"
>>> name
'Riya'

See how it answers each line straight away? You type 2 + 3 and it shows 5 on the very next line. You do not have to save anything or press a Run button. That instant answer is what makes the shell so good for quick tests. So when you are learning a new thing and you just want to see how it behaves, the shell is your friend.

When you are done and want to leave, you have a few ways out. Type exit() and press Enter. On Windows you can also press Ctrl+Z and then Enter. On Mac and Linux, press Ctrl+D.

>>> exit()

The shell forgets everything when you close it

The interactive shell is for trying things, not for saving them. Once you close it, the variables and lines you typed are gone. So for anything you want to keep, write it in a file instead. That is Way 2.

📄 Way 2: Running a saved .py file

This is the main way you run real programs. You write your Python in a file that ends with .py, save it, then tell Python to run that file. The .py part is just the file extension that says “this is a Python file”.

Let’s make one. Create a file named hello.py and put these two lines inside it.

print("Hello from a file!")
print("This program has two lines.")

Now open your terminal in the same folder as that file, and type python followed by the file name.

Terminal window
python hello.py

Python runs the file and prints this.

Output

Hello from a file!
This program has two lines.

So both lines ran, top one first, then the bottom one. This is how almost every Python program runs, from a tiny script like this to a big application with thousands of lines. You save your code in a file, then point Python at the file.

Run it from the right folder

The command python hello.py only works if your terminal is in the same folder as hello.py. If you get an error like “can’t open file”, your terminal is probably somewhere else. Use the cd command to move into the folder where you saved the file first, then run the command again.

🧭 python vs python3 vs py — what do I type?

Here is a small thing that trips people up, so let me clear it up. The command to start Python is not exactly the same on every computer. It depends on your operating system.

  • On Windows, you usually type python. Windows also gives you a handy launcher called py, so py hello.py works too. The py launcher is nice because it always finds your Python even if python is being stubborn.
  • On Mac and Linux, the plain word python sometimes points to an old version, or to nothing at all. So on those systems you usually type python3 instead. The matching installer is pip3.

So if python does not work, try python3. And if you are on Windows, py is a safe bet. They all do the same job, which is starting Python. The name is just different from one system to another.

Terminal window
python hello.py # often works on Windows
python3 hello.py # the safe choice on Mac and Linux
py hello.py # the Windows launcher, also works on Windows

▶️ Way 3: The Run button in an editor

Once you start using a code editor like VS Code, you do not even need to touch the terminal yourself. You open your .py file and click a Run button. It is usually a small triangle that looks like ▶️ in the top corner. The editor runs the file and shows the output in a panel at the bottom of the screen.

Now here is the part people do not realise. That Run button is not magic. Inside, the editor is just running the same python file.py command for you in a hidden terminal. So it is the exact same thing as Way 2, only you did not type the command yourself. Same result, less typing.

We will set up an editor in the next lesson. For now, just know this is usually the most comfortable way once you are writing real programs.

⚡ Bonus: A one-line command

Sometimes you just want to run one tiny piece of Python without making a file or opening the shell. The -c flag lets you do that. It means “run this code right now”.

Terminal window
python -c "print('Quick test!')"

Output

Quick test!

This is handy for a fast check, like confirming Python works or testing a small calculation, without leaving the terminal.

🧠 How does the file actually run?

You might be curious what happens when you type python hello.py and hit Enter. Here is the plain-English version. Python opens your file and reads it from the very top, one line at a time, going downward. It runs each line as it reaches it, in the order they are written. So the first print runs, then the second, then it reaches the end and stops. That order, top to bottom, is why your output comes out in the order it does. You do not need to know more than that right now. Just remember: top to bottom, line by line.

⚠️ Common Mistakes

A few things confuse people early on, so watch for these.

  • “python is not recognized” or “command not found”. This is the most common one. You type python and instead of starting, the terminal complains. It does not mean Python is broken. It means your terminal does not know where Python lives, because Python is not on your PATH. PATH is just the list of places your terminal looks for programs. The calm fix is below.
'python' is not recognized as an internal or external command,
operable program or batch file.

On Windows, the easiest fix is to re-run the Python installer and tick the box that says Add Python to PATH on the first screen. Or just try the py launcher, which usually works anyway. On Mac and Linux, try python3 instead, since plain python may not exist there.

  • Mixing up the shell and a file. The >>> prompt means you are inside the interactive shell. You do not type python hello.py there. That command goes in the normal terminal, not after >>>.
  • Wrong folder. If python hello.py says it cannot find the file, your terminal is probably in a different folder than the file. Move into the right one with cd.
  • Forgetting the .py. When you run a file, include the extension. It is python hello.py, not python hello.

✅ Best Practices

  • Use the interactive shell to try small ideas and see how something behaves. It is the fastest way to learn a new thing.
  • Save anything real in a .py file, so you can run it again later and keep improving it.
  • Keep your .py files in one clear folder, so your terminal can find them without a fight.
  • If a command does not work, try the alternative for your system first: python3 on Mac and Linux, or py on Windows. Half the time that is the whole fix.
  • Once you are comfortable, use an editor’s Run button to save time. Just remember it is running the same command underneath.

🧩 What You’ve Learned

You can now run Python in whatever way fits the moment. Here is the short version.

  • ✅ The interactive shell (REPL) reads, evaluates, prints, and loops, so it runs one line at a time and shows the result instantly. Great for quick tests, and it forgets everything when you close it.
  • ✅ A saved .py file is how you run real programs, using python filename.py from the right folder.
  • ✅ The command is python on Windows, often python3 on Mac and Linux, and py also works on Windows.
  • ✅ An editor’s Run button just runs the same python file.py command for you underneath.
  • ✅ “python is not recognized” or “command not found” means Python is not on your PATH, and the fix is to add it during install or use py / python3.
  • ✅ Python reads your file top to bottom, line by line, running each line in order.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is the interactive shell (REPL) best used for?

    Why: The REPL runs one line at a time and shows the result instantly, which is perfect for quick tests. It does not save your code.

  2. 2

    How do you run a Python file named app.py from the terminal?

    Why: You type `python app.py` (or `python3 app.py`). Include the `.py` extension, and run it from the file's folder.

  3. 3

    You see a `>>>` prompt. Where are you?

    Why: The `>>>` prompt means you are inside the Python interactive shell. Type `exit()` (or Ctrl+Z then Enter on Windows, Ctrl+D on Mac/Linux) to leave it.

  4. 4

    Your terminal says 'python is not recognized'. What does that usually mean?

    Why: It means the terminal does not know where Python is, because it is not on your PATH. Fix it by adding Python to PATH during install, or use the `py` launcher on Windows or `python3` on Mac/Linux.

🚀 What’s Next?

You know how to run code. To write it comfortably, you want a good editor. Let’s look at the best tools for the job.

Python IDEs and Editors

Share & Connect