Write and Run Your First Python Program

Write your first Python script, run it from the terminal or editor, and understand what happens when the code executes.

5 min read

You have Python installed and an editor ready. The next step is to write a small Python file, run it, and see the result. This is the basic loop you will repeat thousands of times while learning.

If you have not installed Python yet, go back to the installation guide. If you need an editor, see the editor comparison first.

Create a Python file

Open your editor and create a new file. Save it as hello.py in a folder you can find easily, such as a python-practice folder on your desktop or in your home directory.

The .py extension tells your editor and your operating system that this file contains Python code. Without it, syntax highlighting and other editor features may not work.

Type this single line into the file:

pythonpython
print("Hello, World!")

Save the file. That is your first Python program.

The print() function sends whatever is inside the parentheses to the terminal. The double quotes around Hello, World! make it a string, which is how Python represents text.

Run the program from the terminal

Open a terminal and navigate to the folder where you saved hello.py. For example, if you saved it on your desktop:

bashbash
cd ~/Desktop/python-practice

Now run the file with Python:

bashbash
python hello.py

If your system uses python3 instead of python, use that:

bashbash
python3 hello.py

You should see this output:

plaintextplaintext
Hello, World!

If you do, congratulations. You just wrote and ran a Python program.

If you see an error instead, do not worry. Read the error message. Common first-time issues include:

  • python: command not found -- Python is not installed or not on your PATH. Go back to the installation guide.
  • can't open file 'hello.py' -- You are not in the right folder, or the filename has a typo. Use ls (macOS/Linux) or dir (Windows) to see what files are in the current folder.
  • SyntaxError -- Check that your code matches the example exactly, including the parentheses and quotes.

Run the program from your editor

Most editors let you run a Python file without leaving the editor. In VS Code, open the file, then click the play button in the top-right corner, or press Ctrl+F5 (Windows) or Cmd+F5 (macOS) to run without debugging.

In PyCharm, right-click the file tab and choose "Run 'hello'".

In IDLE, open the file and press F5, or use the menu: Run > Run Module.

The output appears in a terminal or output panel inside the editor. The result is the same as running it from the terminal directly.

Pick one way to run your code and stick with it for now. Switching between terminal and editor buttons is fine when you want to compare, but using one approach consistently helps you build muscle memory.

Try a few variations

Now that the basic loop works, experiment with small changes. Modify hello.py to print a few more lines:

pythonpython
print("Hello, World!")
print("My name is Python.")
print("I run one line at a time.")

Save and run again. You will see three lines of output, each on its own line:

plaintextplaintext
Hello, World!
My name is Python.
I run one line at a time.

Python runs statements in order, from top to bottom. Each print() call adds a new line at the end by default.

Try printing a number:

pythonpython
print(42)
print(3 + 4)

You do not need quotes around numbers. print(3 + 4) prints 7 because Python calculates the sum before printing.

What happens when you run a Python file

When you type python hello.py, here is what happens behind the scenes:

  1. Python reads the file from disk.
  2. It parses the code to check for syntax errors.
  3. If the syntax is valid, Python compiles the code into bytecode.
  4. The Python Virtual Machine executes the bytecode, line by line.
  5. Each print() call writes text to the terminal.
  6. When the last line finishes, Python exits.

For a deeper look at this process, read how Python runs your code.

Next steps

Now that you can write and run a Python file, you are ready to learn a few more things that make day-to-day coding easier. The next articles cover how Python executes your code, how to install third-party packages, and how to structure your first project.

Once you are comfortable running scripts, spend a few minutes trying different print() statements. Change the message. Print a few numbers. Make a deliberate typo and see what the error looks like. The more you practice this small loop, the faster the rest of Python will feel natural.

Rune AI

Rune AI

Key Insights

Save Python code in files ending with .py; Use python filename.py to run a script in the terminal; print() is your first tool for seeing program output; Error messages are helpful clues, not failures.

RunePowered by Rune AI

Frequently Asked Questions

What is the simplest Python program I can write?

A single line: print('Hello, World!'). Save it as a .py file, then run it with python filename.py in the terminal.

Why does my Python file not run when I double-click it?

Python scripts are not like regular apps. They need the Python interpreter to run them. The most reliable way to run a script is to open a terminal and type python yourfile.py.

What does print() actually do?

print() sends text to the terminal or command prompt. It is the simplest way to see output from your program.

My terminal shows an error when I run my script. Is that normal?

Yes, errors are a normal part of programming. Read the error message carefully. It tells you which line has the problem and usually gives a hint about what went wrong.

Conclusion

Running your first program means you have a working Python setup. The loop of writing code, running it, reading output, and fixing errors is the core of learning Python.