Skip to main content

Hello World (first programs) · Beginner · 14 min

Hello World in Python: the interpreter, input and your first script

By Padding · Updated 18 Nov 2026
Python 3.15.2 · python:3.15-slim

You will print Hello World in the Python 3.15 interpreter, save and run it as a script, greet a person by name from the keyboard and from the command line, and know what Python does with your file when it runs.

On this page

Python runs your code through an interpreter: a program that compiles your whole file to bytecode and then runs it, with no separate build step you run yourself. That means the gap between writing a line and seeing what it does is a few seconds. That is a large part of why Python is a common first language.

In this tutorial you say hello three ways: typed straight into the interpreter, saved in a file, and personalised with a name the user gives. Along the way you see three things that are specific to Python: indentation is part of the grammar, Python quietly compiles your code to bytecode and caches it, and a file can be both a script you run and a module other code imports.

Python is used for data analysis (pandas, Jupyter notebooks), AI and machine learning (PyTorch and most model tooling), automation scripts that rename files or call web APIs, and web back ends (Django, FastAPI). The few lines you write here are the same Python all of those use.

Step 1 · Get Python 3.15 and find its command

Install Python 3.15 for your system. Each system gives the interpreter a slightly different command name, so note which one is yours:

  • Ubuntu or other Linux: follow install Python 3.15 and run your first script, which installs it with uv. Your command is python3.15. Leave the system python3 alone; on Ubuntu 26.04 it is an older version the operating system depends on.
  • macOS: download the macOS installer for 3.15.2 from python.org/downloads and run it. Your command is python3.
  • Windows: install the Python install manager from python.org/downloads (or the Microsoft Store), then run py install 3.15 in a terminal. Your command is py. python also works once 3.15 is your default.

This tutorial writes python3 in every command. If your command is python3.15 or py, type that instead. Check the version now:

Bash
python3 --version

Check it worked

The terminal prints Python 3.15.2 (or a later 3.15 patch number). If it prints Python 3.14 or older, you are running a different Python than the one you installed: use the command named for your system above. command not found or 'python3' is not recognized means that name is not on your PATH, the list of folders your terminal searches for programs. That is the most common first error, and it is almost always fixed by using the right name, not by reinstalling.

Step 2 · Say hello in the interpreter

Run your Python command with nothing after it:

Bash
python3

You are now in the REPL (read, evaluate, print, loop). The >>> prompt means Python is waiting for a line. Type this and press Enter:

Python
>>> print("Hello, world")
Hello, world
>>> 7 * 6
42

print is a built-in function: it writes whatever you pass it to the screen. The round brackets call the function, and the text in double quotes is a string, Python's type for text. Single quotes work the same way. The second line shows the REPL's other habit: when you type an expression without print, it shows the value anyway. A saved script does not do that, which surprises people later.

Leave the REPL with exit() (or Ctrl+D on macOS and Linux, Ctrl+Z then Enter on Windows).

Check it worked

Hello, world appears on the line under your print call, without quotes, and 42 appears under 7 * 6. After exit() your normal terminal prompt is back.

Step 3 · Save hello.py and run it

Make a folder for this tutorial and create a file called hello.py in it. You can paste the whole block into a terminal on macOS or Linux; on Windows, create the file in your editor with the two lines between the PY markers.

Bash
mkdir -p ~/hello-python-demo
cd ~/hello-python-demo
cat > hello.py <<'PY'
# My first Python script
print("Hello, world")
PY
python3 hello.py

Line by line:

  • # My first Python script is a comment. Python ignores everything from # to the end of the line.
  • print("Hello, world") is the same call you typed in the REPL. In a file, nothing appears unless you print it.
  • python3 hello.py starts the interpreter and tells it to run that file from top to bottom, then exit.

If you installed Python with uv (the Linux route above), you can also ask uv to run the file. uv finds a matching Python for you, so the command works even when Python 3.15 is not first on your PATH. If you installed from python.org you probably do not have uv, and you do not need it for this tutorial; if you want it later, its install guide is at https://docs.astral.sh/uv/getting-started/installation/.

Bash
uv run --python 3.15 hello.py

Check it worked

python3 hello.py prints Hello, world and returns you to the prompt. If you have uv, uv run --python 3.15 hello.py prints the same line. Without uv you get command not found (or 'uv' is not recognized on Windows), which is expected: skip that line. ls in the folder shows hello.py.

Step 4 · Greet by name with input()

A program becomes useful when it responds to the person using it. input() shows a prompt, waits for the user to type a line and press Enter, and gives back what they typed. Create greet.py:

Bash
cat > greet.py <<'PY'
name = input("What is your name? ")
print("Hello,", name)
print("Your name has", len(name), "characters.")
PY
  • name = input(...) stores the typed line in a variable called name. The = means "store the value on the right under the name on the left", not "equals".
  • print("Hello,", name) prints its arguments with one space between them.
  • len(name) counts the characters in the string.

Run it and type your name when asked:

Bash
python3 greet.py

Names such as Chidinma, and names with accents and tone marks such as Adébáyọ̀, print correctly. From Python 3.15, the interpreter reads and writes text as UTF-8 by default on every system, including Windows, so multilingual text no longer depends on your computer's regional settings. len counts Unicode code points, so a letter with a combining tone mark can count as two.

Check it worked

You see What is your name?, type Ada and press Enter, and the program prints Hello, Ada and Your name has 3 characters.

Step 5 · Take the name from the command line

input() waits for a person. Scripts that run unattended, or that other programs call, take their values from the command line instead. Create greet_args.py:

Bash
cat > greet_args.py <<'PY'
import sys

if len(sys.argv) > 1:
    name = sys.argv[1]
else:
    name = "world"

print(f"Hello, {name}")
PY
python3 greet_args.py
python3 greet_args.py Grace
  • import sys loads the sys module from Python's standard library, the set of modules that ships with every install.
  • sys.argv is a list of the words on the command line. Item 0 is the script's own name, so the first word you type after it is sys.argv[1].
  • if ... : and else: end with a colon, and the lines under them are indented by four spaces. In Python the indentation is not decoration: it is how the interpreter knows which lines belong to the if. Many languages use braces for this; Python uses the spaces.
  • f"Hello, {name}" is an f-string. The f before the quote tells Python to replace {name} with the variable's value.

Check it worked

The first run prints Hello, world, because no name was given. The second prints Hello, Grace. Try python3 greet_args.py "Ada Lovelace": the quotes make the two words one argument.

Step 6 · Make the script importable and find the bytecode cache

As programs grow, one file uses functions from another with import. Importing a file runs it, which you do not always want. Python's answer is a check on the special variable __name__. Replace hello.py with this version:

Bash
cat > hello.py <<'PY'
def greeting(name):
    return f"Hello, {name}"


if __name__ == "__main__":
    print(greeting("world"))
PY
python3 hello.py
python3 -c "import hello; print(hello.greeting('Ama'))"
ls __pycache__
  • def greeting(name): defines a function: a named, reusable block. The indented line is its body, and return hands the result back to whoever called it.
  • When you run a file directly, Python sets __name__ to "__main__". When another file imports it, __name__ is the module's name, "hello". So the print under the if runs only when you run hello.py itself.
  • python3 -c "..." runs a line of code given on the command line. Here it imports hello and calls its function, without triggering the print("Hello, world") part. The code goes in double quotes on the outside and single quotes inside, because that is the form that works the same in macOS and Linux terminals, Windows PowerShell and Windows cmd. On Windows, type py -c "import hello; print(hello.greeting('Ama'))".

The ls shows something you did not create: a __pycache__ folder holding a file named like hello.cpython-315.pyc. Before running any code, Python compiles it to bytecode, a compact set of instructions for its virtual machine. When a file is imported, Python saves that bytecode so the next import can skip the compile. The 315 in the name is the Python version. The folder is safe to delete, Python recreates it, and it should not be committed to Git. The script you run directly is compiled each time and not cached.

Check it worked

python3 hello.py prints Hello, world. The import line prints only Hello, Ama, with no Hello, world before it. ls __pycache__ lists hello.cpython-315.pyc.

Step 7 · Fix the errors beginners hit

Three mistakes account for most first-day problems.

python versus python3. On macOS and Linux, python may not exist, or may start a different version. On Windows, python3 may open the Microsoft Store instead of running Python. Always run --version with the command you plan to use, as in the first step.

IndentationError. Make a file where the line under if is not indented:

Bash
cat > broken.py <<'PY'
name = "Kofi"
if name:
print("Hello,", name)
PY
Bash
python3 broken.py

Python refuses to run any of the file and reports IndentationError: expected an indented block after 'if' statement on line 2, pointing at line 3. Indent the print line by four spaces and it runs. Mixing tabs and spaces causes the same family of errors, so set your editor to insert four spaces when you press Tab.

input() always returns a string. Create age.py:

Bash
cat > age.py <<'PY'
age = input("How old are you? ")
print("Next year you will be", int(age) + 1)
PY
printf '30\n' | python3 age.py

input() gives back text, even when the user types digits. "30" + 1 fails with TypeError: can only concatenate str (not "int") to str. int(age) turns the text "30" into the number 30 first. If the user types something that is not a whole number, int() raises a ValueError; handling that is a later lesson.

When you have finished, remove the practice folder:

Bash
cd ~
rm -rf ~/hello-python-demo

Check it worked

broken.py stops with an IndentationError naming line 3, and works once the print line is indented. age.py with 30 prints How old are you? Next year you will be 31 (the prompt and answer share a line here because the input was piped in; when you type, your answer appears after the prompt).

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.