Guide Python Beginner

Write and Run Your First Python Program

How to write a first Python program and run it on Windows, macOS, and Linux — creating the file, the python vs python3 vs py command differences, making a script directly executable, and common first-run mistakes.

4 min read
Write and Run Python Program

1.1 Create Your First Python File

Open VS Code, IDLE, PyCharm, or another text editor. Create a file named hello.py and add this code:

print("Hello, World!")

Save the file. The .py ending tells your editor that the file contains Python code.

1.2 Understand the Program

The line contains two parts:

  • print() is a Python function that displays text.
  • "Hello, World!" is the text we want to display.

The output is:

Hello, World!

Python does not require a semicolon at the end of this line.

1.3 Run the Program on Windows

  1. Open PowerShell or Command Prompt.
  2. Move to the folder where you saved hello.py.
cd C:\Users\yourname\projects
  1. Run the program:
python hello.py

You can also use the Windows Python launcher:

py hello.py

The output should be:

Hello, World!

1.4 Run the Program on macOS or Linux

  1. Open a terminal.
  2. Move to the folder where you saved hello.py.
cd ~/projects
  1. Run the program:
python3 hello.py

On macOS and Linux, python3 is usually the correct command for Python 3.

1.5 Run the Program in VS Code

  1. Open the hello.py file in VS Code.
  2. Select the Python interpreter if VS Code asks you to choose one.
  3. Click the Run Python File button in the top-right corner.

VS Code runs the file and shows the output in the terminal panel.

You can also right-click inside the file and select Run Python File in Terminal.

1.6 Run a Python Command Without Creating a File

You can use the -c option to run a short command directly.

On Windows:

python -c "print(2 + 2)"

On macOS or Linux:

python3 -c "print(2 + 2)"

The output is:

4

This is useful for quick checks, but larger programs should be saved in .py files.

1.7 Use the Python Interactive Shell

Run python on Windows or python3 on macOS/Linux without a file name:

>>> 2 + 3
5
>>> print("Testing Python")
Testing Python
>>> exit()

The interactive shell runs one line at a time. It is useful for testing small ideas.

1.8 Run a Script Directly on Linux or macOS

You can run a script directly by adding a shebang as the first line:

#!/usr/bin/env python3

print("Hello, World!")

Make the file executable:

chmod +x hello.py

Run it:

./hello.py

On Windows, run the file with python hello.py or py hello.py instead.

1.9 Add Variables and User Input

Now create a slightly more useful program:

name = input("What is your name? ")
print(f"Hello, {name}!")

When you run the program, Python waits for you to type a name and press Enter.

Example:

What is your name? Alex
Hello, Alex!

1.10 Common Mistakes

Running from the Wrong Folder

If Python cannot find the file, move to the correct folder with cd or provide the full file path.

can't open file 'hello.py': No such file or directory

Using Different Quote Characters

The opening and closing quotes must match:

print("Hello")

This is incorrect:

print("Hello')

Saving the Wrong File Name

Make sure the file is saved as hello.py, not hello.py.txt. On Windows, enable File name extensions in File Explorer if you cannot see the full file name.

Using the Wrong Python Command

Use the command that matches your operating system:

Operating systemCommand
Windowspython hello.py or py hello.py
macOSpython3 hello.py
Linuxpython3 hello.py

Forgetting to Save the File

Save the file before running it. Otherwise, Python may run an older version of your code.

1.11 Practice Exercise

Create a file named system_check.py:

import platform

name = input("Enter your name: ")
print(f"Hello, {name}!")
print(f"Operating system: {platform.system()}")
print(f"Python version: {platform.python_version()}")

Run the program and record the output.

Interview Questions

  • How do you create a Python program?
  • How do you run a Python file on Windows?
  • How do you run a Python file on Linux or macOS?
  • What is the difference between python, python3, and py?
  • What does the print() function do?
  • What is a shebang line?
  • What is the Python interactive shell?

Quick Interview Answer

I create a file with a .py extension, write Python code in it, and run it from a terminal. On Windows I usually use python file.py or py file.py. On Linux and macOS I usually use python3 file.py. I can also run the file from an editor such as VS Code.

Key Takeaways

  • Python programs are usually saved in .py files.
  • print() displays text or values.
  • Use python or py on Windows and python3 on macOS/Linux.
  • The terminal must be in the correct folder before you run a file.
  • VS Code can run Python files directly.

Chapter practice checkpoint

Run python --version (or python3 --version on systems using that command). Save print("Hello, automation!") in hello.py, then run it from the terminal with python hello.py.

Expected output: Hello, automation! on its own line.

Knowledge check: What is the difference between the interpreter prompt and a saved script?

Check your answerThe prompt evaluates input interactively. A script stores statements in a file that the interpreter executes, making the work repeatable and shareable.

You are ready to continue when you can locate your script, run it, change its message, and explain the output.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form